【发布时间】:2014-11-21 09:37:36
【问题描述】:
我创建了非常基本的模型。我有人员表和电子邮件表。
我还在persons/show.blade.php(“添加邮件”)中创建了一个链接。
我的模型是
class Person extends Eloquent {
protected $table = 'persons';
public function email()
{
return $this->HasMany('Email');
}
}
和
class Email extends Eloquent {
protected $table = 'emails';
public static $rules = array(
'email'=>'required|unique:emails'
);
public function person()
{
return $this->belongsTo('Person');
}
}
如何将$person->id 传递给新的控制器?
在我的show.blade.php 中为Person 添加了
{{ HTML::linkRoute('email.adduseremail','Προσθήκη Email',array($person->id))}}
然后我添加到了我的 EmailController
public function adduseremail($id)
{
return View::make('email.createforuser',['id'=>$id]);
}
public function storeforuser($pid)
{
$validator = Validator::make(Input::all(),Email::$rules);
if ($validator->fails()) {
$messages = $validator->messages();
foreach ($messages->all('<li>:message</li>') as $message)
return Redirect::back()->withInput()->WithErrors($messages);
}
$person = Person::FindorFail($pid);
$email = new Email;
$email->email = Input::get('email');
$email->person()->associate($person);
$email->save();
return Redirect::route('person.index');
}
我的createforuser 视图是
<p>
{{Form::open(['route'=>'email.storeforuser'])}}
<div>
{{Form::label('email', 'Email:')}}
{{Form::input('text', 'email')}}
{{$errors->first('email')}}
</div>
</br>
<div>
{{Form::submit('Submit')}}
</div>
{{Form::close()}}
<p>
我一直在尝试获取非对象的属性(查看:/var/www/laravel/app/views/email/show.blade.php)
是否有任何使用表单和模型将新对象插入到数据库中以实现“belongsTo”关系的示例?我找不到任何完整的东西,只是部分示例。
【问题讨论】: