[英]Laravel 5.2 relationship one to one query
我如何執行查詢以類似於此示例:$ model-> model2-> attribute
<div class="form-group">
{!! Form::label('Route name') !!}
{!! Form::text('name', ( isset($climb->route->name) ? $climb->route->name : null ), array('class'=>'form-control' )) !!}
</div>
您可以嘗試在視圖中使用模型,就像在字典中一樣,在控制器中添加類似的內容。
$model = Model::find($id);
$model['model2'] = $model->model2;
return view('your_view', ['model' => $model]);
為此,我假設您已經在模型中准備好關系,對您的真實模型應該這樣做,這樣視圖才能工作
<div class="form-group">
{!! Form::label('Route name') !!}
{!! Form::text('name', ( isset($climb['route']['name']) ? $climb['route']['name'] : null ), array('class'=>'form-control' )) !!}
</div>
創建一個關系:
具有:
class Comment extends Model
{
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post');
然后,您將呼叫lika:
$comment = App\Comment::find(1);
echo $comment->post->title;
https://laravel.com/docs/5.2/eloquent-relationships#one-to-many
我認為是很多,不是一個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.