Laravel 6.0

Laravel 6.0 CRUD

composer global require laravel/installer

composer create-project –prefer-dist laravel/laravel tutorial

cd tutorial

code .

Create new database  and add your database information inside .env file.

php artisan make:migration create_products_table –create=products

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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');
}
}

php artisan migrate

routes/web.php

Route::resource(‘products’,’ProductController’);

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

this path “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::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 \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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 \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return view('products.show',compact('product'));
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
return view('products.edit',compact('product'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
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 \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();

return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
}

tutorial/app/Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
protected $fillable = [
'name', 'detail'
];
}

tutorial/resources/views/products

can you create blade.php files

layout.blade.php

create.blade.php

edit.blade.php

index.blade.php

show.blade.php

tutorial/resources/views/products/layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel 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>

tutorial/resources/views/products/index.blade.php

@extends('products.layout')

@section('content')

Laravel 6 CRUD Example

Create New Product </div> </div> @if ($message=Session::get(‘success’))

{{$message}}

@endif <tableclass=”table table-bordered”> <tr> <th>No</th> <th>Name</th> <th>Details</th> <thwidth=”280px”>Action</th> </tr> @foreach ($productsas$product) <tr> <td>{{++$i}}</td> <td>{{$product->name}}</td> <td>{{$product->detail}}</td> <td> <formaction=”{{route(‘products.destroy’,$product->id) }}”method=”POST”> <aclass=”btn btn-info”href=”{{route(‘products.show’,$product->id) }}”>Show</a> <aclass=”btn btn-primary”href=”{{route(‘products.edit’,$product->id) }}”>Edit</a> @csrf @method(‘DELETE’) <buttontype=”submit”class=”btn btn-danger”>Delete</button> </form> </td> </tr> @endforeach </table> {!!$products->links() !!} @endsection

Laravel 6 CRUD Example

Create New Product tutorial/resources/views/products/create.blade.php  

@extends('products.layout')

@section('content')

Laravel 6 CRUD Example

tutorial/resources/views/products/edit.blade.php

@extends('products.layout')

@section('content')

Edit Product

Back </div> </div> @if ($errors->any()) Whoops! There were some problems with your input.

  • {{ $error }}

@endif <form action=”{{ route(‘products.update’,$product->id) }}” method=”POST”> @csrf @method(‘PUT’) Name: name }}” class=”form-control” placeholder=”Name”> </div> Detail: {{ $product->detail }} </div> Submit </div> </form> @endsection  

Yorum bırakın