Create a Model Product: 


 Schema::create('products', function (Blueprint $table) {

            $table->id();

            $table->string('name');

            $table->string('description');

            $table->string('image');

            $table->timestamps();

        });


Update Route File Name As : Route/web.php


Route::get('/products/productwithajax', [ProductController::class,'productwithajax']);

Route::post('/products/storewithajax', [ProductController::class,'storewithajax']);


Make a Blade File Name As : productwithajax.blade.php


@extends('layout.app')

@section('main')

<h2>Create Product With Ajax</h2>

<form id="fileupload">

  @csrf

  <label>Product Name</label>

  <input type="text" name="name" value="{{old('name')}}" /><br><br>

  <label>Product Description</label>

  <input type="text" name="description" value="{{old('description')}}" /><br><br>

  <label>Product Image</label>

  <input type="file" name="image" value="{{old('image')}}" /><br><br>

  <input type="submit" value="submit" name="submit">

</form>

<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>

<script>

  $(document).ready(function() {

      $("#fileupload").submit(function(e){

          e.preventDefault();

          var formdata = new FormData(this);

          $.ajax({

              type: "POST",

              url:'/products/storewithajax',

              data:formdata,

              contentType: false,

              processData: false,

              success: (response)=>{

                  // this.reset();

                  alert(response);

                  console.log(response);

  

                  alert("File Upload Success");

              },

              error:function(response){

                  alert(response);

                  console.log(response);

              }

          });

      });

  });

  

</script>

@endsection


Make a Controller Name As : ProductController.php


<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Models\Product;

class ProductController extends Controller

{

    //

    public function index()

    {

        // return view('products.index',['products' => Product::all()]);

        // return view('products.index',['products' => Product::get()]);

        // return view('products.index',['products' => Product::latest()->get()]);

        return view("products.index", [

            "products" => Product::latest()->paginate(5),

        ]);

    }

    public function create()

    {

        return view("products.create");

    }


    public function store(Request $request)

    {

        // dd($request->all());


        $request->validate([

            "name" => "required",

            "description" => "required",

            "image" => "required|mimes:jpef,jpg,png,gif|max:10000",

        ]);


        $imagename = time() . "." . $request->image->extension();

        $request->image->move(public_path("product"), $imagename);


        $product = new Product();


        $product->image = $imagename;

        $product->name = $request->name;

        $product->description = $request->description;

        $product->save();


        return back()->withSuccess("Product Created Successfully");


        // dd($imagename);

    }


    public function edit($id)

    {

        $product = Product::where("id", $id)->first();

        //    $product  = Product::find($id);

        //    dd($product);

        return view("products.edit", ["product" => $product]);

    }


    public function update(Request $request, $id)

    {

        // dd($request->all());

        $product = Product::where("id", $id)->first();

        $request->validate([

            "name" => "required",

            "description" => "required",

            "image" => "nullable|mimes:jpef,jpg,png,gif|max:10000",

        ]);


        if (isset($request->image)) {

            $imagename = time() . "." . $request->image->extension();

            $request->image->move(public_path("product"), $imagename);

            $product->image = $imagename;

        }


        $product->name = $request->name;

        $product->description = $request->description;

        $product->save();


        return back()->withSuccess("Product updated Successfully");

    }


    public function destroy($id)

    {

        $product = Product::where("id", $id)->first();

        $product->delete();

        return back()->withSuccess("Product Delete Successfully");

    }

    public function show($id)

    {

        $product = Product::where("id", $id)->first();

        return view("products.show", ["product" => $product]);

    }


    public function productwithajax()

    {

        return view("products.productwithajax");

    }


    public function storewithajax(Request $request)

    {

        $request->validate([

            "name" => "required",

            "description" => "required",

            "image" => "required|mimes:jpef,jpg,png,gif|max:10000",

        ]);


        $imagename = time() . "." . $request->image->extension();

        $request->image->move(public_path("product"), $imagename);


        $product = new Product();


        $product->image = $imagename;

        $product->name = $request->name;

        $product->description = $request->description;

        $product->save();

        return response()->json("success");

    }

}


WITH NORMAL CRUD USING PHP POST METHOD 


Create a file name as :create.blade.php


@extends('layout.app')

@section('main')


    <form action="/products/store" method="post" enctype="multipart/form-data">

        @csrf

        <label>Product Name</label>

        <input type="text" name="name" value="{{old('name')}}" /><br><br>

        @if($errors->has('name'))

        <p style="color:red">{{$errors->first('name')}}</p>

        @endif

        <label>Product Description</label>

        <input type="text" name="description" value="{{old('description')}}" /><br><br>

        @if($errors->has('description'))

        <p style="color:red">{{$errors->first('description')}}</p>

        @endif

        <label>Product Image</label>

        <input type="file" name="image" value="{{old('image')}}" /><br><br>

        @if($errors->has('image'))

        <p style="color:red">{{$errors->first('image')}}</p>

        @endif

        <input type="submit" value="submit" name="submit">

    </form>

    @endsection


show data in balde file :index.blade.php


@extends('layout.app')

@section('main')

<ul>

  <li>Products</li>

  <li> <a href="products/create">Create Products</a> </li>

  <table border="1">

    <thead>

      <tr>

        <th>Sr. No</th>

        <th>Name</th>

        <th>Dsecriptions</th>

        <th>Action</th>

        <th>Delete</th>

        <th>Delete with method</th>

      </tr>

    </thead>

    <tbody>

      @foreach($products as $product)

      <tr>

        <td>{{$loop->index+1}}</td>

        <td><a href="products/{{$product->id}}/show">{{$product->name}}</a></td>

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

        <td><img src="product/{{$product->image}}" width="40" height="40" alt=""></td>

        <td><a href="products/{{$product->id}}/edit">Edit</a></td>

        <td><a href="products/{{$product->id}}/delete">Delete</a></td>

        <td>

          <form action="products/{{$product->id}}/delete" method="post">

            @csrf

            @method('DELETE')

            <button type="submit">Delete</button>

          </form>

        </td>

      </tr>

      @endforeach

    </tbody>

  </table>

  <p>  {{$products->links()}}</p>

</ul>

@endsection


Edit or Update Product Code : edit.blade.php


@extends('layout.app')

@section('main')


    <form action="/products/{{$product->id}}/update" method="post" enctype="multipart/form-data">

        @csrf

        @method('PUT')

        <label>Product Name</label>

        <input type="text" name="name" value="{{old('name',$product->name)}}" /><br><br>

        @if($errors->has('name'))

        <p style="color:red">{{$errors->first('name')}}</p>

        @endif

        <label>Product Description</label>

        <input type="text" name="description" value="{{old('description',$product->description)}}" /><br><br>

        @if($errors->has('description'))

        <p style="color:red">{{$errors->first('description')}}</p>

        @endif

        <label>Product Image</label>

        <input type="file" name="image" value="{{old('image')}}" /><br><br>

        @if($errors->has('image'))

        <p style="color:red">{{$errors->first('image')}}</p>

        @endif

        <input type="submit" value="submit" name="submit">

    </form>

    @endsection


Seprate header or footer / Break Layout in laravel using this: 


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Laravel Crud</title>

</head>

<body>

<a href="/products/productwithajax">createproductwithajax</a>

<h1>Home</h1>

@if($message = Session::get('success'))

    @endif

    <p style="color:green">{{$message}}</p>

    <hr>

    @yield('main')

    </body>

</html>


Important Command : 


  •  php artisan --version
  •  php artisan serve
  •  php artisan make:model Product -m
  •  php artisan migrate:fresh
  •  php artisan make:controller ProductController
  •  php artisan optimize