Laravel 5.5 CRUD Example from scratch

Laravel 5.5 CRUD Example from scratch

In this post we will give you information about Laravel 5.5 CRUD Example from scratch. Hear we will give you detail about Laravel 5.5 CRUD Example from scratchAnd how to use it also give you demo for it if it is necessary.

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.

New Tutorial:Laravel 5.7 CRUD Operation Tutorial

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' => [

....

CollectiveHtmlHtmlServiceProvider::class,

],

'aliases' [

....

'Form' => CollectiveHtmlFormFacade::class,

'Html' => CollectiveHtmlHtmlFacade::class,

],

Also see:Laravel 5.2 and AngularJS CRUD with Search and Pagination Example.

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 IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


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 IlluminateDatabaseEloquentModel;


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 AppHttpControllers;


use IlluminateHttpRequest;

use AppArticle;


class ArticleController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

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 IlluminateHttpResponse

*/

public function create()

{

return view('articles.create');

}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

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 IlluminateHttpResponse

*/

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 IlluminateHttpResponse

*/

public function edit($id)

{

$article = Article::find($id);

return view('articles.edit',compact('article'));

}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param int $id

* @return IlluminateHttpResponse

*/

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 IlluminateHttpResponse

*/

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 >

@yield('content')

</div>


</body>

</html>

resources/views/articles/index.blade.php



@extends('layout')


@section('content')

<div >

<div >

<div >

<h2>Laravel 5.5 CRUD Example from scratch</h2>

</div>

<div >

<a href="{{ route('articles.create') }}"> Create New Article</a>

</div>

</div>

</div>


@if ($message = Session::get('success'))

<div >

<p>{{ $message }}</p>

</div>

@endif


<table >

<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 href="{{ route('articles.show',$article->id) }}">Show</a>

<a 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 >

<div >

<div >

<h2> Show Article</h2>

</div>

<div >

<a href="{{ route('articles.index') }}"> Back</a>

</div>

</div>

</div>


<div >

<div >

<div >

<strong>Title:</strong>

{{ $article->title}}

</div>

</div>

<div >

<div >

<strong>Body:</strong>

{{ $article->body}}

</div>

</div>

</div>

@endsection

resources/views/articles/form.blade.php



<div >

<div >

<div >

<strong>Title:</strong>

{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Body:</strong>

{!! Form::textarea('body', null, array('placeholder' => 'Body','class' => 'form-control','style'=>'height:150px')) !!}

</div>

</div>

<div >

<button type="submit" >Submit</button>

</div>

</div>

resources/views/articles/create.blade.php



@extends('layout')


@section('content')

<div >

<div >

<div >

<h2>Add New Article</h2>

</div>

<div >

<a href="{{ route('articles.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) < 0)

<div >

<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 >

<div >

<div >

<h2>Edit Article</h2>

</div>

<div >

<a href="{{ route('articles.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) > 0)

<div >

<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:

Also see:Laravel 5.7 CRUD (Create Read Update Delete) Tutorial Example


http://localhost:8000/articles

I hope it can help you…

Hope this code and post will helped you for implement Laravel 5.5 CRUD Example from scratch. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

82  +    =  84

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US