Laravel 4创建用于添加具有关系的记录的窗体


Laravel 4 Creating Form for adding record with relationship

我已经创建了非常基本的模型。我有人员表和电子邮件表。此外,我还在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传递给新的控制器?

Personshow.blade.php中,我添加了

{{ 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

有没有使用Form和Models向数据库中插入"belongsTo"关系的新对象的示例?我找不到任何完整的东西,只有部分例子。

我通常使用laravel会话或laravel缓存来临时保存稍后需要使用的id,如:会话::set('personId',$person->id);会话::get('personId');

缓存也是如此,只是缓存只会持续当前请求会话对于会话是持久的希望对有所帮助

我不确定是否应该回答自己的问题,但最终我找到了解决方案。我设置了两条新路线

Route::post('email/adduseremail/{pid}', array('uses'=>'EmailController@adduseremail','as' => 'email.adduseremail'));
Route::post('email/storeforuser/{pid}', array('uses' =>'EmailController@storeforuser','as' => 'email.storeforuser'));

并在我的控制器中创建了相应的方法

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));
}

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));
}

然后在我的视图刀片页面上,我可以将参数从view传递到Form,依此类推

person.show.blade.php

{{Form::Open(array('route' => array('email.adduseremail', $person->id),'method' => 'POST','style'=>'display:inline;'))}}
{{Form::submit('Add Email')}}
{{Form::close()}}

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')}}

希望它也能帮助其他人