라라벨, laravel 로 샘플 만들어 보자. (1) - Migration,Controller,Route

2

마이그레이션

$ php artisan make:migration simple_comment
Created Migration: 2019_01_09_050609_simple_comment
file : database/migrations/2019_01_09_050609_simple_comment.php
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;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

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 |
+--------+-----------+------------------+------------+------------------------------------------------------+--------------+



댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📷 갤러리형
제목
[기본형] HTML (with 부트스트랩5.3 , jquery 3.7, vue.js)
유용한 리눅스(LINUX) 명령어
[공지] 기술 게시판
4.28
4.29
4.30
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14
5.15
5.16
5.17
5.18
5.19
5.20
5.21
5.22
5.23
5.24
5.25
5.26
5.27
5.28
5.29
5.30
5.31
6.1