早些时候我正在研究核心php,目前我转向laravel和 MVC。所以我也不太了解mvc和laravel。我正面对一个 问题。帮我解决这个问题。
我有两张桌子:
公司模型:
class Company extends Eloquent {
protected $fillable = [
'name',
'email',
'website',
'logo',
'phone',
];
protected $primaryKey = 'id';
protected $table = 'companies';
}
company_address的模型:
class Companyaddress extends Eloquent {
protected $fillable = [
'companies_id',
'address_line_1',
'address_line_2',
'city',
'state',
'zipcode',
'country_id',
];
protected $primaryKey = 'id';
protected $table = 'company_address';
}
我的控制器保存这两个表:
public function store(CompanyRequest $request, Company $company, Companyaddress $companyaddress){
if(!Entrust::can('create-company'))
return response()->json(['message' => trans('messages.permission_denied'), 'status' => 'error']);
$data = $request->all();
$company->fill($data)->save();
$companyaddress->fill($data)->save();
storeCustomField($this->form,$company->id, $data);
$this->logActivity(['module' => 'company_address','module_id' => $company->id,'activity' => 'added']);
return response()->json(['message' => trans('messages.company').' '.trans('messages.added'), 'status' => 'success']);
}
我的代码运行正常。在提交我可以根据我的模型存储数据。
我想将company
表中的主键存储在company_address
表中作为外键。所以将来我可以显示特定公司的地址。
怎么做?
答案 0 :(得分:0)
这样做的好方法是使用雄辩的关系定义两者之间的关系。参见:
https://laravel.com/docs/5.4/eloquent-relationships
首先,在Company
模型上定义关系:
public function address()
{
return $this->hasOne(Companyaddress:class);
}
然后在您的控制器中,您可以执行以下操作:
$company = Company::create($data);
$company_address = Companyaddress::create($data);
$company->address()->save($company_address);
答案 1 :(得分:0)
将它放在$ data变量定义之前。 (因为您已经设置了$ company集合)
$request->merge(['companies_id'=>$company->id]);
在$ companyaddress之前。此外,不需要$ data变量(恕我直言)
答案 2 :(得分:0)
在保存公司地址之前设置公司ID:
$data->companies_id = $company->id;
或
$data['companies_id '] = $company->id;
商店方法:
public function store(CompanyRequest $request, Company $company, Companyaddress $companyaddress){
if(!Entrust::can('create-company'))
return response()->json(['message' => trans('messages.permission_denied'), 'status' => 'error']);
$data = $request->all();
$company->fill($data)->save();
$data->companies_id = $company->id; //Set company id here
$companyaddress->fill($data)->save();
storeCustomField($this->form,$company->id, $data);
$this->logActivity(['module' => 'company_address','module_id' => $company->id,'activity' => 'added']);
return response()->json(['message' => trans('messages.company').' '.trans('messages.added'), 'status' => 'success']);
}
答案 3 :(得分:0)
首先通过添加函数在模型中添加一些关系。
在您的公司模式中。
class Company extends Eloquent {
protected $fillable = [
'name',
'email',
'website',
'logo',
'phone',
];
protected $primaryKey = 'id';
protected $table = 'companies';
public function companyAddress()
{
return $this->hasOne('App\CompanyAddress');
}
}
在您的CompanyAddress模型中。
class Companyaddress extends Eloquent {
protected $fillable = [
'companies_id',
'address_line_1',
'address_line_2',
'city',
'state',
'zipcode',
'country_id',
];
protected $primaryKey = 'id';
protected $table = 'company_address';
public function companyAddress()
{
return $this->belongsTo('App\Company');
}
}
然后在您的控制器中。
y'可以在您的方法中添加此代码。 将save()方法转换为"公司" table,获取最后插入的ID,假设您的主键是自动增量。
$companyAddress = new CompanyAddress;
$companyAddress->companies_id = $data->id;
$companyAddress->save();
您可以将此文档作为参考: https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Relations.html
或许你可以在这里获得信息: Laravel, get last insert id using Eloquent
答案 4 :(得分:0)
在您的情况下,如果一家公司有一个地址,您应该将其合并到一个表中。但如果你需要,请点击这里:
Model Company.php
class Company extends Eloquent {
protected $fillable = [
'id',
'name',
'email',
'website',
'logo',
'phone',
];
protected $primaryKey = 'id';
protected $table = 'companies';
public function address(){
$this->hasOne(CompanyAddress::class,'company_id','id')
}
}
Model CompanyAddress.php
class CompanyAddress extends Eloquent {
protected $fillable = [
'company_id',
'address_line_1',
'address_line_2',
'city',
'state',
'zipcode',
'country_id',
];
protected $table = 'company_address';
}
在您的控制器中
public function store(CompanyRequest $request, Company $company, Companyaddress $companyaddress){
if(!Entrust::can('create-company'))
return response()->json(['message' => trans('messages.permission_denied'), 'status' => 'error']);
$data = $request->all();
$company->fill($data)->save();
$data->company_id = $company->id;
$companyaddress->fill($data)->save();
$this->logActivity(['module' => 'company_address','module_id' => $company->id,'activity' => 'added']);
return response()->json(['message' => trans('messages.company').' '.trans('messages.added'), 'status' => 'success']);
}
public function getCompany($id){
$company = Company::find($id);
$company->address;
return $company->toArray();
}
以及调用getCompany($ id)
时的结果{
id : ...,
name : ...,
.
.
.
address : {
company_id: ...,
address_line_1: ...,
.
.
.
}
}
希望它会有所帮助。感谢。