Laravel CRUD Application for Beginner – Scratch
Before kick off developing advance application in Laravel, you should clearly understand the basics of Laravel CRUD (Create, Read, Update and Delete operation). In this lesson, I would like to create a basic CRUD application in Laravel from scratch. I show you step by step process of creating CRUD application. So, do follow each and every step.
Because of a popular MVC framework and large security concerns with various advanced features in Laravel, it has become popular among php developers.
If you are a beginner in laravel, this tutorial will be very fruitful to you. Here I will create a blog having article with CRUD features.
In this blog, I will make a form to insert and edit an article. Then, I will put all the created articles in a list and add facility to delete each article. The final output after completing whole process will be like of following:

Here I am going to show you a way to install latest laravel framework in windows. Click here to find guide for laravel installation.
Just run the below command in command prompt/terminal.
composer create-project --prefer-dist laravel/laravel crudAppsDemo
After laravel installation is completed, it’s the time to configure database. Just create a database called “crudApps” in phpmyadmin.
Now open .env file of crudAppsDemo and do as follows:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crudApps // database name
DB_USERNAME=username // database username
DB_PASSWORD=password // database password
Now, I am going to create a table “articles” here and then make model for that article. To make migration with indicating table name users, just use below code.
php artisan make:migration create_articles_table --create=users
After you run above code, you need to add table columns in migration. To do that, go to >>crudAppsDemo/database/migrations/……create_articles_table.php
Now, do as follows:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
Now, you are required to migrate it. Run below code to migrate new migration
Read also: Syntax error or access violation: 1071 specified key was too long
php artisan make:migrate
Here we require to make a controller called “articleController”. You should know that a controller accepts input and converts it to commands for model and view.
It is easy to create controller in laravel; you just need to run a command.
php artisan make:controller articleController --model=Article
Inside Article model, you just need to define fillable fields for articles table. Model should be like below:
app/Http/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', 'description',
];
}
In articleController, you need to create following methods:
- index() – to show all articles
- create() – to open create form
- store() – to store data submitted through create form
- edit() – to open edit form
- update() – to update data submitted through edit form
- destroy() – to delete a row
Here is a bunch of code for articleController. Just copy it and add in articleController. app/Http/Controllers/articleController.php
<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
class articleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$articles = Article::paginate(4);
return view('article.index',['articles'=>$articles]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('article.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique:articles|max:255',
'description' => 'required',
]);
$article = new Article;
$article->title = $request->title;
$article->description = $request->description;
$article->save();
return redirect('article/create')->with('status','saved');
}
/**
* Display the specified resource.
*
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function show(Article $article)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function edit(Article $article)
{
$article = Article::findOrFail($article->id);
return view('article.edit',['article'=>$article]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Article $article)
{
$article->title = $request->title;
$article->description = $request->description;
$article->save();
return redirect('article/'.$article->id.'/edit')->with('status','updated!');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Article $article
* @return \Illuminate\Http\Response
*/
public function destroy(Article $article)
{
$article = Article::findOrFail($article->id);
$article->delete();
return redirect('article')->with('status','Deleted!');
}
}
In laravel, basic route accepts a uri and closure. We use 3 basic methods in laravel routes: get, post and delete. But, in this tutorial, I am going to use resourceful routing which requires resource controller.
Now, just add below code in web.php file inside routes folder.
Route::resource('article','articleController'); // resource routing
Laravel resourceful route performs routing of CRUD operation. Click here to find more about laravel resourceful route.
Here I am creating 3 different blade files for CRUD application. This is needed to do manually. First make a folder called “article” inside resources>>views, then create following files.
- index.php
- create.php
- edit.php
resources/views/index.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<div class="card-body">
<div class="row card-header"><div class="col-md-9"><h3>{{ __('Article List') }}</h3></div> <div class="col-md-3"><a class="btn btn-success" href="{{url('article/create')}}">New</a></div></div>
<table class="table table-condensed">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Action</th>
</tr>
@foreach($articles as $article)
<tr>
<td>{{$article->title}}</td>
<td>{{$article->description}}</td>
<td>
<form method="POST" action="{{url('article/'.$article->id)}}">
@csrf
{{ method_field('DELETE') }}
<a class="btn btn-primary" href="{{url('article/'.$article->id.'/edit')}}">Edit</a>
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</thead>
</table>
{{ $articles->links() }}
</div>
</div>
</div>
</div>
</div>
@endsection
resources/views/create.php
<form class="form" method="POST" action="{{ url('/article') }}">
@csrf
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" value="{{ old('name') }}" placeholder="Enter Title">
@if ($errors->has('title'))
<span class="text-danger">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" class="form-control" placeholder="Enter Description">{{ old('description') }}</textarea>
@if ($errors->has('description'))
<span class="text-danger">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-primary">
{{ __('Submit') }}
</button>
</form>

resources/views/edit.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-md-9"><h3>{{ __('Edit Article - ').$article->title }}</h3></div> <div class="col-md-3"><a class="btn btn-success" href="{{url('article/create')}}">New</a></div></div>
</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<form class="form" method="POST" action="{{ url('/article/'.$article->id) }}">
@csrf
{{ method_field('PUT') }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" value="{{ $article->title }}" placeholder="Enter Title">
@if ($errors->has('title'))
<span class="text-danger">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" class="form-control" placeholder="Enter Description">{{ $article->description }}</textarea>
@if ($errors->has('description'))
<span class="text-danger">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-primary">
{{ __('Update') }}
</button>
</form>
</div>
</div>
</div>
</div>
@endsection

Eventually, just use below command to run CRUD application.
php artisan serve
Now, you can browse following url in the browser.
http://localhost:8000/article/