php artisan make:migration create_comments_table
編輯 /database/migrations/
剛剛新增的 migration file
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id')->unsigned()->comment ('主 Key');
$table->text('content')->comment ('內容');
$table->integer('article_id')->unsigned ();
$table->foreign('article_id')->references('id')->on('articles');
$table->timestamps();
});
}
public function down()
{
Schema::drop('comments');
}
php artisan migrate
php artisan make:controller ArticleCommentsController
php artisan make:model Comment
/app/Http/routes.php
Route::resource('articles.comments', 'ArticleCommentsController');
link_to_route()
遇到兩個變數狀況時,第三個參數則可放 arrayMethod | url | 行為 | route 名稱 |
---|---|---|---|
GET | /articles/{article id}/comments | index | articles.comments.index |
GET | /articles/{article id}/comments/create | create | articles.comments.create |
POST | /articles/{article id}/comments/ | create post | articles.comments.store |
GET | /articles/{article id}/comments/{comment id} | show | articles.comments.show |
GET | /articles/{article id}/comments/{comment id}/edit | edit | articles.comments.edit |
PUT | /articles/{article id}/comments/{comment id} | edit post | articles.comments.update |
DELETE | /articles/{article id}/comments/{comment id} | destroy | articles.comments.destroy |
程式碼範例:OA's practice laravel5 Nested CRUD
相關參考: