Laravel Simple CRUD Application Script

Today, We want to share with you Laravel Simple CRUD Application Script.In this post we will show you Laravel 5.7 CRUD Tutorial Example Step By Step From Scratch, hear for Laravel 5.7 CRUD Example Tutorial For Beginners From Scratch we will give you demo and example for implement.In this post, we will learn about Laravel CRUD Operations Tutorial step by step in version 5.7 with an example.

Laravel Simple CRUD Application Script

There are the Following The simple About Laravel Simple CRUD Application Script Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 5.7 – simple crud operation with example, so the Laravel simple crud with jquery ajax for this example is following below.

Step 1. Make a new project and database

create a new Laravel Web project using the below PHP artisan command

laravel new laraCrud

Setup the .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=superproduct
DB_USERNAME=root
DB_PASSWORD=

database->migrations->2019_09_create_products_table.php

increments('id');
            $table->string('name');
            $table->text('description');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

run Simple PHP artisan Migrate

php artisan migrate

Step 2. Creating a Laravel model and Controller

create model and controller class

php artisan make:controller productsController -r -m product

Simple Output Like

php artisan make:controller productsController -r -m product
 
 A App\product model does not exist. Do you want to generate it? (yes/no) [yes]:
 > yes
 
Model created successfully.
Controller created successfully.

app->product.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
 
class product extends Model
{
    //
    public $table = "products";
}

app->http->controllers->productsController.php

name = request('name');
        $product->description = request('description');
 
        $product->save();
 
        return redirect('/products');
    }
 
    /**
     * Display the specified resource.
     *
     * @param  \App\product  $product
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $product = Product::findOrFail($id);
        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($id)
    {
        //
        $products = Product::findOrFail($id);
         return view('products.edit',compact('products')); 
    }
 
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\product  $product
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        //dd('grttt');
       
        $product = Product::findOrFail($id);
 
        $product->name = request('name');
        $product->description = request('description');
 
        $product->save();
 
        return redirect('/products');
    }
 
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\product  $product
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       // dd('dele');
       Product::findOrFail($id)->delete();
       return redirect('/products');
    }
}

Step 3. Define a Laravel routes

routes->web.php

<?php

Route::get('/', function () {
    return view('welcome');
});
 
 
Route::resource('products','productsController');

checking all these Laravel routes

php artisan route:list

below output Like this

+--------+----------+----------+------+---------+--------------+
| Domain | Method   | URI      | Name | Action  | Middleware   |
+--------+----------+----------+------+---------+--------------+
|        | GET|HEAD | /        |      | Closure | web          |
|        | GET|HEAD | api/user |      | Closure | api,auth:api |
+--------+----------+----------+------+---------+--------------+
➜  laraCrud php artisan route:list
+--------+-----------+-------------------+---------------+----------------------------------------------+--------------+
| Domain | Method    | URI               | Name          | Action                                       | Middleware   |
+--------+-----------+-------------------+---------------+----------------------------------------------+--------------+
|        | GET|HEAD  | /                 |               | Closure                                      | web          |
|        | GET|HEAD  | api/user          |               | Closure                                      | api,auth:api |
|        | GET|HEAD  | products             | products.index   | App\Http\Controllers\[email protected]   | web          |
|        | POST      | products             | products.store   | App\Http\Controllers\[email protected]   | web          |
|        | GET|HEAD  | products/create      | products.create  | App\Http\Controllers\[email protected]  | web          |
|        | GET|HEAD  | products/{product}      | products.show    | App\Http\Controllers\[email protected]    | web          |
|        | PUT|PATCH | products/{product}      | products.update  | App\Http\Controllers\[email protected]  | web          |
|        | DELETE    | products/{product}      | products.destroy | App\Http\Controllers\[email protected] | web          |
|        | GET|HEAD  | products/{product}/edit | products.edit    | App\Http\Controllers\[email protected]    | web          |
+--------+-----------+-------------------+---------------+----------------------------------------------+--------------+

Step 4. creating a Laravel Blade View files

index() methods in Controller

 public function index()
{
    $products = Product::all();

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

resources->views->index.blade.php file file

@extends('layout')
 
@section('content')
 
</


   


 
<h1 class="title">All Information About Super product</h1>
 
<p>
    <a href="/products/create"> Create a new product </a>
</p>
 
@foreach ($products as $product)
<li> 
 
    <a>id }}">
 
        {{ $product->name}}  
 
    </a>
 
</li>
@endforeach
 


 
@endsection

resources->views->layout.blade.php

</


<title>Laravel 5.7 CRUD (Create Read Update Delete) Example from scratch</title>
      


 
    <div class="container">
        @yield('content')
    </div>    
 

Creating a new product

Laravel create() method in Controller

public function create()
{
    //dd('1');
    return view('products.create');
}

resources->views->products->create.blade.php

</


 


body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}
 
input[type=text], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
}
 
input[type=submit] {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
 
input[type=submit]:hover {
  background-color: #45a049;
}
 
.container {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}



<h1>Create New Super product</h1>
<div class="container">
    <form method="POST" action="/products">
 
        {{ csrf_field() }}
 
       <div>
          <label>product Name</label>
          
      </div>
      <div>
            <label>product description</label>
            <textarea name="description"></textarea>
      </div>
      <div>
            
      </div>
    </form>  
 </div>   


Laravel store() function from controller class

 public function store(Request $request)
{
    $product = new product();

    $product->name = request('name');
    $product->description = request('description');

    $product->save();

    return redirect('/products');
}

Laravel Edit and Delete Database Records

Laravel show() function from controller file

public function show($id)
{
    $product = Product::findOrFail($id);
    return view('products.show',compact('product')); 
}

resources->views->products->show.blade.php

@extends('layout')
 
@section('content')
<h1 class="title"> Below is the name of super product </h1>
<h1 class="title"> {{ $product->name }} </h1>
<h1 class="title"> {{ $product->description }} </h1>
<p>
    <a>id }}/edit"> Edit product </a>
</p>
 
@endsection

edit() function from controller.

public function edit($id)
{
    $products = Product::findOrFail($id);
     return view('products.edit',compact('products')); 
}

resources->views->products->edit.blade.php

@extends('layout')
 
@section('content')
 
    <h1 class="title"> Edit products </h1>
 
    <form method="POST">id }}">
 
    @method('PATCH')
    @csrf
 
 
        <div class="field">
 
            <label class="lable" for="name">Name </label>
 
            <div class="control">
 
                name }}" required>
        
            </div>
       
        </div>
 
        <div class="field">
 
                <label class="lable" for="description">description </label>
 
                <div class="control">
 
                      <textarea class="textarea" name="description">{{ $products->description }} </textarea>
 
                </div>
 
        </div>
 
        <div class="field">
 
            <div class="control">
 
                <button type="submit" class="button is-link">Update product</button>
 
            </div>
 
        </div>
 
    </form>  
 
    <form method="POST">id }}">
 
    @method('DELETE')
    @csrf
 
 
        <div class="field">
 
            <div class="control">
 
                <button type="submit" class="button is-link">Delete product</button>
 
            </div>
 
        </div>         
 
    </form>
 
 
 @endsection

Update product and Delete product

public function update($id)
{
    //dd('grttt');
   
    $product = Product::findOrFail($id);

    $product->name = request('name');
    $product->description = request('description');

    $product->save();

    return redirect('/products');
}

Delete product button, compiler will run destroy() function in Controller

Also Read This πŸ‘‰   CRUD Operations in Laravel 7 PHP Framework

public function destroy($id)
    {
       // dd('dele');
       Product::findOrFail($id)->delete();
       return redirect('/products');
    }

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Simple CRUD Application Script.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.