A community of developers

Uncategorized

PHP Laravel 7 Tutorial by Example: Build Your CRUD App with MySQL and CSS Bootstrap

Throughout this tutorial, we will be learning how to build a PHP Laravel 7 CRUD app by example. We’ll be implementing our example step by step so you’ll be able to follow this tutorial even if you are a Laravel 7 beginner. We’ll particularly show you how to implement CRUD operations against a MySQL database connected to the Laravel 7 app. Laravel 7 is based on PHP so you are required to be familiar with this language as we are not going o explain the basic concepts of PHP.

So after this introduction, let’s get started with our first Laravel 7 CRUD tutorial.

Build your First CRUD App with PHP, MySQL and Laravel 7

Laravel 7 has been released in March 2020, with many new features. We are not going to cover these features as they are listed with details in the official website but we’ll see instead how to create a CRUD app that shows you how to implement the common create, read, update, and delete operations against a MySQL database.

We’ll see how to create the basic artifacts of a Laravel 7 app using controllers, models, routes, and blade templates. We’ll also be using CSS Bootstrap 4 for styling our CRUD application.

Let’s now see the steps!

Step 1 : Installing PHP Laravel 7

Let’s get started our tutorial by installing Laravel 7 in our development machine. Open a new command-line interface and run the following command to install the PHP framework using the composer package manager:

composer create-project --prefer-dist laravel/laravel laravel7-crud-example

Step 2: Configuring the MySQL Database

After installing Laravel 7 and creating a new project using Composer, we next need to configure a MySQL database.

Make sure you have the name, username, and password of your MySQL database that you have created earlier. Next, open the .env file and provide your database credentials:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=mydb

DB_USERNAME=root

DB_PASSWORD=root

You need to update the credentials as required.

Step 3: Creating a Database Migration

We’ll be making CRUD operations for reading, creating, updating and deleting products in a MySQL database. In Laravel (7), we can do that using migrations. Head back to your command-line interface and the following artisan command:

php artisan make:migration create_products_table –create=products

You’ll find the migration file inside the “database/migrations” folder. Open it and update is as follows:

<?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->id();

$table->string(‘name’);

$table->text(‘description’);

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists(‘products’);

}

}

Next, you’ll need to apply the migration to qctually create the corresponding MySQL table using the following artisan command:

php artisan migrate

Step 4: Adding a Laravel Resource Route

Now that we have created and applied our migration file. we can proceed to create a resource route for products.

Open the “routes/web.php” file and add a new resource route as follows:

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

Step 5: Adding a Laravel Controller and Model

Now, we need to generate and implement a new Laravel controller.

Head back to your command-line interface and run the following command:

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

Open the “app/Http/Controllers/ProductController.php” file and update it as follows:

In this controller will create seven methods by default as bellow methods:

<?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’,

‘desciption’ => ‘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’,

‘description’ => ‘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’);

}

}

Next, open the “app/Product.php” file and update it as follows:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model

{

protected $fillable = [

‘name’, ‘description’

];

}

Step 6: Creating the Blade Templates

In this step. create the folowing templates in the resources/views/products folder:

layout.blade.php

index.blade.php

create.blade.php

edit.blade.php

show.blade.php

Open the resources/views/products/layout.blade.php file and update it as follows:

<!DOCTYPE html>

<html>

<head>

<title>Laravel 7 CRUD Example </title>

<link href=”https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css” rel=”stylesheet”>

</head>

<body>

<div class=”container”>

@yield(‘content’)

</div>

</body>

</html>

Next, open the resources/views/products/index.blade.php file and update it as follows:

@extends(‘products.layout’)

@section(‘content’)

<div class=”row”>

<div class=”col-lg-12 margin-tb”>

<div class=”pull-left”>

<h2>Laravel 7 CRUD Example</h2>

</div>

<div class=”pull-right”>

<a class=”btn btn-success” href=”{{ route(‘products.create’) }}”> Create New Product</a>

</div>

</div>

</div>

@if ($message = Session::get(‘success’))

