Laravel:使用资源控制器更新表的单个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47226428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:56:57  来源:igfitidea点击:

Laravel: Update single field of table using a Resource Controller

phplaravelrestlaravel-5

提问by TJ is too short

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).

假设我们有一个用户表,其中包含姓名、电子邮件、密码和活动(活动是一个小整数 0 或 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 updatemethod?

我应该在控制器中创建一个新方法 updateStatus 还是有办法使用update方法来处理这个问题?

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!

提前致谢!

回答by Alireza Amrollahi

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',]) !!}

回答by fred2

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

听起来您是 Laravel 的新手,有些关键概念可能难以掌握。

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.

在我看来,最好的方法是通过 Model 类。Laravel 有一个内置的 Users 模型,这有点令人困惑,所以我将使用不同的模型作为如何更新 db 字段的示例。

php artisan make:model MyData

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

将为 app/ 中的 MyData 表创建一个新的空模型文件

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.

即使那里没有任何内容,它现在允许您使用 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

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