LARAVEL

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


Let’s go to show.blade.php

I have added the <a href="/projects/{{ $project->id }}/edit> link. So if you click on it right now, it shows an empty page.

Let’s add the view for that.

Go to ProjectController.php

Add this to the edit function

So it fetches the details of the project and sends it to the edit view to present them to the user.

Now, create an edit.blade.php in the views/projects folder

edit.blade.php

In the edit view, you can see that the action for the form routes to the update function in the  ProjectController

Take a look at this part of the edit.blade.php

This is in the select form field. We did this to determine which other project states to show in the drop down if the project status is a certain state.

So if the project status is Upcoming, then the other options would be Active and Completed.

Now this implementation is actually ugly and it  bloats our view file and it means we are now putting real logic in our view files which is wrong to do.

Let’s do it in a better way.

We will create a helper function to take care of that.

Create a helpers.php file in the app/Http directory.

helpers.php

Here we just loop through the states, remove the current project state and return the other potential project status.

Go to your composer.json file and make it look like this:

The only thing we are actually adding is this:

We want Laravel to be able to see our helper function, so that we can use it in any part of the app.

Now run composer dumpautoload from the terminal.

This makes sure the autoloader can see the helper file.

Go back to the edit.blade.php and replace our earlier code with

Just one line of code. Is that not beautiful and clean?. :smile:

Now, the edit.blade.php looks like this:

Go to the Project Model

Project.php Add this:

You are making the fields fillable so that the update operation can work seamlessly.

Go to ProjectController.php

In the update function add this:

That validates the fields and gets all the form values.

$request->all() returns all the form values in an array

$project->fill($values)->save() actually replaces the database values with the new values from the edit form.

..then a success message is shown to the user.

 

Time to implement the Delete functionality.

Go to show.blade.php

Replace the delete link with this:

We are going to use Javascript here to make the interaction more user-friendly and fast.

Go to the public directory  and create a js folder.

Create an app.js inside the js folder and copy this into it:

The gist file is here: Gist

This fakes a delete request by making a post request to the /projects/{project} route and sends a delete method alongside. So when the user clicks OK, it sends an ajax request to the destroy method in the ProjectController.php, then redirects back to the projects index page.

Go to your master.blade.php and add this:

You are linking a jQuery file and the app.js file.

The destroy method in ProjectController.php should contain this:

This does the actual deletion.

Hurray!!!..Now we can create, edit and delete a project.

Next post, we’ll learn how to create tasks and attachments for each project. Stay tuned.

Please if you have any question or 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.