PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 4, 2022

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

 September 04, 2022     authentication, laravel, php     No comments   

Issue

The code $checklist->user_id = Auth::user()->user_id; stores the user_id from the currently logged in user in the checklist table.

On the homepage I want to view the checklist(s) from the currently logged in user and not from all the users. I can't seem to find the right code.

Checklist Table

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;

Users Table

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;

Index action of the HomeController

public function getIndex()
{
    $checklist = Checklist::all();

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

Store action from the 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);
        }
    }

User Model

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');
    }
}

Checklist Model

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');
    }

}

The Home View

@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

Solution

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

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


Answered By - Gilko
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing