公司導入 Laravel 分享 - 5. 資料庫
目錄
一、設定
二、Migration
1. 建立 migration
2. 執行 migration
3. 還原 migration
三、Query Builder
1. 取得結果
2. select
3. join
inner join
left join
4. where
5. 排序
6. Group
7. 其他
四、分頁
1. 顯示
一、設定
二、Migration
1. 建立 migration
2. 執行 migration
3. 還原 migration
三、Query Builder
1. 取得結果
2. select
3. join
inner join
left join
4. where
5. 排序
6. Group
7. 其他
四、分頁
1. 顯示
一、設定
在 config/database.php 檔案裡會看到很多不同的資料庫連線資訊。如果是要配合我們公司的系統資料庫,這裡要將編碼改成 utf8,因為公司的系統資料庫不支援 utf8mb4。
- 'mysql' => [
- 'driver' => 'mysql',
- 'host' => env('DB_HOST', '127.0.0.1'),
- 'port' => env('DB_PORT', '3306'),
- 'database' => env('DB_DATABASE', 'forge'),
- 'username' => env('DB_USERNAME', 'forge'),
- 'password' => env('DB_PASSWORD', ''),
- 'unix_socket' => env('DB_SOCKET', ''),
- 'charset' => 'utf8mb4',
- 'collation' => 'utf8mb4_unicode_ci',
- 'prefix' => '',
- 'strict' => true,
- 'engine' => null,
- ],
在 config/database.php 裡頭有看到一些寫著類似 env('DB_HOST', '127.0.0.1') 的語法,之前也有說過 env 會去抓網站的環境設定檔 .env 裡面的值,如果沒有的話就帶後面的預設值,所以我們需要在 .env 檔裡面去設定資料庫的相關資訊。
一般來說 .env 這個檔案不會進版本控制系統。
- DB_CONNECTION=mysql
- DB_HOST=127.0.0.1
- DB_PORT=3306
- DB_DATABASE=my_web_database
- DB_USERNAME=root
- DB_PASSWORD=12345678
二、Migration
這是一個資料庫版本控制的機制,只針對「結構」的部分,而不是「資料」的部分。1. 建立 migration
php artisan make:migration create_posts_table --create=post這時我們可以在 /database/migrations 看到 2017_05_08_080254_create_posts_table.php 這樣子的檔案。
up() 是版本發布時會 run 的指令,down() 則是還原時會 run 的指令。
中間內容的部分這裡只列出幾種常見的,其他詳細內容請自行參考:https://laravel.com/docs/5.4/migrations#tables。
- <?php
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class CreatePostsTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('posts', function (Blueprint $table) {
- $table->increments('id');
- $table->string('title');
- $table->text('content');
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('posts');
- }
- }
語法 | 說明 |
---|---|
$table->increments('id'); | 一個遞增的 id 欄位(主鍵) |
$table->string('title'); | VARCHAR |
$table->text('content'); | TEXT |
$table->integer('votes'); | INT |
$table->timestamps(); | 會變成兩個 TIMESTAMP 欄位,分別是 created_at 和 updated_at |
2. 執行 migration
php artisan migrate
第一次執行之後會在資料庫中建立一個 migrations 資料表來記錄版本。
3. 還原 migration
php artisan migrate:rollback
三、Query Builder
這裡列出一些常用的,主要是針對查詢的部分說明,還有很多這裡沒有列,如果需要請查詢:https://laravel.com/docs/5.4/queries。1. 取得結果
get() 是取得結果的方法,例如:Post::where('id', '>', 3)->get()如果不需要其他 sql 語法單純的取得整張資料表的資料也可以用 all(),例如:
Post::all();如果是要第一筆資料也可以使用 first(),例如:
Post::where('id', '>', 3)->first();另外,也可以使用 count、max、min、avg、sum 取得結果。
$price = Order::where('finalized', 1)->avg('price');
2. select
select 是 select 欄位。$posts = Post::select('title', 'content as post_content')->get();
3. join
inner join
$posts = Post::join('categories', 'posts.category_id', '=', 'categories.id')->get();
left join
$posts = Post::leftJoin('categories', 'posts.category_id', '=', 'categories.id')->get();
4. where
$posts = Post::where('id', 5)->get();取得 id = 5 的文章。
$posts = Post::where('id', '>', 5)->get();取得 id > 5 的文章。
$posts = Post::where('title', 'like', "%學測%")->get();取得標題有「學測」的文章。
5. 排序
$posts = Post::where('id', '>', 5)->orderBy('id', 'desc')->get();
6. Group
$posts = Post::where('id', '>', 5)->groupBy('user_id')->get();
7. 其他
如果這些都沒辦法組成我們想要的 SQL,這時我們可以使用 DB::raw('這裡放語法') 來達成。四、分頁
一頁 15 筆資料$posts = Post::where('title', 'like', "%學測%")->paginate(15);
1. 顯示
如果我們有其他的查詢條件
- <div class="container">
- @foreach ($posts as $post)
- {{ $post->title }}
- @endforeach
- </div>
- {{ $posts->links() }}
{{ $posts->appends(['title' => '學測'])->links() }}
留言
張貼留言