请检查this answer看到推的差异()和save()
您需要正确地定义你的模型relationships as per documentation 如果做得正确,它应该工作。 这是推()的作用:
/**
* Save the model and all of its relationships.
*
* @return bool
*/
public function push()
{
if (! $this->save()) return false;
// To sync all of the relationships to the database, we will simply spin through
// the relationships and save each model via this "push" method, which allows
// us to recurse into all of these nested relations for the model instance.
foreach ($this->relations as $models)
{
foreach (Collection::make($models) as $model)
{
if (! $model->push()) return false;
}
}
return true;
}
在你的情况,你有一个(经销商)属于多个(用户)
在用户模式:
class Users extends Eloquent {
public function dealership()
{
return $this->belongsTo('Dealership');
}
}
在上面的例子中,Eloquent将在users表中寻找dealerhip_id列。 在您的经销店型号:
class Dealership extends Eloquent {
public function users()
{
return $this->hasMany('User');
}
}
在你的存储功能:
public function store()
{
$user = new User();
$user->email = Input::get('email');
$user->password = Input::get('password');
$user->dealership = new Dealership();
$user->dealership->name = Input::get('dealership_name');
$user->push();
return "User Saved";
}
在这里学到更多关于eloquent relationships
另外请look at my answer here
哪个代码获取的部分是经销商ID? – ajtrichards
对不起,如果我包含我的模型的代码,我想也是有帮助的,哈哈。 http://laravel.io/bin/N7Pm – BeardedInBindary