2015-09-13 66 views
3

我正在使用Laravel 5.1。Laravel 5.1 - 雄辩的关系更新函数

我有表,user表,users_detailoneToOne关系。 我已经成功地将两个数据表显示为一种形式。

现在我想在我的控制器中运行update函数。我已经知道如何在我的其他项目中运行update函数单个表。你可以给我一个线索如何运行update这个两个表在单个控制器?

我们是否需要使用此Laravel update()函数?要不然?

我用这对我view

... 
{!! Form::model($users, ['method' => 'PATCH', 'route' => 'dashboard.user.update', $users -> id]) !!} 
    <div class="form-group"> 
     {!! Form::label('User ID', 'User ID:') !!} 
     {!! Form::text('id', $users->id, $attributes = array('id'=>'disabledInput', 'Disabled', 'class'=>'form-control', 'placeholder'=>'User ID')); !!} 
    </div> 
... 
    <div class="form-group"> 
     {!! Form::label('Place Of Birth', 'Place Of Birth:') !!} 
     {!! Form::text('placeOfBirth', $users->UsersDetail->placeOfBirth, $attributes = array('class'=>'form-control', 'placeholder'=>'Place Of Birth')); !!} 
    </div> 
... 
... 
{!! Form::close() !!} 
... 

回答

0

最后,我找到了一种方法。这并不像我那么难。

public function update(Request $request, $id) { 
    //   
    $usersUpdate = $request->all(); //get all value from form 

    $users = User::find($id); //find the primary key of User table 
    $usersDetail = UsersDetail::find($id); //find the foreign key UsersDetail table 

    $users->update($usersUpdate); //update the User table 
    $usersDetail->update($usersUpdate); //update the UsersDetail table 

    return redirect('dashboard/user'); 
} 
1

继承人一个很好的把戏,所有在一行。 associate()用于更新belongsTo()关系。

它会像这样

$user = User::find($id); 
    $record= new User_Detail($request->all()); 

    $record->user()->associate($user); 
    $record->save(); 

许多你可以使用附加同步模式

生病用品这个例子很多关系。

$new_article = new Article($request->all()); 
    $new_article->save(); //I need to save it first so that we the article ID 

    Auth::user()->article()->attach($new_artcile->id);