2014-09-26 102 views
0

我已經創建了非常基本的模型。我有人桌子和電子郵件表。 我還在persons/show.blade.php(「添加郵件」)中創建了一個鏈接。 我的模型是Laravel 4創建添加關係記錄表格

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.phpPerson我加

{{ 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'關係的例子?我找不到任何完整的東西,只是部分例子。

回答

1

我通常使用laravel會話或laravel緩存來溫和地保存一個我以後需要使用的id: Session :: set('personId',$ person-> id); Session :: get('personId');

這同樣適用於除高速緩存將僅持續到當前請求會話持久性的會話 希望幫助

+0

謝謝,我終於實現你的建議,因爲我的方式(見下文),我遇到問題時,驗證失敗,不得不重新定向到一個帖子路線 – geoandri 2014-09-29 06:33:19

0

我不知道如果我「M應該回答我的問題,但最後我發現一個辦法。 我設置兩條新航線

Route::post('email/adduseremail/{pid}', array('uses'=>'[email protected]','as' => 'email.adduseremail')); 
Route::post('email/storeforuser/{pid}', array('uses' =>'[email protected]','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傳遞參數表格等

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

希望它可以幫助別人也