How To Update Data For Existing Table Using Model In Laravel

admin_img Posted By Bajarangi soft , Posted On 06-09-2020

The save method may also be used to update models that already exist in the database. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method

How To Update Data For Existing Table Using Model In Laravel

Once you have created a model and its associated database table, Now we need edit the added data in existing table using model name. 
To learn more about creating model and inserting data visit our Bajarangisoft site.


Updates
 

The save method may also be used to update models that already exist in the database. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method. Again, the updated_at timestamp will automatically be updated, so there is no need to manually set its value:

Example(1)

1.create edit.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Update Data For Existing Table Using Model In Laravel</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">
</head>
<body>

<div class="container">
    <div class="text-center">
        <h1>Edit & Update Data For Existing Table Using Model In Laravel</h1>
    </div>
 
    <div class="well ">
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                <tr class="heading">
                    <th>Name</th>
                    <th>Description</th>
                    <th>Edit</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($demo as $de)
                    <tr>
                        <td>{!! $de->name !!}</td>
                        <td>{!! $de->desc !!}</td>
                        <td><a href = 'editdemo/{{ $de->id }}'>Edit</a></td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>

    </div>
    <br>
</div>
</body>
</html>
 

2.create demoshow.blade.php to edit the data

<!DOCTYPE html>
<html>
<head>
    <title>Update Data For Existing Table Using Model In Laravel </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">
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>Edit & Update Data For Existing Table Using Model In Laravel</h1>
    </div>
    <div class="form-group col-sm-2"></div>
    <div class="form-group col-sm-8">
        <div class="well">
            <form action="{{url('/update/'.$demo->id)}}" method = "post">
                <!-- Name Field -->
                <label>Name</label>
                <input type="text" name="name" value="{{$demo->name}}" class="form-control">
                <br>
                <label>Desc</label>
                <input type="text" name="desc" value="{{$demo->desc}}" class="form-control">
                <br>
                <input type="submit" value = "Update" class="btn btn-primary">
            </form>
            <div class="form-group col-sm-2"></div>
        </div>
    </div>
    <br>
</div>
</body>
</html>


3.Implement code in controller as below to update data

<?php

namespace App\Http\Controllers;

use App\Models\demo;
use Illuminate\Http\Request;
use DB;

class demoController extends Controller
{
    public function index(){
        $demo=demo::paginate(5);
        return view('demo',compact('demo'));
    }
    public function show($id) {
        $demo=demo::whereId($id)->first();
        return view('showdemo',['demo'=>$demo]);
    }
    public function update(Request $request,$id)
    {
        $name = $request->input('name');
        $desc = $request->input('desc');
        $demo = demo::whereId($id)->update(['name' => $name, 'desc' => $desc]);
       return 'Demo updated successfully';
    }
}


4.Create route in web.php

Route::get('demo','demoController@index');
Route::get('editdemo/{id}','demoController@show');
Route::post('updatedemo/{id}','demoController@update');




Mass Updates
Updates can also be performed against any number of models that match a given query. In this example, all flights that are active and have a destination of San Diego will be marked as delayed:

App\demo::where('id', 1)->update(['name' => 'test]);


Examining Attribute Changes

Eloquent provides the isDirtyisClean, and wasChanged methods to examine the internal state of your model and determine how its attributes have changed from when they were originally loaded.

The isDirty method determines if any attributes have been changed since the model was loaded. You may pass a specific attribute name to determine if a particular attribute is dirty. The isClean method is the opposite of isDirty and also accepts an optional attribute argument:

$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);

$user->title = 'Painter';

$user->isDirty(); // true
$user->isDirty('title'); // true
$user->isDirty('first_name'); // false

$user->isClean(); // false
$user->isClean('title'); // false
$user->isClean('first_name'); // true

$user->save();

$user->isDirty(); // false
$user->isClean(); // true

The wasChanged method determines if any attributes were changed when the model was last saved within the current request cycle. You may also pass an attribute name to see if a particular attribute was changed:

$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);

$user->title = 'Painter';
$user->save();

$user->wasChanged(); // true
$user->wasChanged('title'); // true
$user->wasChanged('first_name'); // false

The getOriginal method returns an array containing the original attributes of the model regardless of any changes since the model was loaded. You may pass a specific attribute name to get the original value of a particular attribute:

$user = User::find(1);

$user->name; // John
$user->email; // john@example.com

$user->name = "Jack";
$user->name; // Jack

$user->getOriginal('name'); // John
$user->getOriginal(); // Array of original attributes...

Related Post