How to update data in Laravel controller?

I am trying to update my template details in the controller. But currently, my code is not working. It is not updating the fields. Anyhow if I add $template->save(). It saves the updated record as a new record. How to make my current code work? Why I am facing this situation? Please, someone, explain to me and correct my code as still, I am learning Laravel. Thanks in advance.

update function in TemplateController

public function update(Request $request, $id)
    {
        if(! lara_club_has_permission('edit-template') ){
            return view('403');
        }

        $this->validate($request, [
                'title'=>'required',
                'start_date'=> 'required',
                'end_date'=>'required',
                'template_content'=>'required',
            ]
        );

        //check status response
        if(isset($request->status)&&$request->status=='on'){
            $status='1';
        }else{
            $status="0";
        } 

        $template=new Template();

        $template->title=$request->title;
        $template->start_date=convert_to_sql_date_format($request->start_date);
        $template->end_date=convert_to_sql_date_format($request->end_date);
        $template->is_active=$status;
        $template->template_content=$request->template_content;

        $input = $request->all();

        $template->update($input);

        $name = $input['title'];

        return redirect()->route('templates.index')->with('success', 'Template <b>'. $name.'</b> Updated!');
    }

you creating new object $template=new Template(); but you need to update existing one so try below code

        $template=Template::find($id);
        //or use $template=Template::where('id',$id)->first();

        $template->title=$request->title;
        $template->start_date=convert_to_sql_date_format($request->start_date);
        $template->end_date=convert_to_sql_date_format($request->end_date);
        $template->is_active=$status;
        $template->template_content=$request->template_content;

        $input = $request->all();

        $template->update($input);

Update data from database using Laravel framework, Form::model($task, [ 'method' => 'PATCH', 'route' => ['tasks. update', $task->id] ]) !!} <div class="form-group"> {!! Form::label('title', 'Title:', ['class' => 'control-label']) !!} Update data from database using Laravel framework Previous Next In this example we will discuss about how to update a record or data from MySQL database using laravel framework PHP.

You should use Laravel find method, instead of declaring a new instance of it.

It should be

$template=Template::find($id);

Not

$template=new Template();

CRUD (Create Read Update Delete) in a Laravel App, Here.. in this video.. the updation of data into database is done in laravel..like.. Click on EDIT Duration: 22:46 Posted: Sep 8, 2018 Often while making an application we need to perform CRUD (Create, Read, Update, Delete) operations. Laravel makes this job easy for us. Just create a controller and Laravel will automatically provide all the methods for the CRUD operations. You can also register a single route for all the methods in routes.php file. Example

In my case I specify the table name in the method name, like this.

public function update(Request $request, Template $id)
{
    $id->title=$request->title;
    $id->start_date=convert_to_sql_date_format($request->start_date);
    $id->end_date=convert_to_sql_date_format($request->end_date);
    $id->is_active=$status;
    $id->template_content=$request->template_content;

    $input = $request->all();

    $id->update($input);
}

How to Update Data in Laravel using Resource Controller, Laravel - Update Records - We can update the records using the DB facade with $query(string) − query to execute in database; $bindings(array) − values to bind with queries php artisan make:controller StudUpdateController --plain. This Is Laravel 5.6 CRUD Tutorial example using resource controller.It is a simple example of CRUD operation in laravel 5.6. We are building the simple laravel application that has listing records, and add record, edit record and delete the record from database.

Laravel - Update Records, Now, execute php artisan make:controller PagesController . and initialize $ data Array and pass in Page::updateData() with $editid´┐Ż By default, Laravel's sample environment configuration is ready to use with Laravel Homestead, which is a convenient virtual machine for doing Laravel development on your local machine. You are free to modify this configuration as needed for your local database.

Insert Update and Delete record from MySQL in Laravel, Edit Specific Customer */ Route::get('/customers/{id}/edit', array( 'as' => 'customers -edit', 'uses' => 'CustomerController@edit' )); /** * Update Specific Customer´┐Ż I want to update multiple rows in Permission Table. I have permission id in array but i am unable to use where clause in query. I want to use permissionid in where clause and the permission id is in array of data. I can insert new records using array but i am finding difficulty to update using array. Here is my controller

Updating data to database, The refresh method will re-hydrate the existing model using fresh data from the class FlightController extends Controller { /** * Create a new flight instance. In this step, i would like to share with you step by step tutorial of crud operation with laravel 5.8 application for beginners. you will get how to create simple insert update delete operation with laravel 5.8 from scratch. here is a simple example of laravel 5.8 crud app.

Comments
  • you should add the condition for where to update