php 创建一个包含关系的 Eloquent 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30788583/
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
Creating an Eloquent Object with relation included
提问by echoashu
I'm pretty much new to opps and laravel both
So, to insert the values into my users
and profiles
table which hav OneToOne
relationship, Here is how my store()
method looks like
我对 opps 和 laravel 都很陌生所以,将值插入到我的users
和profiles
表中有OneToOne
关系的表中,这是我的store()
方法的样子
public function store(Requests\StoreNewUser $request)
{
// crate an objct of user model
$user = new \App\User;
// now request and assign validated input to array of column names in user table
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
$user->email = $request->input('email');
$user->password = $request->input('password');
/* want to assign request input to profile table's columns in one go
*/
$user->profile()->user_id = $user->id; // foreign key in profiles table
$user->profile()->mobile_no = $request->input('mobile');
dd($user); // nothing related to profile is returned
}
I'm creating the new record, hence dd()
never returns anything related to profile table.
我正在创建新记录,因此dd()
永远不会返回与配置文件表相关的任何内容。
Is this Because the $user
object is not including relationship by default?
If yes Can i create the $user
object which includes the associated relations in User
Model ?
这是因为$user
对象默认不包含关系吗?如果是,我可以创建$user
包含User
模型中关联关系的对象吗?
Or do i have to create two separate objects of each table and save()
the data
But then what is the significance of push()
method ?
或者我必须为每个表和save()
数据创建两个单独的对象
但是push()
方法的意义是什么?
EDIT 1P.S. yes, the relationships are already defined in User
& Profile
model
编辑 1PS 是的,关系已经在User
&Profile
模型中定义
回答by The Alpha
You may try something like the following. At first save the parent model like this:
您可以尝试以下操作。首先像这样保存父模型:
$user = new \App\User;
$user->first_name = $request->input('first_name');
// ...
$user->save();
Then create and save the related model using something like this:
然后使用以下内容创建并保存相关模型:
$profile = new \App\Profile(['mobile_no' => $request->input('mobile')]);
$user->profile()->save($profile);
Also make sure you have created the profile
method in User
model:
还要确保您已经profile
在User
模型中创建了方法:
public function profile()
{
return $this->hasOne('App\Profile');
}
回答by Matt
I thought i'd update this answer and make it applicable to Laravel 5 onwards. I'll use @The Alpha answer as a basis.
我想我会更新这个答案并使其适用于 Laravel 5。我将使用@The Alpha 答案作为基础。
$profile = new \App\Profile(['mobile_no' => $request->input('mobile')]);
$user->profile()->associate($profile); // You can no longer call 'save' here
$user->profile()->save();
The reason for this is you can no longer call save
on the belongsTo
relation (or any other), this now returns an instance of Illuminate\Database\Query\Builder
.
这样做的原因是,你可以再调用save
的belongsTo
关系(或任何其他),这个现在返回的一个实例Illuminate\Database\Query\Builder
。
回答by diegochaves
The clean way to do it now would be having on your User Class file:
现在执行此操作的干净方法是在您的用户类文件中:
public function profile()
{
return $this->hasOne(App\Profile::class);
}
and in your User Controller, the following store method:
并在您的用户控制器中,使用以下存储方法:
public function store(Requests\StoreNewUser $request)
{
$user = App\User::create(
$request->only(
[
'first_name',
'last_name',
'email'
]
)
);
$user->password = Illuminate\Support\Facades\Hash::make($request->password);
//or $user->password = bcrypt($request->password);
$user->profile()->create(
[
'mobile_no' => $request->mobile;
]
);
dd($user);
}
I didn know if u were saving plain text password to you database or using a mutator on password attribute, anyhow the suggested above is a good practice I think
我不知道您是将纯文本密码保存到您的数据库还是在密码属性上使用修改器,无论如何,我认为上面的建议是一个很好的做法
回答by haakym
Is this Because the $user object is not including relationship by default? If yes Can i create the $user object which includes the associated relations in User Model ?
这是因为 $user 对象默认不包含关系吗?如果是,我可以创建包含用户模型中关联关系的 $user 对象吗?
Yes you should create the relationship, they're not included by default.
是的,您应该创建关系,默认情况下不包括它们。
In your User
model you'd want to do something like this:
在你的User
模型中,你想要做这样的事情:
public function profile()
{
return $this->hasOne('App\Profile'); // or whatever your namespace is
}
This would also require you to have a Profile
model created.
这还需要您Profile
创建一个模型。
This would definitely answer your questions regarding inserting related models: http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models
这肯定会回答您关于插入相关模型的问题:http: //laravel.com/docs/5.1/eloquent-relationships#inserting-related-models
As The Alpha mentioned, and you also eluded to, I think you need to save your user model first then you can add via relationship.
正如 The Alpha 提到的,你也没有想到,我认为你需要先保存你的用户模型,然后你可以通过关系添加。