iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 29
0
Modern Web

VUE & PHP (Apache2) & Docker 實戰開發系列 第 29

Day29 - PHP-Laravel Eloquent Relationships 介紹

今天來講解LaravelEloquent Relationships裡的關係要如何建立,
資料庫(database)的表單(tables)都是有著相互依賴的關係,
例如,部落格的文章可能有很多評論,或者是那位客戶下的訂單是哪間供應商之間的關係,
Eloquent使管理和處理這些關係變得容易,並支持幾種不同類型的關係:

  • One To One
  • One To Many
  • Many To Many
  • Has Many Through
  • Polymorphic Relations
  • Many To Many Polymorphic Relations

Eloquent的關係被定義在model classes的方法裡,
像是model裡都可看到use Illuminate\Database\Eloquent\Model;
將關係定義為是非常強大的連接方法和查詢功能,下面就來一一介紹。

One To One

一對一關係在這裡是非常基礎的,例如,一位使用者發表一篇文章,它們彼此間就可以是One to One的關係,所以先在migrations裡建立好usersposts在到各自的model建立關係

users migration

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

posts migration

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

User model

    public function post()
    {
        return $this->hasOne('App\Post');         //hasOne表示一對一
    }

Post model

    protected $fillable = ['title', 'content'];

    public function user()
    {
        return $this->belongsTo('App\User');      //belongsTo表示逆轉一對一或一對多
    }

到資料庫管理工具手動輸入資料

users table

https://ithelp.ithome.com.tw/upload/images/20181110/20108846drvfMcAdrN.jpg

posts table

https://ithelp.ithome.com.tw/upload/images/20181110/20108846v76nUfQKbM.jpg

下一步建立Route

//One To One Relationship

Route::get('/user/{id}/post', function($id){

    return App\User::find($id)->post;          //回傳到App\User裡的`post`function

});

Route::get('/post/{id}/user', function($id){

    return App\Post::find($id)->user;          //回傳到App\Post裡的`user`function

});

啟動服務到127.0.0.1:8000/user/1/post,此路徑代表的是User的id:1的ost資料
https://ithelp.ithome.com.tw/upload/images/20181110/20108846MoAtXJoimK.jpg

啟動服務到127.0.0.1:8000/post/1/user,此路徑代表的是Post的id:1的user資料
https://ithelp.ithome.com.tw/upload/images/20181110/20108846Cw1biVfSWQ.jpg

One To Many

一對多關係用於定義單個model擁有許多數量的其他model的關係。例如,文章可能有無數的評論,可以在Eloquent Model上放置一個函數來定義一對多關係,Eloquent會自動導向posts裡的_id也就是user_id

User model

    public function posts()
    {
        return $this->hasMany('App\Post');         //Many表示一對多
    }

Post model

    protected $fillable = ['title', 'content'];

    public function user()
    {
        return $this->belongsTo('App\User');      //belongsTo表示逆轉一對一或一對多
    }

下一步建立Route

//One To Many Relationship

Route::get('/posts', function(){

	return App\User::find(1)->posts;               //回傳到App\User裡的`posts`function

});

Route::get('/users', function(){
   
	return App\Post::find(1)->user;                //回傳到App\Post裡的`user`function

});

啟動服務到127.0.0.1:8000/posts,此路徑代表的是User的id:1的全部Post資料
https://ithelp.ithome.com.tw/upload/images/20181111/20108846vekSDZUosh.jpg

啟動服務到127.0.0.1:8000/users,此路徑代表的是Post的id:1的User資料(這裡三筆資料都是回傳一樣的User)
https://ithelp.ithome.com.tw/upload/images/20181111/20108846aRhIbmMmXf.jpg

鐵人賽也到了尾聲,但我仍然會繼續記錄我的學習過程,
本篇介紹到此,下次見~


上一篇
Day28 - Vue & Container Instance
下一篇
Day30 - 最後的Azure Devops自動化建置部署流程
系列文
VUE & PHP (Apache2) & Docker 實戰開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言