Laravel 5.6 CRUD Application for Starter

Laravel 5.6 CRUD Application for Starter

In this post we will give you information about Laravel 5.6 CRUD Application for Starter. Hear we will give you detail about Laravel 5.6 CRUD Application for StarterAnd how to use it also give you demo for it if it is necessary.

Here, I would like to share with you basic crud(create, read, update and delete) application module in laravel 5.6 version. In this port i will show explain simple inert update delete application in laravel 5.6 project. You have to just follow bellow step to create CRUD app in laravel 5.6.

Laravel is a popular open-source PHP MVC Framework with lots of advanced development features. Laravel released it’s new version 5.6 as few days ago. If you are learner or beginner of crud example in laravel 5.6 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 products. You can simply create new product, view product, edit product and remove product from lists. I make very simple example using Laravel Form builder using resource route. I make resource controller for crud application in laravel 5.6. So you have to just follow few step and get full example of CRUD application. end of product you will get layout like as bellow screen shot.

New Tutorial:Laravel 5.7 CRUD Operation Tutorial

Layout:

Step 1 : Install Laravel 5.6

I am going to show you scratch, So we require to get fresh Laravel 5.6 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: Database Configuration

After complete installation, we will make database configuration for example database name, username, password etc for our crud application of laravel 5.6. 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)

Also see:Codeigniter 3 and AngularJS CRUD with Search and Pagination Example.

Step 3: Create products Table and Model

we are going to create crud application for product. so we have to create migration for products table using Laravel 5.6 php artisan command, so first fire bellow command:



php artisan make:migration create_products_table --create=products

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 products table.



<?php


use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateProductsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('products', function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->text('detail');

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('products');

}

}


Step 4: Add Resource Route

In this is step we need to add resource route for product crud application. so open your routes/web.php file and add following route.

routes/web.php



Route::resource('products','ProductController');

Step 5: Create ProductController

Here, now we should create new controller as ProductController. So run bellow command and create new controller. bellow controller for create resource controller.



php artisan make:controller ProductController --resource --model=Product

After bellow command you will find new file in this path app/Http/Controllers/ProductController.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 ProductController.php file.

app/Http/Controllers/ProductController.php



<?php


namespace AppHttpControllers;


use AppProduct;

use IlluminateHttpRequest;


class ProductController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index()

{

$products = Product::latest()->paginate(5);


return view('products.index',compact('products'))

->with('i', (request()->input('page', 1) - 1) * 5);

}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{

return view('products.create');

}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

request()->validate([

'name' => 'required',

'detail' => 'required',

]);


Product::create($request->all());


return redirect()->route('products.index')

->with('success','Product created successfully.');

}


/**

* Display the specified resource.

*

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function show(Product $product)

{

return view('products.show',compact('product'));

}


/**

* Show the form for editing the specified resource.

*

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function edit(Product $product)

{

return view('products.edit',compact('product'));

}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function update(Request $request, Product $product)

{

request()->validate([

'name' => 'required',

'detail' => 'required',

]);


$product->update($request->all());


return redirect()->route('products.index')

->with('success','Product updated successfully');

}


/**

* Remove the specified resource from storage.

*

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function destroy(Product $product)

{

$product->delete();


return redirect()->route('products.index')

->with('success','Product deleted successfully');

}

}

Ok, so after run bellow command you will find app/Product.php and put bellow content in Product.php file:

app/Product.php



<?php


namespace App;


use IlluminateDatabaseEloquentModel;


class Product extends Model

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name', 'detail'

];

}

Step 6: 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 “products” 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/products/layout.blade.php



<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.6 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/products/index.blade.php



@extends('products.layout')


@section('content')

<div >

<div >

<div >

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

</div>

<div >

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

</div>

</div>

</div>


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

<div >

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

</div>

@endif


<table >

<tr>

<th>No</th>

<th>Name</th>

<th>Details</th>

<th width="280px">Action</th>

</tr>

@foreach ($products as $product)

<tr>

<td>{{ ++$i }}</td>

<td>{{ $product->name }}</td>

<td>{{ $product->detail }}</td>

<td>

<form action="{{ route('products.destroy',$product->id) }}" method="POST">


<a href="{{ route('products.show',$product->id) }}">Show</a>

<a href="{{ route('products.edit',$product->id) }}">Edit</a>


@csrf

@method('DELETE')


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

</form>

</td>

</tr>

@endforeach

</table>


{!! $products->links() !!}


@endsection

resources/views/products/show.blade.php



@extends('products.layout')


@section('content')

<div >

<div >

<div >

<h2> Show Product</h2>

</div>

<div >

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

</div>

</div>

</div>


<div >

<div >

<div >

<strong>Name:</strong>

{{ $product->name }}

</div>

</div>

<div >

<div >

<strong>Details:</strong>

{{ $product->detail }}

</div>

</div>

</div>

@endsection

resources/views/products/create.blade.php



@extends('products.layout')


@section('content')

<div >

<div >

<div >

<h2>Add New Product</h2>

</div>

<div >

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

</div>

</div>

</div>


@if ($errors->any())

<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 action="{{ route('products.store') }}" method="POST">

@csrf


<div >

<div >

<div >

<strong>Name:</strong>

<input type="text" name="name" placeholder="Name">

</div>

</div>

<div >

<div >

<strong>Detail:</strong>

<textarea style="height:150px" name="detail" placeholder="Detail"></textarea>

</div>

</div>

<div >

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

</div>

</div>


</form>


@endsection

resources/views/products/edit.blade.php



@extends('products.layout')


@section('content')

<div >

<div >

<div >

<h2>Edit Product</h2>

</div>

<div >

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

</div>

</div>

</div>


@if ($errors->any())

<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 action="{{ route('products.update',$product->id) }}" method="POST">

@csrf

@method('PUT')


<div >

<div >

<div >

<strong>Name:</strong>

<input type="text" name="name" value="{{ $product->name }}" placeholder="Name">

</div>

</div>

<div >

<div >

<strong>Detail:</strong>

<textarea style="height:150px" name="detail" placeholder="Detail">{{ $product->detail }}</textarea>

</div>

</div>

<div >

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

</div>

</div>


</form>


@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/products

I hope it can help you…

Hope this code and post will helped you for implement Laravel 5.6 CRUD Application for Starter. 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 *

  +  49  =  58

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