라라벨, laravel 로 샘플 만들어 보자. (1) - Migration,Controller,Route
마이그레이션
$ php artisan make:migration simple_comment
Created Migration: 2019_01_09_050609_simple_comment
file : database/migrations/2019_01_09_050609_simple_comment.php<br></br>class SimpleComment extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('simple_comments', function (Blueprint $table) {
$table->increments('sc_id');
$table->string('sc_name',50);
$table->string('sc_pass',255);
$table->text('sc_comment');
$table->timestamps();
// $table->timestamp('created_at');
// $table->timestamp('updated_at');
$table->timestamp('deleted_at')->nullable();
$table->rememberToken();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('simple_comments');
}
}
$ php artisan migrate
Migrating: 2019_01_09_050609_simple_comment
Migrated: 2019_01_09_050609_simple_comment
DB TABLE
Field Type Collation Null Key Default Extra Privileges Comment
-------------- ---------------- ------------------ ------ ------ ------- -------------- ------------------------------- ---------
sc_id int(10) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
sc_name varchar(50) utf8mb4_unicode_ci NO (NULL) select,insert,update,references
sc_pass varchar(255) utf8mb4_unicode_ci NO (NULL) select,insert,update,references
sc_comment text utf8mb4_unicode_ci NO (NULL) select,insert,update,references
created_at timestamp (NULL) YES (NULL) select,insert,update,references
updated_at timestamp (NULL) YES (NULL) select,insert,update,references
deleted_at timestamp (NULL) YES (NULL) select,insert,update,references
remember_token varchar(100) utf8mb4_unicode_ci YES (NULL) select,insert,update,references
Model 생성
$ php artisan make:model SimpleComment
Model created successfully.
file : app/SimpleComment.php
namespace App;
<br></br>
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
<br></br>
class SimpleComment extends Model
{
use SoftDeletes;
protected $primaryKey ='sc_id'; //없을 경우 id 가 기본값
protected $dates = ['deleted_at'];
}
Controller 생성 (resource규칙 적용)
$ php artisan make:controller SimpleCommentController -m SimpleComment -r --api
Controller created successfully.
Route 적용
routes/api.php
Route::resource('sc', 'SimpleCommentController');
$ php artisan route:list
+--------+-----------+------------------+------------+------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------+------------+------------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/sc | sc.index | App\Http\Controllers\SimpleCommentController@index | api |
| | POST | api/sc | sc.store | App\Http\Controllers\SimpleCommentController@store | api |
| | GET|HEAD | api/sc/create | sc.create | App\Http\Controllers\SimpleCommentController@create | api |
| | GET|HEAD | api/sc/{sc} | sc.show | App\Http\Controllers\SimpleCommentController@show | api |
| | PUT|PATCH | api/sc/{sc} | sc.update | App\Http\Controllers\SimpleCommentController@update | api |
| | DELETE | api/sc/{sc} | sc.destroy | App\Http\Controllers\SimpleCommentController@destroy | api |
| | GET|HEAD | api/sc/{sc}/edit | sc.edit | App\Http\Controllers\SimpleCommentController@edit | api |
| | GET|HEAD | api/user | | Closure | api,auth:api |
+--------+-----------+------------------+------------+------------------------------------------------------+--------------+