TL;DR
$model->relation()
은 모델 객체(object)를 반환$model->relation
은 relation의 결과값을 반환
Laravel에선 모델간의 관계(relation)을 편리하게 사용할수 있는 기능을 제공한다.[docs]
외래키(foreign_key)를 이용하여 해당 데이터에 해당되는 값을 가져올 수 있는 편리한 기능이다.
다음과 같은 구조의 DB가 있다고 생각해보자.
User : table
– id : integer (auto_increase, primary_key)
– name : string
– password : string
Post : table
– id : integer (auto_increase, primary_key)
– title : text
– content : text
– user_id : integer (foreign_key on user table)
참고로 DB에 string이란 type은 없다.
이해를 돕기 위해 string으로 표기한것이다.
이때 Post table의 user_id
로 해당 post를 작성한 user의 정보를 가져올 수 있고,user_id
로 User Table의 id(primary_key)
를 검색하여 user가 적성한 post를 가져올 수 있다.
이를 Laravel의 Eloquent의 Relation의 hasMany
, belongsTo
등을 이용해
Model끼리의 Relation을 만들 수 있다.
자세한 사항은 Eloquent Relation 문서를 확인하기 바라며,
위와 같이 연결하였다면, 다음과 같이 Post혹은 User를 불러올 수 있다.
<?php
// Get user info of user id 1
$user = App\User::find(1)->first();
// Get posts that user_id is 1 in post table
$user->posts();
$user->posts;
<?php
// Get user info of user id 1
$post = App\Post::find(1)->first();
// Get user that id is 1 in user table
$post->user();
$post->user;
그런데 여기서 궁금증이 생겼다.
Model의 Relation을 통해 데이터를 가져올 수 있는 방식은 총 2가지 인데,$model->relation()
과 $model->relation
이다.
이 두가지의 방법의 가장 큰 차이는
Result(결과 값) 를 반환 하느냐
Object 를 반환 하느냐의 차이이다.
$model->relation()
$model->relation()
은 Object를 반환하는 데 해당 오브젝트는 hasMany, BelongsTo 등 여러가지가 있지만 일단 Eloquent를 상속 받은 object를 반환한다.
실제로 tinker
을 통해 get_class()
를 통해 어떠한 값을 반환하는 지 확인해 보면 아래와 같은 값을 출력한다.
>>> get_class(App\User::find(1)->posts())
"Illuminate\Database\Eloquent\Relations\HasMany"
>>> get_class(App\Post::find(1)->user())
"Illuminate\Database\Eloquent\Relations\BelongsTo"
위와 같이 Eloquent의 Relation class를 가지는 object를 반환하며 해당 값은 Eloquent의 함수들인->select()
,->where()
, ->find()
등을 사용할 수 있다. [docs]
$model->relation
$model->relation
은 Result를 반환하는데 여기서 말하는 Result는 Model을 말한다.
이것도 tinker
를 이용해 결과값을 보면 아래와 같이 Model을 반환하는 것을 볼 수 있다.
>>> get_class(App\User::find(1)->posts)
=> "App\Post"
>>> get_class(App\Todo::find(1)->user)
=> "App\User"
즉, $model->relation
은 Relation에 해당되는 결과 값 모델을 가져온다는 뜻이다.
여기서 당연한 사항을 한개 말하자면,get_class($model->relation)
와 get_class($model->relation()->first())
은 서로 같은 값을 반환한다.
>>> get_class(App\Post::find(1)->user()->first())
=> "App\User"
>>> get_class(App\Post::find(1)->user)
=> "App\User"