2017年9月27日 星期三

Laravel,relationshop2,country,photo


//下指令
php artisan make:model Country -m


//下指令
php artisan make:migration add_country_id_column_to_users --table=users


    //檔案:database/migrations/日期_編號_add_country_id_column_to_users.php
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('country_id');
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('country_id');
});
}


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


//下指令
php artisan migrate


//SQL,增加一些國家
INSERT INTO `countries` (`id`, `name`, `created_at`, `updated_at`) VALUES
(1, '台灣', NULL, NULL),
(2, '美國', NULL, NULL),
(3, '日本', NULL, NULL),
(4, '俄羅斯', NULL, NULL),
(5, '印度', NULL, NULL),
(6, '以色列', NULL, NULL),
(7, '加拿大', NULL, NULL),
(8, '英國', NULL, NULL);


    //檔案:app/country.php
public function posts(){
return $this->hasManyThrough('App\Post','App\User');
}


//檔案:routes/web.php

use App\Country;

Route::get('/user/country', function() {
$country=Country::find(1);
foreach ($country->posts as $post) {
return $post->title;
}
});


瀏覽器輸入:public/user/country



呈現出屬於編號1這個國家的使用者,他所發的文章





//下指令
php artisan make:model Photo -m


    //檔案:日期_編號_create_photos_table.php
public function up()
{
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->string('path');
$table->integer('imageable_id');
$table->string('imageable_type');
$table->timestamps();
});
}


//下指令
php artisan migrate


    //檔案: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


//增加一些資料
INSERT INTO `photos` (`id`, `path`, `imageable_id`, `imageable_type`, `created_at`, `updated_at`) VALUES
(1, 'bear.jpg', 1, 'App\\User', NULL, NULL),
(2, 'post1.jpg', 1, 'App\\Post', NULL, NULL);
INSERT INTO `posts` (`id`, `title`, `content`, `created_at`, `updated_at`, `deleted_at`, `is_admin`) VALUES
(1, '標題1', '內容1', NULL, NULL, NULL, 0),
(2, '標題2', '內容2', NULL, NULL, NULL, 0);

INSERT INTO `roles` (`id`, `name`, `created_at`, `updated_at`) VALUES
(1, '管理員', NULL, NULL),
(2, '訂閱者', NULL, NULL);
INSERT INTO `role_user` (`id`, `user_id`, `role_id`, `created_at`, `updated_at`) VALUES
(1, 1, 1, NULL, NULL),
(2, 2, 2, NULL, NULL);
INSERT INTO `users` (`id`, `name`, `email`, `password`, `remember_token`, `created_at`, `updated_at`, `country_id`) VALUES
(1, '熊熊', 'bear@gmail.com', 'bear', NULL, NULL, NULL, 0),
(2, '阿福', 'fu@gmail.com', 'fu', NULL, NULL, NULL, 0);


	//檔案:app/photo.php
public function imageable(){

return $this->morphTo();
}


	//檔案:app/post.php
public function photos(){
return $this->morphMany('App\Photo','imageable');
}


    //檔案:app/user.php
public function photos(){
return $this->morphMany('App\Photo','imageable');
}


//檔案:routes/web.php

Route::get('user/photos', function() {
$user = User::find(1);

foreach ($user->photos as $photo) {
return $photo;
}
});


瀏覽器輸入:public/user/photos



呈現出編號1會員對應到的photo資料表的那行資料的物件



//檔案:routes/web.php

Route::get('post/photos', function() {
$post = Post::find(1);

foreach ($post->photos as $photo) {
return $photo;
}
});


瀏覽器輸入:public/post/photos



呈現出編號1貼文對應到的photo資料表的那行資料的物件



//再加一張照片
INSERT INTO `photos` (`id`, `path`, `imageable_id`, `imageable_type`, `created_at`, `updated_at`)
VALUES (NULL, 'php.jpg', '1', 'App\\Post', NULL, NULL);


//檔案:routes/web.php

Route::get('post/photos', function() {
$post = Post::find(1);

foreach ($post->photos as $photo) {
echo $photo->path . "<br>";
}
});


瀏覽器輸入:public/post/photos



呈現出編號1貼文對應到的photo資料表的所有照片



(也可以從網址傳入參數至find 這個method)



//檔案:routes/web.php

use App\Photo;

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

$photo=Photo::findOrFail($id);
return $photo->imageable;
});


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



呈現出編號1照片對應到的資料表的那列資料內容(會員或貼文資料)



沒有留言:

張貼留言