Php Laravel 5 API:更新记录错误
因此,我正在开发一个Laravel5.2API,并在特定模型上创建更新方法。到目前为止,索引、show($id)和存储方法工作得很好。但是,我在更新方法实现中遇到以下错误:Php Laravel 5 API:更新记录错误,php,laravel,laravel-5.2,laravel-routing,api-design,Php,Laravel,Laravel 5.2,Laravel Routing,Api Design,因此,我正在开发一个Laravel5.2API,并在特定模型上创建更新方法。到目前为止,索引、show($id)和存储方法工作得很好。但是,我在更新方法实现中遇到以下错误: BadMethodCallException in Macroable.php line 81: Method save does not exist. 以下是更新方法: public function update($id, CreateZoneRequest $request) { $zones = Zone:
BadMethodCallException in Macroable.php line 81:
Method save does not exist.
以下是更新方法:
public function update($id, CreateZoneRequest $request)
{
$zones = Zone::where('_id', '=', $id) -> get();
if(sizeof($zones) == 0){
return response()->json(['message' =>'That zone number is invalid', 'code' => 404], 404);
}
$description = $request->get('description');
$zones ->description = $description;
$zones ->save();
return response() -> json(['message' => 'The zone has been updated'], 200);
}
以下是CreateZoneRequest代码:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateZoneRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return
[
'description' => 'required',
];
}
public function response (array $errors){
return response() -> json(['message' => 'You are missing a required field', 'code' => 422], 422);
}
}
必须使用first()获取区域模型的单个实例,然后才能使用save()或update()方法
您必须使用first()获得区域模型的单个实例,然后才能使用save()或update()方法
当您使用get()
方法时,您会得到一个集合
对象。您应该使用first()
方法来获取模型对象
在此处阅读更多信息:当您使用get()
方法时,您将获得一个集合
对象。您应该使用first()
方法来获取模型对象
请在此处阅读更多信息:使用$zones=Zone::where(“'u id','=',$id)->first()代码>或$zones=Zone::find($id)
而不是get
否则您将返回“数组”$zones->description=$request->description;更换这些线路的更漂亮的方法$description=$request->get('description')$区域->描述=$description@apokryfos如果我使用它,我现在在SQL查询中得到一个错误:SQLSTATE[42S22]:Column not found:1054未知列'id'在'where子句'中,这很奇怪,因为我告诉where子句要使用_id@WiloMaldonado哪一个导致了这个错误?实际上,查找
或第一个
?两者都是。但我想知道问题是发现还是第一个?或者,如果问题实际上是在尝试保存时出现的,请使用$zones=Zone::where(“'u id','=',$id)->first()代码>或$zones=Zone::find($id)
而不是get
否则您将返回“数组”$zones->description=$request->description;更换这些线路的更漂亮的方法$description=$request->get('description')$区域->描述=$description@apokryfos如果我使用它,我现在在SQL查询中得到一个错误:SQLSTATE[42S22]:Column not found:1054未知列'id'在'where子句'中,这很奇怪,因为我告诉where子句要使用_id@WiloMaldonado哪一个导致了这个错误?实际上,查找
或第一个
?两者都是。但我想知道问题是发现还是第一个?或者如果问题实际上是在它试图储蓄的时候?这是有道理的。但是,现在我从SQL查询中得到这个错误:SQLSTATE[42S22]:Column not found:1054“where子句”(SQL:updateZone
setdescription
=Blah whereid
为null)中的未知列“id”,因为该表的主键不是id而是id,获取区域时where子句是特定的。您的主键似乎是\u id
而不是传统的id
,您有什么建议?您是否在模型中设置了受保护的$primaryKey='_id'?对受保护的$primarykey=''u id';受保护的$table='区域';public$timestamps=false;受保护的$fillable=数组('description');$id
变量是否正确传递给更新
函数?尝试输出它,哪一行导致最后一个错误?他们使用save()
method?我发现了错误。我使用$primarykey而不是$primarykey。。。该死的骆驼箱,说得通。但是,现在我从SQL查询中得到这个错误:SQLSTATE[42S22]:Column not found:1054“where子句”(SQL:updateZone
setdescription
=Blah whereid
为null)中的未知列“id”,因为该表的主键不是id而是id,获取区域时where子句是特定的。您的主键似乎是\u id
而不是传统的id
,您有什么建议?您是否在模型中设置了受保护的$primaryKey='_id'?对受保护的$primarykey=''u id';受保护的$table='区域';public$timestamps=false;受保护的$fillable=数组('description');$id
变量是否正确传递给更新
函数?尝试输出它,哪一行导致最后一个错误?他们使用save()
method?我发现了错误。我使用$primarykey而不是$primarykey。。。这似乎是解决方案,但是,第一个()方法似乎假定该表的primarykey是id。但在我的例子中,它是_id。我在区域模型中正确地定义了它:protected$primarykey='_id';受保护的$table='区域'@WiloMaldonado如果您定义了primarykey,那么当您尝试使用first()
时,我看不出有问题。尝试并在这里说,或者在出现错误时编辑您的问题。我发现了错误。我使用$primarykey而不是$primarykey。。。这似乎是解决方案,但是,first()方法似乎假定该表的primarykey是id。但在我的例子中,它是_id。我在区域模型中正确地定义了它:protected$primarykey='_id';受保护的$table='区域'@WiloMaldonado如果您定义了primarykey,那么当您尝试使用first()
时,我看不出有问题。尝试并在这里说,或者在出现错误时编辑您的问题。我发现了错误。我使用$primarykey而不是$primarykey。。。今天是星期一,哈哈
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Zone;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\CreateZoneRequest;
class ZoneController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$zones = Zone::all();
if(sizeof($zones) == 0){
return response()->json(['message' =>'There are no zones', 'code' => 404], 404);
}
return response() -> json(['data' => $zones], 200);
}
public function update($id, CreateZoneRequest $request)
{
$zones = Zone::where('_id', '=', $id) -> first();
if(sizeof($zones) == 0){
return response()->json(['message' =>'That zone number is invalid', 'code' => 404], 404);
}
$description = $request->get('description');
$zones ->description = $description;
$zones ->save();
return response() -> json(['message' => 'The zone has been updated'], 200);
}