Posted inProgramming / JavaScript / jQuery / Laravel / Mysql / Mysqli

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\productsController@index   | web          |
|        | POST      | products             | products.store   | App\Http\Controllers\productsController@store   | web          |
|        | GET|HEAD  | products/create      | products.create  | App\Http\Controllers\productsController@create  | web          |
|        | GET|HEAD  | products/{product}      | products.show    | App\Http\Controllers\productsController@show    | web          |
|        | PUT|PATCH | products/{product}      | products.update  | App\Http\Controllers\productsController@update  | web          |
|        | DELETE    | products/{product}      | products.destroy | App\Http\Controllers\productsController@destroy | web          |
|        | GET|HEAD  | products/{product}/edit | products.edit    | App\Http\Controllers\productsController@edit    | 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')
 
</


   


 

All Information About Super product

Create a new product

@foreach ($products as $product)
  • id }}"> {{ $product->name}}
  • @endforeach @endsection

    resources->views->layout.blade.php

    </
    
    
    Laravel 5.7 CRUD (Create Read Update Delete) Example from scratch
          
    
    
     
        
    @yield('content')

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

    Create New Super product

    {{ csrf_field() }}

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

    Below is the name of super product

    {{ $product->name }}

    {{ $product->description }}

    id }}/edit"> Edit product

    @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')
     
        

    Edit products

    id }}"> @method('PATCH') @csrf
    name }}" required>
    id }}"> @method('DELETE') @csrf
    @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

    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.

    I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype