繁体   English   中英

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

[英]How can you only show the checklists from the currently logged in user - Laravel

代码$ 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

我认为您可能已经弄乱了用户和清单之间的关系:

$ user-> checklists()应该是hasMany而不是belongsTo。 $ checklist-> user()应该是belongsTo。

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

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

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

请注意,我编写了清单(复数),因为当您希望退回一个或多个项目时,这样做很有意义。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM