简体   繁体   English

无法在 Model object Laravel 上调用 SAVE 方法

[英]Not able to call SAVE method on Model object Laravel

I'm trying to call save method on an object of a Model Complaints Model to update a specific resource in Database but when I send a POST request on api url it gives this error I'm trying to call save method on an object of a Model Complaints Model to update a specific resource in Database but when I send a POST request on api url it gives this error

BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::save does not exist. BadMethodCallException:方法 Illuminate\Database\Eloquent\Collection::save 不存在。

I also tried calling update method but same error.我也尝试调用update方法,但同样的错误。

Code for edit:编辑代码:

$complaint = Complaint::where('complaint_id', $request->input('id'))->get();

            $complaint->title = $request->input('Complaint_Subject');
            $complaint->description = $request->input('Complaint_Details');
            $complaint->address = $request->input('Complaint_Address');

            $complaint->update();

            return [
                'save_status' => "success"
            ];

the very first line is returning the response and correct response.第一行是返回响应和正确响应。

AND

also I'm trying to call delete or destroy method to delete the resource but it also gives the same error我也试图调用deletedestroy方法来删除资源,但它也给出了同样的错误

BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::delete does not exist. BadMethodCallException:方法 Illuminate\Database\Eloquent\Collection::delete 不存在。

or或者

BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::destroy does not exist. BadMethodCallException:方法 Illuminate\Database\Eloquent\Collection::destroy 不存在。

Code for delete:删除代码:

$complaint = Complaint::where('complaint_id', $request->input('id'))->get();
            $complaint->destroy($request->input('id'));

            return [
                'request_status' => 'success',
                'complaint' => $complaint
            ];

here also the response is being returned correctly.这里也正确返回了响应。

Note that the complaint_id is not Primary Key in the table so I cannot simply call Complaint::find($request->input('id')) I must have to cross check with this complaint_id column value to find the resource.请注意, complaint_id不是表中的主键,因此我不能简单地调用Complaint::find($request->input('id'))我必须交叉检查此complaint_id列值才能找到资源。 I have been searching since yesterday but could not find any solution.我从昨天开始一直在搜索,但找不到任何解决方案。 How can I solve this problem.我怎么解决这个问题。 Thanks!!!谢谢!!!

If your $complaint is a collection of output.如果您的$complaint是 output 的集合。 So save didn't work for this if you change your code like $complaint = Complaint::where('complaint_id', $request->input('id'))->first();因此,如果您save代码更改为$complaint = Complaint::where('complaint_id', $request->input('id'))->first(); Then save() will work.然后save()将工作。

just use first() instead of get()只需使用 first() 而不是 get()

Complaint::where('complaint_id', $request->id)->first()

In your query在您的查询中

$complaint = Complaint::where('complaint_id', $request->input('id'))->get();

it returns Associative Arrays它返回Associative Arrays

But if you use但是如果你使用

$complaint = Complaint::where('complaint_id', $request->input('id'))->first();

it returns Indexed or Numeric Arrays .它返回Indexed or Numeric Arrays Then you have to use $complaint[0]->destroy($request->input('id'));然后你必须使用$complaint[0]->destroy($request->input('id')); or $complaint[1]->destroy($request->input('id'));$complaint[1]->destroy($request->input('id')); and so one所以一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM