【问题标题】:Laravel 4 Creating Form for adding record with relationshipLaravel 4 创建表单以添加具有关系的记录
【发布时间】: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”关系的示例?我找不到任何完整的东西,只是部分示例。

【问题讨论】:

    标签: php forms laravel-4 model


    【解决方案1】:

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

    缓存也是如此,除了缓存只会持续到当前请求会话是持久的会话 希望有帮助

    【讨论】:

    • 谢谢,我最终实施了您的建议,因为我的方式(见下文)在验证失败时遇到了问题,不得不重定向到发布路线
    【解决方案2】:

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

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

    并在我的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));
    }
    

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

    然后在我的视图刀片页面上,我可以将参数从视图传递到表单等等

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

    希望它也能帮助其他人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-27
      • 2012-08-21
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      相关资源
      最近更新 更多