<div class=”alert alert-success”>

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

</div>

@endif

<table class=”table table-bordered”>

<tr>

<th>No</th>

<th>Name</th>

<th>Description</th>

<th width=”280px”>Actions</th>

</tr>

@foreach ($products as $product)

<tr>

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

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

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

<td>

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

<a class=”btn btn-info” href=”{{ route(‘products.show’,$product->id) }}”>Show</a>

<a class=”btn btn-primary” href=”{{ route(‘products.edit’,$product->id) }}”>Edit</a>

@csrf

@method(‘DELETE’)

<button type=”submit” class=”btn btn-danger”>Delete</button>

</form>

</td>

</tr>

@endforeach

</table>

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

@endsection

Next open the resources/views/products/create.blade.php file and update it as follows:

@extends(‘products.layout’)

@section(‘content’)

<div class=”row”>

<div class=”col-lg-12 margin-tb”>

<div class=”pull-left”>

<h2>Add New Product</h2>

</div>

<div class=”pull-right”>

<a class=”btn btn-primary” href=”{{ route(‘products.index’) }}”> Back</a>

</div>

</div>

</div>

@if ($errors->any())

<div class=”alert alert-danger”>

<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 class=”row”>

<div class=”col-xs-12 col-sm-12 col-md-12″>

<div class=”form-group”>

<strong>Name:</strong>

<input type=”text” name=”name” class=”form-control” placeholder=”Name”>

</div>

</div>

<div class=”col-xs-12 col-sm-12 col-md-12″>

<div class=”form-group”>

<strong>Detail:</strong>

<textarea class=”form-control” style=”height:150px” name=”detail” placeholder=”Detail”></textarea>

</div>

</div>

<div class=”col-xs-12 col-sm-12 col-md-12 text-center”>

<button type=”submit” class=”btn btn-primary”>Submit</button>

</div>

</div>

</form>

@endsection

Next open the resources/views/products/edit.blade.php file and update it as follows:

@extends(‘products.layout’)

@section(‘content’)

<div class=”row”>

<div class=”col-lg-12 margin-tb”>

<div class=”pull-left”>

<h2>Edit Product</h2>

</div>

<div class=”pull-right”>

<a class=”btn btn-primary” href=”{{ route(‘products.index’) }}”> Back</a>

</div>

</div>

</div>

@if ($errors->any())

<div class=”alert alert-danger”>

<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 class=”row”>

<div class=”col-xs-12 col-sm-12 col-md-12″>

<div class=”form-group”>

<strong>Name:</strong>

<input type=”text” name=”name” value=”{{ $product->name }}” class=”form-control” placeholder=”Name”>

</div>

</div>

<div class=”col-xs-12 col-sm-12 col-md-12″>

<div class=”form-group”>

<strong>Detail:</strong>

<textarea class=”form-control” style=”height:150px” name=”description” placeholder=”Detail”>{{ $product->description }}</textarea>

</div>

</div>

<div class=”col-xs-12 col-sm-12 col-md-12 text-center”>

<button type=”submit” class=”btn btn-primary”>Submit</button>

</div>

</div>

</form>

@endsection

Next open the resources/views/products/show.blade.php file and update it as follows:

@extends(‘products.layout’)

@section(‘content’)

<div class=”row”>

<div class=”col-lg-12 margin-tb”>

<div class=”pull-left”>

<h2> Show Product</h2>

</div>

<div class=”pull-right”>

<a class=”btn btn-primary” href=”{{ route(‘products.index’) }}”> Back</a>

</div>

</div>

</div>

<div class=”row”>

<div class=”col-xs-12 col-sm-12 col-md-12″>

<div class=”form-group”>

<strong>Name:</strong>

{{ $product->name }}

</div>

</div>

<div class=”col-xs-12 col-sm-12 col-md-12″>

<div class=”form-group”>

<strong>Description:</strong>

{{ $product->description }}

</div>

</div>

</div>

@endsection

Finally we can run our Laravel 7 application using the following artisan command:

php artisan serve

Leave a Reply

Skip to toolbar