如何只显示当前登录用户的清单 - Laravel

时间:2013-12-29 18:01:40

标签: php authentication laravel logged

代码$ checklist-> user_id = Auth :: user() - > user_id;将当前登录用户的user_id存储在核对表表中。

在主页上,我想查看当前登录用户的清单,而不是所有用户。我似乎无法找到合适的代码。

清单表

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

2 个答案:

答案 0 :(得分:0)

我想你可能搞砸了用户和清单之间的关系:

$ user-&gt; checklists()应该是hasMany而不是belongsTo。 $ checklist-&gt; user()应该是belongsTo。

请参阅执行的SQL语句的一对一文档,这在一对多关系中几乎相同:http://laravel.com/docs/eloquent#one-to-one

在您的控制器中,您可以使用:

$checklists = Auth::user()->checklists();

请注意,我写了清单(复数),因为当你想要返回一个或多个项目时,这是有意义的。

答案 1 :(得分:0)

public function getIndex()
{
    $checklist = Checklist::where('user_id', Auth::user()->user_id)->get();

    return View::make('home.index')
        ->with('checklist', $checklist);
}