laravel hasone hasmany belongsto

hasone

user 単
article 単


# 下準備


Article.php

```

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //
}



```

user.php


```

<?php

namespace App;

class User extends Authenticatable implements MustVerifyEmail
{
    public function articles()
    {
        return $this->hasOne('App\Article');
    }

}



```


NikkiController.php


```

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Route;
use Log;

//使いたいモデルを指定
use App\Nikki;
use App\User;


class NikkiController extends Controller
{


    public function show()
    {
        $res = User::query()->get();

        foreach ($res as $key => $v) {
            print_r($v->name);
            if(!empty($v->articles->title)){
                print_r($v->articles->title);
            }
        }

        print_r($res);
        die;

    }
    

}


```


# belongsTo


Article.php



```

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //

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



```



NikkiController.php


```

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Route;
use Log;

//使いたいモデルを指定
use App\Nikki;
use App\User;
use App\Article;


class NikkiController extends Controller
{
    
    public function show()
    {
        
        $res = Article::query()
            ->where('id',6)//id6のを検索
            ->get();

pd($res[0]->user->name);

        foreach ($res as $key => $v) {
            print_r($v->title);
            print_r($v->user->name);
        }
        die;
        
    }

}


```


# hasmany


User.php

```

    public function articles()
    {
        return $this->hasMany('App\Article');
    }


```

NikkiController.php


```

<?php


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Route;
use Log;

//使いたいモデルを指定
use App\Nikki;
use App\User;
use App\Article;




class NikkiController extends Controller
{
    public function show()
    {
        $res = User::query()->get();
        foreach ($res as $key => $v) {
            pd($v->name);
            if(!empty($v->articles)){
                foreach ($v->articles as $s) {
                    pd($s->title);
                }
            }
        }
        pd($res);
        die;
    }
    
}

```

# アソシーエーション複雑な検索

with に 2 つのテーブルを指定することで複数のテーブルを連結できる。

```

        $res = User::query()
//            articlesの情報をもつユーザーのみ取得
//            ->has('articles')
            ->get();


//        articlesテーブルの ID 6 をもつユーザーのみ取得
        $res = User::whereHas('articles', function ($query) {
            $query->where('id',6);
        })->get();



////        articlesテーブルのID が 6 を持たないユーザーのみ取得
        $res = User::whereDoesntHave('articles', function ($query) {
            $query->where('id',6);
        })->get();


//        articles.id 6 を持っている記事のみ取得。ユーザーはすべて取得
        $res = User::with(['articles' => function ($query) {
            $query->where('id', 6);
        }])->get();



//        articles.id 6 を持っている記事のみ取得。ユーザーはすべて取得
        $res = User::with(
            [
                'nikkis' => function ($query) {
                    $query->where('id', 600);
                },
                'articles' => function ($query) {
                    $query->where('id', 7);
                }
            ]
        )->get();


        foreach ($res as $key => $v) {
            pd($v->name);
            if(!empty($v->articles)){
                foreach ($v->articles as $s) {
                    pd($s->title);
                }
            }
        }

```


# 関連モデルの関連モデルまで取得する

```

//        ユーザーID 10 のユーザーのみ取得し、さらに id 6 の記事のみ取得
        $res = User::with(
            [
                'articles' => function ($query) {
                    $query->where('id', 6);
                }
            ]
        )
            ->where('id',10)
            ->get();

// articles に関連した info も取得する
        foreach ($res as $key => $v) {
            if(!empty($v->articles)){
                foreach ($v->articles as $s) {
                    pd($s->uploads);

                    foreach ($s->uploads as $r) {

                        pd($r->info);

                    }

                }
            }
        }

```