LARAVEL

How to build a Project Management App in Laravel 5 – Part 4


We have the authentication system working and the dashboard is in place.

Now, let’s start creating projects.

We’ll do some clean up and re-arranging of the view and css files.

create a sidebar.blade.php in layouts/partials folder.

sidebar.blade.php

Head over to the index.blade.php and add this:

The dashboard header and the div were there before. The only thing we are adding is

Head over to your routes.php and this:

The route resource method is very cool. It takes care of all the routes for a basic crud operation.

Create the ProjectController by running this in your terminal:

It will create a ProjectController.php in your Controllers folder with some boilerplate code. ProjectController.php should be looking like this:

Now that we have done that. Run this command in your terminal

This will show you all the routes, their route names, controller methods and middleware.

If you look carefully, you will discover that there are so many routes for projects with their respective route names. Yes!!!, that’s the power of the Route::resource method, it generates all the basic crud HTTP methods and routes for you.

Let’s go ahead and create the Project Model. Run this in your terminal:

A Project.php file would be created in the root of the app folder. That’s our Project Model.

Go back to the views directory.

Create a projects folder and create two files in it namely index.blade.php and new.blade.php

projects/new.blade.php

old() is a laravel helper function that helps the form remember the previous values entered into its fields. In case, if there is a validation condition that is not fulfilled, you don’t have type all over again.

csrf_token() is  a laravel helper function that helps to prevent our forms from cross-site request forgery attacks.

projects/index.blade.php

In these two files we are including the sidebar.blade.php file with @include('layouts.partials.sidebar')

Let’s go back to our Project Controller and fix things up: Add this to the index method

So the index method returns all the current projects in the app and pass the projects to the index view.

The create method will return the view for creating a new project. It will return the new.blade.php file which is a view.

Please make sure you add this to the top of the Project Controller

just before use Prego\Http\Requests;. That references the Project Model unless the index method will return an error because we are making use of it in Project::all()

In the homepage now, we should have this:

When you click on the New Project button, it should direct us to the projects/create route which will show this page:

From the sidebar, Click on the Projects link. It should redirect you to the Projects index page

From that index page, you can easily click on the New Project button to go ahead and create a new project.

Go the projects/new.blade.php

In the form, make sure the action attribute references the route('projects.store') like you see in the code above.

Head over to the ProjectsController.php, let’s add validation and logic to the store method:

Now, there are different ways of doing this…but I did it this way for clarity. The validation rules for the project form are inside the array, then we created a new instance of Project Model and saved the inputs from our form manually into the project instance, then called the save method on it. The save() does the actual dumping of values into the database. The we redirect the user  to the Projects index page.

Note: You can check the available validation rules that Laravel provides here . You can also create a custom rule if you can’t find the one you want inbuilt with Laravel.

Auth::user()->id gets the id of the currently logged in user.

Don’t forget to include this at the top of your controller just before use PregoProject:

If you don’t, the Auth Facade won’t be found.

Now, we need to present the currently created projects to the user on the Projects index page.

Go to projects/index.blade.php, we have added some code and changed some existing one, this is the new one:

Now create a new project and refresh your page.  We should have something like this:

Awesome!!!

You have done well, dear reader. Take some time relax and test out your new functionality. You can also show your friends the cool stuff you are hacking on.

Watch out for the next post, we’ll be adding the ability to edit and update projects, add tasks to a project, attachments and also adding comments. It will be amazing.

Please if you are unclear about something or you have an observation, feel free to drop it in the comments section.

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.