2017年9月27日 星期三

Laravel,relationship,建立關聯


    //檔案:database/migrations/日期_編號_create_posts_table.php
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();
});
}


//下指令
php artisan migrate:refresh


資料庫的posts資料表將會增加user_id這個欄位



    //檔案:APP/user.php
public function post(){
return $this->hasOne('App\Post','user_id');
//user_id其實是預設值
}


//檔案:routes/web.php

use App\User;

Route::get('user/{id}/post', function($id) {
return User::find($id)->post;
});


瀏覽器輸入:public/user/1/post



出現他那篇文章的文章物件



(post後面加->title,會出現標題,加->content,會出現內容)



hasOne與hasMany就是在建立關聯



參考文件:https://laravel.com/docs/5.5/eloquent-relationships#one-to-one



return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
//user表中的id //post表中代表user的id


//tinker
$post->first()->user()->get()
//就會跑出post與user的關聯(此篇文章的作者)

=> Illuminate\Database\Eloquent\Collection {#736
all: [
App\User {#714
id: 1,
name: "熊熊",
email: "bear@gmail.com",
created_at: null,
updated_at: null,
},
],
}

 





 



//檔案:routes/web.php

use App\User;
use App\Post;

Route::get('post/{id}/user', function($id) {
return Post::find($id)->user->name;
});


	//檔案:app/post.php
public function user(){
return $this->belongsTo('App\User');
}


瀏覽器輸入:public/post/1/user



出現編號1的文章的作者





 



//檔案:routes/web.php

Route::get('/posts', function() {
$user = User::find(1); //先確定有User這個model被use

foreach ($user->posts as $post) {
echo $post->title . "<br>";
}
});


   //檔案:APP/user.php
public function posts(){
return $this->hasMany('App\Post');
}


瀏覽器輸入:public/posts



出現編號1會員的所有文章title





 



//下指令
php artisan make:model Role -m
//-m代表說會同時做migration


//下指令
php artisan make:migration create_users_roles_table --create=role_user
//新的名稱要放前面


    //檔案:database/migrations/日期_編號_create_roles_table.php
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}


    //檔案:database/migrations/日期_編號_create_users_roles_table.php
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('role_id');
$table->timestamps();
});
}


//下指令
php artisan migrate


//SQL,增加一些資料
INSERT INTO `roles` (`id`, `name`, `created_at`, `updated_at`)
VALUES (NULL, 'aministrator', NULL, NULL), (NULL, 'subscriber', NULL, NULL);


//SQL,讓使用者1是階級1(administor),使用者2是階級2(subscriber)
INSERT INTO `role_user` (`id`, `user_id`, `role_id`, `created_at`, `updated_at`) VALUES
(1, 1, 1, NULL, NULL),
(2, 2, 2, NULL, NULL);


    //檔案:APP/user.php
public function roles(){
return $this->belongsToMany('App\Role');
}


//檔案:routes/web.php

Route::get('/user/{id}/role', function($id) {
$user= User::find($id);
foreach ($user->roles as $role) {
return $role->name;
}
});


瀏覽器輸入:public/user/1/role



出現編號1會員的階級的名稱





 



//檔案:routes/web.php

Route::get('/user/{id}/role', function($id) {
$user= User::find($id)->roles()->get();
//選出這個user //找出他的階級
return $user;
});


瀏覽器輸入:public/user/1/role



[
{
id: 1,
name: "aministrator",
created_at: null,
updated_at: null,
pivot:
{
user_id: 1,
role_id: 1
}
}
]




    //檔案:APP/user.php
public function roles(){
// return $this->belongsToMany('App\Role','user_roles','user_id','role_id');
//若表格命名為role_user,則可以使用預設值,不用上面那行
return $this->belongsToMany('App\Role')->withPivot('created_at');
}


//檔案:app/role.php

class Role extends Model
{
public function users(){
return $this->belongsToMany('App\User');
}
}


//檔案:routes/web.php

Route::get('user/pivot', function() {
$user = User::find(1);
foreach ($user->roles as $role) {
return $role->pivot->created_at;
}
});


瀏覽器輸入:public/user/peviot



出現編號1這個會員被賦予階級1時的時間



沒有留言:

張貼留言