代码$清单-
在主页上,我想查看当前登录用户的清单,而不是所有用户的清单。我似乎找不到正确的代码。
检查表
CREATE TABLE IF NOT EXISTS `festival_aid`.`checklists` (
`checklist_id` BIGINT NOT NULL AUTO_INCREMENT,
`checklist_body` VARCHAR(45) NOT NULL,
`checklist_created` TIMESTAMP NOT NULL,
`checklist_modified` TIMESTAMP NULL,
`checklist_deleted` TIMESTAMP NULL,
`user_id` BIGINT NOT NULL,
PRIMARY KEY (`checklist_id`),
INDEX `fk_checklists_users_idx` (`user_id` ASC),
CONSTRAINT `fk_checklists_users`
FOREIGN KEY (`user_id`)
REFERENCES `festival_aid`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
用户表
CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
`user_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_username` VARCHAR(45) NOT NULL,
`user_email` VARCHAR(255) NOT NULL,
`user_password` VARCHAR(255) NOT NULL,
`user_salt` CHAR(32) NULL,
`user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_modified` TIMESTAMP NULL,
`user_deleted` TIMESTAMP NULL,
`user_lastlogin` TIMESTAMP NULL,
`user_locked` TIMESTAMP NULL,
`user_token` VARCHAR(128) NULL,
`user_confirmed` TIMESTAMP NULL,
PRIMARY KEY (`user_id`),
UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
ENGINE = InnoDB;
HomeController的索引操作
public function getIndex()
{
$checklist = Checklist::all();
return View::make('home.index')
->with('checklist', $checklist);
}
存储来自ChecklistController的操作
public function store()
{
$input = Input::all();
$rules = array(
'checklist_body' => 'required'
);
$validator = Validator::make($input, $rules);
if($validator->passes())
{
$checklist = new Checklist();
$checklist->user_id = Auth::user()->user_id;
$checklist->checklist_body = $input['checklist_body'];
$checklist->save();
Session::flash('message', 'Successfully created a checklist!');
return Redirect::to('checklists');
}
else {
return Redirect::to('checklists/create')->withInput()->withErrors($validator);
}
}
用户模型
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'users';
protected $primaryKey = 'user_id';
protected $hidden = ["password"];
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthPassword()
{
return $this->user_password;
}
public function getReminderEmail()
{
return $this->email;
}
public $timestamps = false;
public function location()
{
return $this->belongsTo('Location', 'location_id', 'location_id');
}
public function person()
{
return $this->belongsTo('Person', 'person_id', 'person_id');
}
public function checklist()
{
return $this->belongsTo('Checklist', 'checklist_id', 'checklist_id');
}
}
清单模型
class Checklist extends Eloquent {
protected $guarded = array();
public static $rules = array();
protected $primaryKey = 'checklist_id';
public $timestamps = false;
public function user()
{
return $this->hasMany('Checklist', 'checklist_id', 'checklist_id');
}
}
主视图
@extends('layouts.master')
@section('content')
@if(Auth::user())
Welcome {{ ucwords(Auth::user()->user_username) }} in the backoffe off Festival Aid!
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>ID</td>
<td>Body</td>
<td>Created</td>
</tr>
</thead>
<tbody>
@foreach($checklist as $checklist)
<tr>
<td>{{ $checklist->checklist_id }}</td>
<td>{{ $checklist->checklist_body }}</td>
<td>{{ $checklist->checklist_created }}</td>
<td>
{{ Form::open(array('url' => 'checklists/' . $checklist->checklist_id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{Form::button('<i class="glyphicon glyphicon-remove"></i>', array('type' => 'submit', 'class' => ''))}}
{{ Form::close() }}
<a class="" href="{{ URL::to('checklists/' . $checklist->checklist_id) }}"><i class="glyphicon glyphicon-user"></i></a>
<a class="" href="{{ URL::to('checklists/' . $checklist->checklist_id . '/edit') }}"><i class="glyphicon glyphicon-edit"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>Welcome in de backoffe van Festival Aid!</p>
<p>Gelieve in te loggen.</p>
@endif
@stop
public function getIndex()
{
$checklist = Checklist::where('user_id', Auth::user()->user_id)->get();
return View::make('home.index')
->with('checklist', $checklist);
}
我想你可能弄乱了用户和检查表之间的关系:
$user-
有关执行的SQL语句,请参见一对一文档,这在一对多关系中几乎相同:http://laravel.com/docs/eloquent#one-一
在您的控制器中,您可以使用:
$checklists = Auth::user()->checklists();
请注意,我编写了检查表(复数),因为当您希望返回一个或多个项目时,这是有意义的。
问题内容: 以下是允许用户将数据存储到Firestore中的代码。问题是,登录到该应用程序的所有用户都可以查看上传的数据。我如何才能仅允许当前登录的用户仅存储其帐户的数据? 对于Firebase数据库,我使用以下代码(有效,但不适用于Firestore) FirebaseUser用户= FirebaseAuth.getInstance()。getCurrentUser(); 字符串userId =
我想设置以下工作流: 最初,在没有登录的情况下,Swagger只显示2-3个endpoint——这将通过从后端提供有限的OpenAPI3JSON来实现,没有问题 有可能和斯威格一起做第三项吗?我如何手动发出请求从Swagger与OAuth2承载令牌(因为用户登录,令牌必须在某些地方)和重绘Swagger页面?
如何获取当前登录用户的型号?例如,如果系统职员和管理员中有两种类型的用户?我如何知道用户属于哪个模型?
任何人都可以帮助我使用Laravel登录用户,这是我的尝试: 这是错误: 传递给Illumate\Auth\Guard::__construct()的参数1必须是Illumate\Couts\Auth\UserProvider的实例,没有给出,在第31行 /home/comment/public_html/bild/app/AuthenticateUser.php调用并定义
X1.0新增 sp_update_current_user($user) 功能: 更新session里当前登录用户的信息 参数: $user:当前登录用户的最新信息 返回: 无
我对Laravel有点陌生,我已经设置了一个表用户,其中包含用户名和密码字段。 我的登录屏幕上有一个表单,其中正式声明了这些字段,我有一个函数是UserController: 我知道这是顺利的,因为如果我空白提交表单,我可以看到错误消息。 我已经删除了默认的用户认证。 当我提交时,我得到: 错误异常在EloquentUserProvider.php第114行:参数1传递给Illumate\Auth