Laravel 5.5 CRUD Example from scratch
In this tutorial i will shows how you can create basic crud(create, read, update and delete) module in laravel 5.5 application. Laravel is a popular open-source PHP MVC Framework with lots of advanced development features. Laravel released it's new version 5.5 as few days ago.
If you are learner or beginner of crud example in laravel 5.5 application then you are a right place. It's always help to more understand or learn from crud application for beginner developer.
Here, i will create insert, update, delete and view with pagination example of articles. You can simply create new article, view article, edit article and remove article from lists. I make very simple example using Laravel Form builder using resource route. I make resource controller for crud application in laravel 5.5. So you have to just follow few step and get full example of CRUD application. end of article you will get layout like as bellow screen shot.
Layout:
Step 1 : Install Laravel 5.5
I am going to show you scratch, So we require to get fresh Laravel 5.5 version application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2 : Install Laravel Collective
Here, we will install laravelcollective/html composer package for form builder so open your terminal and run bellow command:
composer require laravelcollective/html
After successfully install package, you have to open config/app.php file and add service provider and alias.
config/app.php
'providers' => [
....
Collective\Html\HtmlServiceProvider::class,
],
'aliases' [
....
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
],
Step 3: Database Configuration
In this step we have to make database configuration for example database name, username, password etc for our crud application of laravel 5.5. So let's open .env file and fill all details like as bellow:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)
Step 4: Create articles Table and Model
we are going to create crud application for article. so we have to create migration for articles table using Laravel 5.5 php artisan command, so first fire bellow command:
php artisan make:migration create_articles_table
After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create articles table.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticalesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
After creating table we have to create model for "articles" table so just run bellow command and create new model:
php artisan make:model Article
Ok, so after run bellow command you will find app/Article.php and put bellow content in Article.php file:
app/Article.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'body'
];
}
Step 5: Add Resource Route
In this is step we need to add resource route for article crud application. so open your routes/web.php file and add following route.
routes/web.php
Route::resource('articles','ArticleController');
Step 6: Create ArticleController
Here, now we should create new controller as ArticleController. So run bellow command and create new controller. bellow controller for create resource controller.
php artisan make:controller ArticleController -resource
After bellow command you will find new file in this path app/Http/Controllers/ArticleController.php.
In this controller will create seven methods by default as bellow methods:
1)index()
2)create()
3)store()
4)show()
5)edit()
6)update()
7)destroy()
So, let's copy bellow code and put on ArticleController.php file.
app/Http/Controllers/ArticleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$articles = Article::latest()->paginate(5);
return view('articles.index',compact('articles'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('articles.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
request()->validate([
'title' => 'required',
'body' => 'required',
]);
Article::create($request->all());
return redirect()->route('articles.index')
->with('success','Article created successfully');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$article = Article::find($id);
return view('articles.show',compact('article'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$article = Article::find($id);
return view('articles.edit',compact('article'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
request()->validate([
'title' => 'required',
'body' => 'required',
]);
Article::find($id)->update($request->all());
return redirect()->route('articles.index')
->with('success','Article updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Article::find($id)->delete();
return redirect()->route('articles.index')
->with('success','Article deleted successfully');
}
}
Step 7: Create Blade Files
now we move in last step. In this step we have to create just blade files. So mainly we have to create layout file and then create new folder "articles" then create blade files of crud app. So finally you have to create following bellow blade file:
1) layout.blade.php
2) index.blade.php
3) show.blade.php
4) form.blade.php
5) create.blade.php
6) edit.blade.php
So let's just create following file and put bellow code.
resources/views/layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 5.5 CRUD Application</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
resources/views/articles/index.blade.php
@extends('layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 5.5 CRUD Example from scratch</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('articles.create') }}"> Create New Article</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Title</th>
<th>Body</th>
<th width="280px">Action</th>
</tr>
@foreach ($articles as $article)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $article->title}}</td>
<td>{{ $article->body}}</td>
<td>
<a class="btn btn-info" href="{{ route('articles.show',$article->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('articles.edit',$article->id) }}">Edit</a>
{!! Form::open(['method' => 'DELETE','route' => ['articles.destroy', $article->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</table>
{!! $articles->links() !!}
@endsection
resources/views/articles/show.blade.php
@extends('layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Article</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('articles.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
{{ $article->title}}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Body:</strong>
{{ $article->body}}
</div>
</div>
</div>
@endsection
resources/views/articles/form.blade.php
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Body:</strong>
{!! Form::textarea('body', null, array('placeholder' => 'Body','class' => 'form-control','style'=>'height:150px')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
resources/views/articles/create.blade.php
@extends('layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Article</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('articles.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) < 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors-<all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::open(array('route' => 'articles.store','method'=>'POST')) !!}
@include('articles.form')
{!! Form::close() !!}
@endsection
resources/views/articles/edit.blade.php
@extends('layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Article</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('articles.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::model($article, ['method' => 'PATCH','route' => ['articles.update', $article->id]]) !!}
@include('articles.form')
{!! Form::close() !!}
@endsection
Now we are ready to run our crud application example so run bellow command for quick run:
php artisan serve
Now you can open bellow URL on your browser:
http://localhost:8000/articles
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Delete Multiple Records using Checkbox in Laravel?
- Laravel Confirmation Before Delete Record from Database
- Laravel Image Gallery CRUD Example Tutorial
- PHP CRUD Operation Using Ajax and JQuery Example
- Laravel AJAX CRUD Tutorial Example
- Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch
- Laravel 5.2 and AngularJS CRUD with Search and Pagination Example.