[英]Associate models with eachother Laravel
I have this if statement, if no $address information has been added to a user, then create, else go on. 我有这个if语句,如果没有将$ address信息添加到用户,则创建,否则继续。
public function getIndexEditClient(Request $request, $id) {
$regions = DB::table("regions")->pluck("name","id");
$address = Address::where('id', $request->address_id)->with('region')->first();
if(empty($address)){
$address = Address::create([
'street_name',
'house_number',
'postcode',
'city_id',
'country_id',
'region_id'
]);
// assiociate address with user
//then save it
}else{
$data = $this->data->getEditClient($id);
$admins = $this->data->getAdmin();
return view('client.edit', [
'client' => $data,
'admins' => $admins,
'regions' => $regions,
'address' => $address
]);
}
}
The only thing is, i have to associate the user(client) with the addres @ the commented lines. 唯一的是,我必须将user(client)与addres @注释行关联。 I don't get it to work.
我不明白。
try this: 尝试这个:
$user->address()->associate($address);
$user->save();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.