Learning Objectives: Insert data into database using Model, Controller in Laravel. This day includes understanding of Edit and Delete Operations.
You need the following:
- Model
- Controller
- View
- Routes
- Database Table Creation (direct or using Migration)
Model
php artisan make:model Todo -mcr
Once you execute the above command you will find the Model File, Migration file, and Resource Controller file, Now we need to change these files one by one.
Migration file
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTodosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('todos', function (Blueprint $table) { $table->id(); $table->string('tasktitle'); $table->string('taskdesc'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('todos'); } }
Model (No Changes required as of now)
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Todo extends Model { use HasFactory; }
Controller file
<?php namespace App\Http\Controllers; use App\Models\Todo; use Illuminate\Http\Request; class TodoController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $p = Todo::all(); return view('todo',["mydata" => $p]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('todoadd'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { //echo $request->title; //echo $request->description; $t = new Todo; $t->tasktitle = $request->title; $t->taskdesc = $request->description; $t->save(); return redirect('/todo'); } /** * Display the specified resource. * * @param \App\Models\Todo $todo * @return \Illuminate\Http\Response */ public function show(Todo $todo) { // } /** * Show the form for editing the specified resource. * * @param \App\Models\Todo $todo * @return \Illuminate\Http\Response */ public function edit(Todo $todo) { // return view('todoedit',["todo"=>$todo]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Models\Todo $todo * @return \Illuminate\Http\Response */ public function update(Request $request, Todo $todo) { // $todo->tasktitle = $request->title; $todo->taskdesc = $request->description; $todo->save(); return redirect('/todo'); } /** * Remove the specified resource from storage. * * @param \App\Models\Todo $todo * @return \Illuminate\Http\Response */ public function destroy(Todo $todo) { //echo "Hello"; //echo $todo->id; $todo->delete(); return redirect()->route('todo.index'); } }
Routes
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\TodoController; Route::resource('todo', TodoController::class);
todo.blade.php
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <br> <br> <br> <h2>Todo List <a href="{{ route('todo.create') }}" class="btn btn-info" role="button">Add New</a></h2> <p> </p> <table class="table"> <thead> <tr> <th> ID </th> <th> Title </th> <th> Desc </th> <th> edit </th> <th> Delete </th> </tr> </thead> <tbody> @if(count($mydata)>0) @foreach ($mydata as $user) <tr> <td> {{ $user->id }} </td><td> {{ $user->tasktitle }} </td><td> {{ $user->taskdesc }} </td><td> <a href="{{ route('todo.edit', $user->id) }}" class="btn btn-primary btn-sm">Edit</a> </td> <td> <form action="{{ route('todo.destroy', $user->id) }}" method="post"> @csrf @method('DELETE') <button type="submit" class="btn btn-danger btn-sm">Delete</button> </form> </td> </tr> @endforeach @else {{ "<tr><td>No Data Found</td><td></td><td></td><td></td><td></td></tr>" }} @endif </tbody> </table> </div> </body> </html>
todoadd.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Form Example Tutorial</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
@if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<div class="card">
<div class="card-header text-center font-weight-bold">
Laravel 8 - Add Blog Post Form Example
</div>
<div class="card-body">
<form name="add-blog-post-form" id="add-blog-post-form" method="post" action="{{url('todo')}}">
@csrf
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" id="title" name="title" class="form-control" required="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Description</label>
<textarea name="description" class="form-control" required=""></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>
todoedit.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Form Example Tutorial</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
@if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<div class="card">
<div class="card-header text-center font-weight-bold">
Laravel 8 - Add Blog Post Form Example
</div>
<div class="card-body">
<form action="{{ url('todo/'.$todo->id) }}" method="POST">
@csrf
@method('PUT')
<div class="form-group mb-3">
<label for="">Title</label>
<input type="text" name="title" value="{{$todo->tasktitle}}" class="form-control">
</div>
<div class="form-group mb-3">
<label for="">Description</label>
<input type="text" name="description" value="{{$todo->taskdesc}}" class="form-control">
</div>
<div class="form-group mb-3">
<button type="submit" class="btn btn-primary">Update Student</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>