[英]How to update foreign key ide value in Laravel Eloquent?
我對 Laravel 還很陌生,無論是在 Laravel 文檔中,還是在這里,我都無法找到這個問題的答案。 我想這只是如何搜索它的問題,因為我很確定這是一個常見的情況。
我有兩個相關的模型(這是一個簡化的案例),我通過資源文件檢索了我需要的信息,但我無法理解如何正確存儲或更新信息。 這是一個代碼示例:
Models\Company.php
class Company extends Model
{
protected $fillable = [
'name', 'blablabla', 'country_id', 'blablabla2',
];
public function country() {
return $this->belongsTo(Country::class);
}
}
Models\Country.php
class Country extends Model
{
protected $fillable = [
'code', 'name', 'prefix', 'tax_code_id',
];
public function companies() {
return $this->hasMany(Company::class);
}
}
然后我有一個CompanyController文件來管理 API 請求:
Controllers\CompanyController.php
class CompanyController extends BaseController
{
public function index()
{
$companies = Company::paginate();
$response = CompanyResource::collection($companies)->response()->getData(true);
return $this->sendResponse($response, 'Companies retrieved successfully');
}
public function store(Request $request)
{
$input = $request->all();
$validator = Validator::make($input, $this->validation_rules);
if($validator->fails()){
return $this->sendError('Validation error.', $validator->errors());
}
$company = Company::create($input);
return $this->sendResponse($company->toArray(), 'Company added successfully.');
}
}
...
public function update(Request $request, Company $company)
{
$input = $request->all();
$validator = Validator::make($input, $this->validation_rules);
if($validator->fails()){
return $this->sendError('Validation error.', $validator->errors());
}
$company->update($input);
return $this->sendResponse($company->toArray(), 'Company updated successfully.');
}
這里是我用來根據需要顯示信息的CompanyResource 。
Resources/CompanyResource.php
class CompanyResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'blablabla' => $this->blablabla,
'country' => $this->country,
'blablabla2' => $this->blablabla2,
];
}
}
因此,在檢索公司(或單個公司)時,我得到一個嵌套的 JSON:
{
"id": "1",
"name": "something",
"blablabla": "blablabla",
"country": {
"id": "100",
"code": "MA",
"name": "Mars",
"prefix": "123",
"tax_code_id": "#$%"
},
"blablabla2": "blablabla2"
}
如果我創建或更新一家新公司,我會發送一個與上面的結構相同的有效負載,但是如果我編輯國家/地區ID 值,我的公司模型就不會得到它。
PUT Api/companies/1
{
"name": "something",
"blablabla": "blablabla3",
"country": {
"id": "200",
"code": "JU",
"name": "Jupiter",
"prefix": "456",
"tax_code_id": "#=%"
},
"blablabla2": "blablabla2"
}
我期待更新公司表中記錄 1 的country_id字段,以便它匹配有效負載(所以從 100 到 200),但它沒有發生。
我可以編輯前端邏輯以便在有效負載中僅發送country_id ,因為我不會更新國家/地區表,並且所有這些附加信息都是多余的,但我想知道如何使用 Laravel 在控制器中管理它。
你介意幫我嗎? 提前致謝。
如果您希望它現在與代碼一起使用,您需要在您發送的根 JSON 對象中包含country_id
。 因為這是您填寫 id 的方式。 在我看來,這不是最好的方法,但這就是您的更新目前不起作用的原因。
{
"name": "something",
"blablabla": "blablabla3",
"country_id": 200,
...
我實際上喜歡發送完整對象的方法。 通常填充 id 是不好的,因為它會干擾關系的工作方式。 Laravel會在你關聯時設置你的關系,否則你不能保證填充后有正確的關系。
因此,我會取出 id 並將國家對象與公司相關聯。 在與此類似的邏輯中。
// only fill non relation fields, fill used as save is needed after associate()
$company->fill($request->only(['name', 'blabla']));
$company->country()->associate(Country::find($request->get('country')['id']));
//associate does not save
$company->save();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.