Laravel-8-Crud

by Saimon Akram

laravel-8-crud

<?php

//This is crudCrontoller.php from controller folder//

namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Models\Crud;

use Session;


class CrudController extends Controller

{

    public function showData(){

       

        $showData = Crud::simplepaginate(5);

        return view('show_data',compact('showData'));

    }


    public function addData(){

        return view('add_data');

    }

    public function storeData(Request $request){

        $rules=[

            'name'=> 'required|max:15',

            'email'=> 'required|email',

        ];

        $cm=[

            'name.required'=>'Enter your name',

            'name.max'=>'your name must be at least 15 characters',

            'email.required'=>'Enter Your Email',

            'email.email'=>'Your Email must be a valid email',

        ];


        $this->validate($request,$rules,$cm);

       

        $crud=new Crud();

        $crud->name=$request->name;

        $crud->email=$request->email;

        $crud->save();

        Session::flash('msg','successfully Data added');


        return redirect('/');

       

    }

    public function editData($id=null)

    {

        $editData= Crud::find($id);

        return view('edit_data',compact('editData'));

    }

    public function updateData(Request $request,$id){

        $rules=[

            'name'=> 'required|max:15',

            'email'=> 'required|email',

        ];

        $cm=[

            'name.required'=>'Enter your name',

            'name.max'=>'your name must be at least 15 characters',

            'email.required'=>'Enter Your Email',

            'email.email'=>'Your Email must be a valid email',

        ];


        $this->validate($request,$rules,$cm);

       

        $crud=Crud::find($id);

        $crud->name=$request->name;

        $crud->email=$request->email;

        $crud->save();

        Session::flash('msg','successfully Data Updated');


        return redirect('/');

       

    }

    public function deleteData($id=null){

        $deleteData= Crud::find($id);

        $deleteData->delete();

        Session::flash('msg','successfully Data deleted');


        return redirect('/');


    }

}


//This is Model Crud//


<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;


class Crud extends Model

{

    use HasFactory;

}



//view folder//

//add_data.blade.php//



<!doctype html>

<html lang="en">


<head>

    <!-- Required meta tags -->

    <meta charset="utf-8">

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


    <!-- Bootstrap CSS -->

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"

        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


    <title>Laravel Crud</title>

</head>


<body>

    <div class="container">

        <a class="btn btn-primary my-3" href="{{url('/')}}">Show Data</a>


        <form action="{{url('/store-data')}}" method="post">

            @csrf

            <div class="form-control">

                <label for="">Name</label>

                <input type="text" class="form-control" name="name" placeholder="Enter your name">

                @error('name')

                <span class="text-danger">{{$message}}</span>

                @enderror


            </div>

            <div class="form-control">

                <label for="">email</label>

                <input type="text" class="form-control" name="email" placeholder="Enter your email">

                @error('email')

                <span class="text-danger">{{$message}}</span>

                @enderror

            </div>

            <input type="submit" class="btn btn-primary my-3" value="Submit">

        </form>


    </div>


    <!-- Option 1: Bootstrap Bundle with Popper -->

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"

        integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous">

    </script>




</body>


</html>




//edit_data.blade.php//


<!doctype html>

<html lang="en">


<head>

    <!-- Required meta tags -->

    <meta charset="utf-8">

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


    <!-- Bootstrap CSS -->

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"

        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


    <title>Laravel Crud</title>

</head>


<body>

    <div class="container">

        <a class="btn btn-primary my-3" href="{{url('/')}}">Show Data</a>


        <form action="{{url('/update-data/'.$editData->id)}}" method="post">

            @csrf

            <div class="form-control">

                <label for="">Name</label>

                <input type="text" class="form-control" name="name" value="{{$editData->name}}" placeholder="Enter your name">

                @error('name')

                <span class="text-danger">{{$message}}</span>

                @enderror


            </div>

            <div class="form-control">

                <label for="">email</label>

                <input type="text" class="form-control" name="email" value="{{$editData->email}}" placeholder="Enter your email">

                @error('email')

                <span class="text-danger">{{$message}}</span>

                @enderror

            </div>

            <input type="submit" class="btn btn-primary my-3" value="Submit">

        </form>


    </div>


    <!-- Option 1: Bootstrap Bundle with Popper -->

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"

        integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous">

    </script>




</body>


</html>


//show_data.blade.php//



<!doctype html>

<html lang="en">


<head>

    <!-- Required meta tags -->

    <meta charset="utf-8">

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


    <!-- Bootstrap CSS -->

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"

        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


    <title>Laravel Crud</title>

</head>


<body>

    <div class="container">


        <a class="btn btn-primary my-3" href="{{url('/add-data')}}">Add Data</a>

        @if(Session::has('msg'))

        <p class="alert alert-success my-3">{{Session::get('msg')}}</p>

        @endif

        <table class="table table-bordered">

            <thead>

                <tr>

                    <th scope="col">ID</th>

                    <th scope="col">NAME</th>

                    <th scope="col">EMAIL</th>

                    <th scope="col">Action</th>


                </tr>

            </thead>

            <tbody>

                @foreach($showData as $key => $data)

                <tr>


                    <td>{{$key+1}}</td>

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

                    <td>{{$data->email}}</td>

                    <td>

                        <a href="{{url('/edit-data/'.$data->id)}}" class="btn btn-primary">Edit</a>

                        <a href="{{url('/delete-data/'.$data->id)}}" onclick="return confirm('Are you sure you want to delete this')"class="btn btn-danger">Delete</a>

                    </td>


                </tr>

                @endforeach



            </tbody>

        </table>

        {{$showData->links()}}

    </div>


    <!-- Option 1: Bootstrap Bundle with Popper -->

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"

        integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous">

    </script>




</body>


</html>


//web.php from routes folder//


<?php


use Illuminate\Support\Facades\Route;

use App\Http\Controllers\CrudController;




// Route::get('/', function () {

//     return view('welcome');

// });

Route::get('/', [CrudController::class,'showData']);

Route::get('/add-data', [CrudController::class,'addData']);

Route::post('/store-data', [CrudController::class,'storeData']);

Route::get('/edit-data/{id}', [CrudController::class,'editData']);

Route::post('/update-data/{id}', [CrudController::class,'updateData']);

Route::get('/delete-data/{id}', [CrudController::class,'deleteData']);


Leave a Reply