Laravel
271
w3alert.com
09-01-2020
In the previous tutorial, you have learned how to insert and retrieve/fetch data into a database table in laravel web application.
In this update data into database table laravel tutorial. You will learn how to update data into a database table in laravel web applications.
Now, open your project in a text editor and go to routes/web.php file and update the following routes into your file:
Route::get('edit/{id}','[email protected]'); Route::post('update-record','[email protected]');
Next step, you need to create two method in the controller for update data from the database table.
So update the below methods in your PostController.php:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response; use App\Post; class PostController extends Controller { public function edit(Request $request, $id) { $data['post'] = Post::where('id', $id)->first(); if(!$data['post']){ return redirect('/list'); } return view('edit', $data); } public function updateRecord(Request $request) { $data = request()->validate([ 'title' => 'required', 'description' => 'required', ]); if(!$request->id){ return redirect('/list'); } $check = Post::where('id', $request->id)->update($data); return Redirect::to("list")->withSuccess('Great! Form data successfully updated'); } }
Next step, you need to create blade view file, so go resource/views and create a new file name edit.blade.php and update the following code into your file:
<html> <head> <title>Laravel Update Form Data Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="csrf-token" content="{{ csrf_token() }}"> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container mt-5"> <div class="row"> <div class="col-md-12"> <form action="{{ url('update-record') }}" method="post" accept-charset="utf-8">@csrf <input type="hidden" class="form-control" name="id" value="{{ $post->id }}"> <div class="form-group"> <label class="control-label col-sm-2" for="fname">Title</label> <div class="col-sm-10"> <input type="text" class="form-control" placeholder="Enter Title" name="title" value="{{ $post->title }}"> <span class="text-danger">{{ $errors->first('title') }}</span> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="comment">Description</label> <div class="col-sm-10"> <textarea class="form-control" rows="5" name="description">{{ $post->description }}</textarea> <span class="text-danger">{{ $errors->first('description') }}</span> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-success">Submit</button> </div> </div> </form> </div> </div> </div> </body> </html>
You can use the following command artisan command to start the development server:
php artisan serve
The final step, go to your browser and type the below-given URL into your browser:
http://localhost:8000/list OR http://localhost/project-name/public/list
This tutorial idea has taken from the following urls: