How to build a Project Management App in Laravel 5 – Part 5
We need to make a little adjustment.
On the Sign In page, you will discover that if the email or password is wrong, it doesn’t show the user a message, it just blocks the user from gaining access. Let’s take care of that.
Add this in:
layouts/partials/alerts.blade.php
1 2 3 4 5 |
@if ( session()->has('warning')) <div class="alert alert-danger" role-"alert"> {{ session()->get('warning') }} </div> @endif |
Go to AuthController.php
1 2 3 |
if (!$authStatus) { return redirect()->back()->with('warning', 'Invalid Email or Password'); } |
I have changed info
to warning
in that block of code.
One more thing. Go to login.blade.php
1 2 3 |
<h3>Sign in</h3> @include('layouts.partials.alerts') <div class="row"> |
Add @include('layouts.partials.alerts')
in between the h3 tag and the div.
Now, if a user wants to login and the credentials are wrong, the user will see an appropriate error message.
Go to AuthController.php
In the postLogin function we had
1 |
return redirect()->route('index')->with('info', 'You are now signed in'); |
Change it to
1 |
return redirect()->route('projects.index')->with('info', 'You are now signed in'); |
We want the users to be redirected to see all their projects once they are logged in.
Go to views/index.blade.php
We had something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
.... .... @if ( Auth::check()) <div class="container-fluid"> <div class="row"> <div class="col-sm-3 col-md-2 sidebar"> <ul class="nav nav-sidebar"> <li style="margin-left:20px;"> <img src="{{ Auth::user()->getAvatarUrl() }}" height="50" width="50" style="border-radius:25px;" /> </li> <li><a href="#"> @ {{ Auth::user()->username }}</a></li> <li class="active"><a href="#">PREGO<span class="sr-only">(current)</span></a></li> <li><a href="#">Edit Account</a></li> <li><a href="#">Projects</a></li> <li><a href="#">Todos</a></li> </ul> <ul class="nav nav-sidebar"> <li><a href="">Account</a></li> <li><a href="">Help</a></li> <li><a href="{{ route('auth.logout') }}">Sign Out</a></li> </ul> </div> <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"> <h1 class="page-header">Dashboard</h1> <h2 class="sub-header">Projects</h2> <a class="btn btn-info" href="{{ route('projects.create') }}">New Project</a> </div> </div> </div> @endif |
We are going to replace part of that code with the sidebar blade template. We don’t want repetition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
... ... @if ( Auth::check()) <div class="container-fluid"> <div class="row"> @include('layouts.partials.sidebar') <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"> <h1 class="page-header">Dashboard</h1> <h2 class="sub-header">Projects</h2> <a class="btn btn-info" href="{{ route('projects.create') }}">New Project</a> </div> </div> </div> @endif |
We have removed the sidebar div and replaced it with @include('layouts.partials.sidebar')
.
If you don’t have the sidebar template, create a sidebar.blade.php file in layouts/partials folder and dump the contents in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<div class="col-sm-3 col-md-2 sidebar"> <ul class="nav nav-sidebar"> <li style="margin-left:20px;"> <img src="{{ Auth::user()->getAvatarUrl() }}" height="50" width="50" style="border-radius:25px;" /> </li> <li><a href="#"> @ {{ Auth::user()->username }}</a></li> <li class="active"><a href="#">PREGO<span class="sr-only">(current)</span></a></li> <li><a href="#">Edit Account</a></li> <li><a href="{{ route('projects.index') }}">Projects</a></li> <li><a href="#">Todos</a></li> </ul> <ul class="nav nav-sidebar"> <li><a href="">Account</a></li> <li><a href="">Help</a></li> <li><a href="{{ route('auth.logout') }}">Sign Out</a></li> </ul> </div> |
Now, if a new user creates an account and logs in, the user can see the projects of other users. That doesn’t make sense. We need to make the user have access to only his/her own projects.
Let’s get to work
We’ll use the concepts of Scopes in Laravel. Query scopes are convenient ways to isolate often used conditions when querying your database for information.
Go to the Project model
Project.php
Add this function to the class:
1 2 3 4 |
public function scopePersonal($query) { return $query->where('user_id', Auth::user()->id); } |
Make sure you require Auth at the top like so:
1 |
use Auth; |
just before use IlluminateDatabaseEloquentModel;
Now, go to ProjectController.php
1 2 3 4 5 6 7 |
.... .... public function index() { $projects = Project::all(); return view('projects.index')->withProject($projects); } |
will become
1 2 3 4 5 6 7 |
.... .... public function index() { $projects = Project::personal()->get(); return view('projects.index')->withProject($projects); } |
We just called the personal() from the QueryScope method we wrote in the Project Model. Awesome!!!
If you reload your projects page. You will discover that a user can only see his/her own projects.
Time to view each Project
Create a show.blade.php in projects folder
show.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
@extends('layouts.master') @section('content') @include('layouts.partials.sidebar') <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"> @include('layouts.partials.alerts') @if( $project ) <h1 class="page-header"> {!! $project->project_name !!} </h1> <div class="container"> <div class="row"> <div class="col-md-3" style="border:1px solid #ccc;margin-left:5px;padding:10px;"> <p>Due : {!! date_format(new DateTime($project->due_date), "D, m Y") !!}</p> <p>Status: {!! $project->project_status !!}</p> <p>Tasks: 0</p> <p>Comments: 0</p> <p>Attachments: 0</p> <p><a href="#">Edit</a></p> <p><a href="#">Delete</a></p> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <h4 class="page-header"> Tasks </h4> <div class="row" style="border:1px solid #ccc;margin-left:5px;width:100%;padding:15px;"> <form class="form-vertical" role="form" method="post" action="#"> <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> <input type="text" name="name" class="form-control" id="name" value="{{ old('name') ?: '' }}"> @if ($errors->has('name')) <span class="help-block">{{ $errors->first('name') }}</span> @endif </div> <div class="form-group"> <button type="submit" class="btn btn-info">Create Task</button> </div> <input type="hidden" name="_token" value="{{ csrf_token() }}"> </form> </div> </div> <div class="col-md-5"> <h4 class="page-header"> Files </h4> <div class="row" style="border:1px solid #ccc;margin-left:5px;width:100%;padding:15px;"> <form class="form-vertical" role="form" method="post" action="#"> <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> <input type="text" name="name" class="form-control" id="name" value="{{ old('name') ?: '' }}"> @if ($errors->has('name')) <span class="help-block">{{ $errors->first('name') }}</span> @endif </div> <div class="form-group"> <button type="submit" class="btn btn-info">Add Files</button> </div> <input type="hidden" name="_token" value="{{ csrf_token() }}"> </form> </div> </div> </div> <hr> <div class="row"> <h4 class="page-header"> Comments </h4> <div class="row" style="margin-left:5px;padding:5px;"> <form class="form-vertical" role="form" method="post" action="#"> <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> <textarea name="comment" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea> @if ($errors->has('comment')) <span class="help-block">{{ $errors->first('comment') }}</span> @endif </div> <div class="form-group"> <button type="submit" class="btn btn-info">Add Comment</button> </div> <input type="hidden" name="_token" value="{{ csrf_token() }}"> </form> </div> </div> </div> @endif </div> @stop |
Go to ProjectController.php
We already have a show function created by Laravel when we used the command to create the controller earlier.
Let’s add to the function
1 2 3 4 5 |
public function show($id) { $project = Project::find($id); return view('projects.show')->withProject($project); } |
Project::find($id);
basically does a select * from projects where id = $id
and then return the results to the show view.
Go to projects/index.blade.php
1 2 3 4 |
.... .... <h2><a href="/projects/{{ $proj->id }}">{!! $proj->project_name !!}</a></h2> |
Before we didn’t have a link there, but now we want the name of the project to be clickable. $proj->id
gets the id. So, if the id of that particular project is 5, then we’ll have a link like projects/5
.
When a user clicks on that link, the id is passed to the show method in the Project Controller that then renders the features of that project in the view.
I have also added the tasks, files and comments form.
Now we can’t edit or delete the project yet, but we’ll take care of that functionality in the next project.
Please if you have any questions or observations, feel free to drop it in the comments section here.

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016