first of all we need to get fresh Laravel 5.8 version application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
In second step, we will make database configuration for example database name, username, password etc for our crud application of laravel 5.8. 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)we are going to create crud application for product. so we have to create migration for products table using Laravel 5.8 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 Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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');
}
}
Now you have to run this migration by following command:
php artisan migrate
Here, 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 Controller and Model
In this step, 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 add index, create, function in ProductController.php file.
app/Http/Controllers/ProductController.php
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return view('products.index',compact('products'));
}
}
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 Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'name', 'detail'
];
}
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 create.blade.php
4 edit.blade.php
5 show.blade.php
So let's just create following file and put bellow code.
resources/views/products/layout.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Laravel 5.8</title>
<link rel="stylesheet" href="{{asset('css/bulma.css')}}">
</head>
<body>
<nav class="navbar is-primary">
<div class="navbar-brand">
<a href="" class="navbar-item">iMovie</a>
</div>
<div class="navbar-menu">
<div class="navbar-start">
</div>
<div class="navbar-end">
<a href="{{url('/')}}" class="navbar-item">
Home
</a>
<a href="{{url('/products')}}" class="navbar-item">
Product
</a>
<a href="{{url('/about')}}" class="navbar-item">
About
</a>
</div>
</div>
</nav>
@yield('content')
</body>
</html>
next resources/views/products/index.blade.php
@extends('layouts.master')
@section('content')
<div class="columns is-marginless">
<div class="column">
<a href="{{route('products.create')}}" class="button is-info is-pulled-right">Create New Product</a>
</div>
</div>
<table class="table is-fullwidth">
<thead>
<tr>
<th>Product Name</th>
<th>Product Detail</th>
<th>Action</th>
</tr>
</thead>
@foreach ($products as $product)
<tr>
<td > {{$product->name}}</td>
<td >{{$product->detail}}</td>
<td >
<form action="{{ route('products.destroy',$product->id) }}" method="POST">
<a class="button is-info" href="{{ route('products.show',$product->id) }}">Show</a>
<a class="button is-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="button is-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
@endsection
and resources/views/products/create.blade.php
@extends('layouts.master')
@section('content')
<form action="{{ route('products.store') }}" method="POST">
@csrf
<div class="field">
<label for="name" class="label">Name: </label>
<div class="control">
<input type="text" class="input" placeholder="Product Name" name="name">
</div>
</div>
<div class="field">
<label for="detail" class="label">Detail: </label>
<div class="control">
<textarea class="textarea" placeholder="Product Detail" name="detail"></textarea>
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button type="submit" class="button is-link">Submit</button>
</div>
<div class="control">
<button class="button is-text">Cancel</button>
</div>
</div>
</form>
@endsection
and resources/views/products/edit.blade.php
@extends('layouts.master')
@section('content')
<div class="columns is-marginless">
<div class="column">
<span class="has-text-info is-pulled-left"> Edit Product</span>
<a href="{{route('products.index')}}" class="button is-info is-pulled-right">Back</a>
</div>
</div>
<form action="{{ route('products.update', $product->id) }}" method="POST">
@csrf
@method('PUT')
<div class="field">
<label for="name" class="label">Name: </label>
<div class="control">
<input type="text" class="input" placeholder="Product Name" name="name" value="{{$product->name}}">
</div>
</div>
<div class="field">
<label for="detail" class="label">Detail: </label>
<div class="control">
<textarea class="textarea" placeholder="Product Detail" name="detail" >{{$product->detail}}</textarea>
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button type="submit" class="button is-link">Submit</button>
</div>
<div class="control">
<button class="button is-text">Cancel</button>
</div>
</div>
</form>
@endsection
and resources/views/products/show.blade.php
@extends('layouts.master')
@section('content')
<div class="notification ">
<div class="columns is-fullwidth">
<div class="column is-pulled-left">
<h2>Show Product</h2>
</div>
<div class="is-pulled-right">
<a href="{{route('products.index')}}" class="button is-primary">Back</a>
</div>
</div>
<div class="columns is-bordered">
<div class="column is-one-fifth">
Name:
</div>
<div class="column">
Detail
</div>
</div>
<div class="columns is-bordered">
<div class="column is-one-fifth">
{{$product->name}}
</div>
<div class="column">
{{$product->detail}}
</div>
</div>
</div>
@endsection
Now we are ready to run our crud application example with laravel 5.8 so run bellow command for quick run:
php artisan serve