How to edit-Update & Delete in Laravel

StudentController.php

<?php

namespace App\Http\Controllers;

use App\Models\Student;
use Illuminate\Http\Request;

class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $students = Student::all();
      return view('students/student', compact('students'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $students = new Student();
       $students->name = $request->name;
        $students->reg_no = $request->reg;
        $students->cnic = $request->cnic;
        $students->save();
        return back();

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
      $student = Student::findOrFail($id);
      return view('students/update', compact('student'));


    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $students = Student::findOrFail($id);
        $students->name = $request->name;
        $students->reg_no = $request->reg;
        $students->cnic = $request->cnic;
        $students->update();
        return redirect('/student');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
      Student::where('id','=',$id)->delete();
      return back();
    }
}

Stuent.blade.php

@extends('layout.app')
@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <form action="{{route('student.store')}}"method="post">
                    @csrf
                    <br>
                    <input type="text" name="name" class="form-control" placeholder="Name">
                    <br>
                    <input type="text"class="form-control" name ="reg" placeholder="Reg No">
                    <br>
                    <input type="text"class="form-control" name ="cnic" placeholder="CNIC">
                    <br>
                    <button class="btn btn-primary" type="submit">save</button>
                    <button class="btn btn-danger" type="reset">reset</button>
                </form>
            </div>
        </div>
    </div>

    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <table class="table table-hover">
                    <tr>
                        <th>S.NO</th>
                        <th>Name</th>
                        <th>Reg No</th>
                        <th>CNIC</th>
                        <th>Action</th>
                    </tr>
                    <tbody>
                    @forelse($students as $Student)
                        <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$Student->name ?? 'N/A'}}</td>
                            <td>{{$Student->reg_no ?? 'N/A'}}</td>
                       <td>
                           <a href="{{route('student.edit', $Student->id)}}" class="btn btn-sm btn-primary"><i class="icon-pencil">Edit</i></a>
                           <a href="{{route('student.delete', $Student->id)}}" class="btn btn-sm btn-primary"><i class="icon-pencil">Delete</i></a>
                       </td>

                        </tr>
                    @empty
                        <td colspan="3">No data found</td>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection

web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;


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


Route::get('/student',[StudentController::class, 'index']);
Route::post('/store',[StudentController::class,'store'])->name('student.store');
Route::get('/edit/{id}',[StudentController::class,'edit'])->name(('student.edit'));
Route::post('/update/{id}',[StudentController::class,'update'])->name(('student.update'));
Route::get('/delete/{id}',[StudentController::class,'destroy'])->name(('student.delete'));

update.blade.php

@extends('layout.app')
@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <form action="{{route('student.update', $student->id)}}"method="post">
                    @csrf
                    <br>
                    <input type="text" name="name" value="{{$student->name}}" class="form-control" placeholder="Name">
                    <br>
                    <input type="text"class="form-control" name ="reg" value="{{$student->reg_no}}" placeholder="Reg No">
                    <br>
                    <input type="text"class="form-control" name ="cnic" value="{{$student->cnic}}" placeholder="CNIC">
                    <br>
                    <button class="btn btn-primary" type="submit">Update</button>
                    <button class="btn btn-danger" type="reset">reset</button>
                </form>
            </div>
        </div>
    </div>


@endsection

students_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('reg_no');
            $table->string('cnic');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}

Leave a Reply