CRUD (Create Read Update Delete) in a Laravel App

An excerpt from http://www.sitepoint.com/crud-create-read-update-delete-laravel-app/, by @callmenick

This entry is part 2 of 2 in the series “How to Build a Basic Laravel CRUD App
In the previous part, we’ve bootstrapped our Laravel CRUD application by creating the database, some controllers, basic routes and simple views. In this part, we’ll wrap things up and implement proper CRUD.
If you’d like to follow along through this interactive walk through Laravel’s docs, please catch up by reading the first part now.

Creating A Record

Continuing right where we left off, let’s create the page where we’ll actually perform this action. In our TasksController, let’s return a view like this:

public function create()
{
    return view('tasks.create');
}

And now, in our views directory, let’s create tasks/create.blade.php, and enter some starter content:

@extends('layouts.master')
 
@section('content')
 
<h1>Add a New Task</h1>
<p class="lead">Add to your task list below.</p>
<hr>
 
@stop

At this point, we could manually create a form, but Laravel offers a package to lighten this load for us – Illuminate/Html. Let’s pull that in quickly by running the following command:

composer require illuminate/html

Now, inside our config/app.php file, let’s add the service provider to the list:

'Illuminate\Html\HtmlServiceProvider',

Let’s add the aliases:

'Form'      => 'Illuminate\Html\FormFacade',
'Html'      => 'Illuminate\Html\HtmlFacade',

We can now easily create a form in our create.blade.php file. Let’s go ahead and do that using the form facade and blade syntax:

{!! Form::open([
    'route' => 'tasks.store'
]) !!}
 
<div class="form-group">
    {!! Form::label('title', 'Title:', ['class' => 'control-label']) !!}
    {!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
 
<div class="form-group">
    {!! Form::label('description', 'Description:', ['class' => 'control-label']) !!}
    {!! Form::textarea('description', null, ['class' => 'form-control']) !!}
</div>
 
{!! Form::submit('Create New Task', ['class' => 'btn btn-primary']) !!}
 
{!! Form::close() !!}

Check out this screenshot of our create view so far.

One important thing to note here is that I’ve specified the route on which we will POST to, according to our resourceful routes list. We’ll use the store method to process the data, so let’s head back to our TasksController, and start processing that data.

Continue reading this article on SitePoint!

I posed this question in the article comments and have received no response yet, so I hope I am not breaking any rules by also posting it here. Unfortunately, I can’t continue with this tutorial until I clear this up, so if anyone familiar with Laravel has read this article here is my issue:

I got as far as creating the store method and got this error "FatalErrorException in TasksController.php line 40: “Class ‘App\Http\Controllers\Task’ not found”.

Why is it looking for my Task class in the App\Http\Controllers folder when the command 'php artisan make:model Task" placed the class in the App folder?

I moved a post to a new topic: New Articles are using Disqus?

Hi WebMachine,

To use a class from a different namespace you need to tell PHP where to find it, either by referencing the class using its full namespace:

\App\Task::create($input);

or by adding a use statement at the top of your controller:

<?php namespace App\Http\Controllers;

use App\Task;

class TasksController extends Controller {

Thank you, @fretburner. This was not mentionned anywhere in the article, and being very new to Laravel (and any php framework for that matter), I still don’t know what the framework takes care of for me, and what I need to do myself.

Two of us posted this same question on part I and part II in the article without any response. Since this thread answered the question so well, I added a comment to the articles linking to this thread. The comments were held up in a moderation queue for a while, and when I got back to check on them, they had both vanished. Is that because they were deleted (not having passed moderation)? If so, why. This answer would have been very helpful to the other person asking about and maybe future readers. Did I break any rules?

That’s very strange, I doubt you broke any rules. Can you re-post your comments? We accept all comments except obvious spam, to articles.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.