Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Update single field of table using a Resource Controller

I have a Resource Controller (with all the actions: index, create, store, show, edit, update and destroy) and I was wondering what is the best approach to edit a single field column?

Let's say we have a Users table with name, email, password and active (active is a tiny int 0 or 1).

In the users management page, there is a button to activate/deactivate users (makes a request to the server to update the "active" field for the selected user).

Should I create a new method updateStatus in the Controller or is there a way to handle this using the update method?

I don't want, by mistake, allow empty values in the name, email or password when updating the "active" column, so I need to keep the validation rules (in short, all fields are required), but this means when updating the "active" field, I need to pass all the user data in the request.

At this point I'm very confused and all help will be appreciated.

Thanks in advance!

like image 429
TJ is too short Avatar asked Nov 10 '17 15:11

TJ is too short


2 Answers

When you send an instance from edit action to the form , all the data will be sent and you can edit one or more columns if you need . For instance :

public function update(Request $request , $id) {
    $data = YourModel::find($id);
    $data->someColumn = $request->someColumn;
    $data->save();
}

other fields that you didn't send any value for them will be saved as they were before . for this you can set the form like below :

{!! Form::model($yourInstance,['route'=>['someRoute.update','id'=>$yourInstance->id],'method'=>'PATCH',]) !!}
like image 162
Alireza Amrollahi Avatar answered Oct 29 '22 11:10

Alireza Amrollahi


It sounds like you are new to Laravel, and some key concepts can be hard to grasp.

In my opinion the best way to do it would be via a Model class. This is slightly confused by the fact that Laravel has a built in Users model, so I'm going to use a different model as the example of how to update a db field.

php artisan make:model MyData

Will create a new empty model file for the MyData table in app/

The file will look like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MyData extends Model
{
    //
}

Even though there's nothing in there, it now allows you do alter the database table using Eloquent.

In your controller add this to make sure the model is included:

use App\MyData as MyData;

The controller should have a method something like this if updating with user input from a form:

public function updateStatus(MyData $myData, Request $request){

    $myData->where('id', $request->id)->update(['active' => $request->active]);
}

You could do the exact same thing like this:

public function updateStatus(Request $request){
  $data = MyData::find($request->id);
  $data->active = $request->active;
  $data->save();
}

Both approaches make sense in different circumstances.

See https://laravel.com/docs/5.5/eloquent#updates

like image 34
fred2 Avatar answered Oct 29 '22 11:10

fred2