[英]Laravel 4 Creating Form for adding record with relationship
I have created very basic Model. 我创建了非常基本的模型。 I have persons table and emails table.
我有人员表和电子邮件表。 Also I have create a link in the
persons/show.blade.php
("Add mail"). 另外,我在
persons/show.blade.php
创建了一个链接(“添加邮件”)。 My models are 我的模特是
class Person extends Eloquent {
protected $table = 'persons';
public function email()
{
return $this->HasMany('Email');
}
}
and 和
class Email extends Eloquent {
protected $table = 'emails';
public static $rules = array(
'email'=>'required|unique:emails'
);
public function person()
{
return $this->belongsTo('Person');
}
}
How can I pass the $person->id
to the new Controller? 如何将
$person->id
传递给新的Controller?
In my show.blade.php
for Person
I added 在我
show.blade.php
的Person
show.blade.php
中
{{ HTML::linkRoute('email.adduseremail','Προσθήκη Email',array($person->id))}}
and I added to my EmailController 然后我添加到我的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');
}
and my createforuser
view is 我的
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>
I keep getting Trying to get property of non-object (View: /var/www/laravel/app/views/email/show.blade.php
) 我一直在尝试获取非对象的属性(查看:
/var/www/laravel/app/views/email/show.blade.php
)
Is there any example using Form and Models for inserting new objects to the database for 'belongsTo' Relationship? 有没有使用表格和模型将新对象插入数据库的“ belongsTo”关系的示例? I couldn't find anything complete , just partial examples.
我找不到完整的内容,只是部分示例。
I generally use laravel sessions or laravel cache to tempererally save an id that i need to use later like: Session::set('personId',$person->id); 我通常使用laravel会话或laravel缓存来临时保存一个以后需要使用的ID,例如:Session :: set('personId',$ person-> id); Session::get('personId');
会议::得到( 'PERSONID');
The same is for cache except cache will only last for the current request session is persistent for the session Hope that helps 缓存也一样,只是缓存将仅在当前请求会话中持续,对于会话是持久的。希望有所帮助
I am no sure if I 'm supposed to answer my own question but finally I found a solution. 我不确定是否应该回答自己的问题,但最终我找到了解决方案。 I set two new routes
我设定了两条新路线
Route::post('email/adduseremail/{pid}', array('uses'=>'EmailController@adduseremail','as' => 'email.adduseremail'));
Route::post('email/storeforuser/{pid}', array('uses' =>'EmailController@storeforuser','as' => 'email.storeforuser'));
and created the corresponding methods in my Controller 并在我的控制器中创建了相应的方法
public function adduseremail($id)
{
$pname = Person::Find($id)->name;
$psurname = Person::Find($id)->surname;
return View::make('email.createforuser',array('id'=>$id,'name'=>$pname, 'surname'=>$psurname));
}
and 和
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.show',array($person->id));
}
then on my view blade pages I can pass the parameters from View to Form and so on 然后在我的视图刀片页面上,我可以将参数从“视图”传递到“表单”,依此类推
person.show.blade.php
{{Form::Open(array('route' => array('email.adduseremail', $person->id),'method' => 'POST','style'=>'display:inline;'))}}
{{Form::submit('Add Email')}}
{{Form::close()}}
and 和
email.createforuser.blade.php
{{Form::open(array('route'=>array('email.storeforuser',$id),'method' => 'POST','style'=>'display:inline;'))}}
{{Form::label('email', 'Email:')}}
{{Form::input('text', 'email')}}
{{Form::submit('Submit')}}
Hope it helps others also 希望它也能帮助别人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.