LoginSignup
1
1

More than 5 years have passed since last update.

Laravel5.6 データベースのリレーション belongsTo

Posted at

記事(article)をリレーションしてユーザ(user)を取り出す例です。

モデルでbelongsToを設定


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    /**
     * モデルと関連しているテーブル
     *
     * @var string
     */
    protected $table = 'article';

    protected $fillable = ["title"];

    //belongsTo設定
    public function user()
    {
       return $this->belongsTo('App\User');
    }
}

html内で次で呼べるようだ。


{{$article->user->name}}

参考サイト

Laravel 5.6 Eloquent:リレーション

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1