php 使用修改的主键更新条目时,Laravel 5.5 方法保存不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46884860/
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
Laravel 5.5 Method save does not exist when updating entries with modified primary key
提问by Melaku Minas Kasaye
I am working with laravel 5.5 to update entries. The problem is after changing the primary key 'id', which is elequoent default pk to 'project_id'. adding an item works fine but updating an item is not working properly. Here is the error I am getting.
我正在使用 laravel 5.5 来更新条目。问题是在更改主键 'id' 之后,这是 elequoent 默认 pk 到 'project_id'。添加项目工作正常,但更新项目无法正常工作。这是我得到的错误。
Method save does not exist.
方法 save 不存在。
Here is my Model.
这是我的模型。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $primaryKey = 'project_id';
public function user()
{
return $this->belongsTo(User::class);
}
public function tasks()
{
return $this->hasMany(Task::class);
}
}
Here is my controller function.
这是我的控制器功能。
public function editProject($id){
$project = Project::where('project_id', $id)->firstOrFail();
$data = ["project_info" => $project];
return view('projects.edit')->with($data);
}
public function updateProject(Request $request){
$data = $request->all();
$validator = Validator::make($data, [
'project_title' => 'required',
'project_description' => 'required',
'project_start_date' => 'required',
'project_end_date' => 'required',
'project_status' => 'required',
]);
$response = [];
if ($validator->fails()){
$response["errors"] = [$validator->messages()->first()];
$response["success"] = false;
return json_encode($response);
}
else{
$project = Project::where("project_id", $request->input('project_id'))->get();
$project->project_title = $request->project_title;
$project->user_id = Session::get('user_id');
$project->project_description = $request->project_description;
$project->project_start_date = $request->project_start_date;
$project->project_end_date = $request->project_end_date;
$project->project_status = $request->project_status;
$project->save();
return redirect('/listProjects');
}
}
回答by James
Using get()
returns a collection. Despite the fact you are passing in a 'unique' ID, the project_id
, it will still return a collection - the collection will simply have one element in it.
使用get()
返回一个集合。尽管您传入了一个“唯一”ID,但project_id
它仍然会返回一个集合 - 该集合中只会包含一个元素。
Subsequently, your code will not work as you have experienced, or at least not without a few changes to make $project
reference the first element in the collection.
随后,您的代码将无法像您所经历的那样工作,或者至少在没有一些更改以$project
引用集合中的第一个元素的情况下。
It's a quick fix though, just change this:
不过,这是一个快速修复,只需更改此:
$project = Project::where("project_id", $request->input('project_id'))->get();
to this:
对此:
$project = Project::where("project_id", $request->input('project_id'))->first();
By using first()
, eloquent will return the first element that matches the query and actually return the element (as opposed to a collection with one element) and so you can directly edit and save it.
通过使用first()
,eloquent 将返回与查询匹配的第一个元素并实际返回该元素(而不是具有一个元素的集合),因此您可以直接编辑和保存它。
回答by Melaku Minas Kasaye
Here is the solution I found.
这是我找到的解决方案。
$project_id = $request->input('project_id');
$project = Project::find($project_id);
$project->save();
回答by Thiago Maciel
You can find it by id using
您可以使用 id 找到它
Project::find($id);
Project::find($id);
Or get the first element like James said:
或者像 James 说的那样获取第一个元素:
$project = Project::where("project_id", $request->input('project_id'))->first();