라라벨, laravel 로 샘플 만들어 보자. (3) - model, seeder

model 파일 수정

app\SimpleComment.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Hash;


class SimpleComment extends Model
{
use SoftDeletes;
protected $primaryKey ='sc_id'; //없을 경우 id 가 기본값
protected $dates = ['deleted_at'];
protected $fillable=['sc_name','sc_comment',];
//-- 자동 암호화
public function setScPassAttribute($value)
{
// $this->attributes['sc_pass'] = \Crypt::encrypt($value); //양방향 암호화
$this->attributes['sc_pass'] = self::hash($vaule); //해쉬
}
// public function getScPassAttribute($value)
// {
// return \Crypt::decrypt($value); //양방향 복호화
// }
public static function hash($v){
return Hash::make($v);
}
public function checkScPass($v){
return Hash::check($v, $this->attributes['sc_pass']);
}
public static function lists(){
return self::orderBy('sc_id','desc')->get();
}
}



seeder 생성

$ php artisan make:seeder SimpleCommentSeeder
Seeder created successfully.
database\seeds\SimpleCommentSeeder.php
<?php

use Illuminate\Database\Seeder;

class SimpleCommentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      $sc = new App\SimpleComment();
      for($i=0,$m=20;$i<$m;$i++){
        $t = str_repeat(rand(0,9), 4);
        $sc->create([
          // 'sc_id'=>null,
          'sc_name'=>$t,
          'sc_pass'=>$t,
          'sc_comment'=>md5(rand(1,9))." ".md5(rand(1,9)),
        ]);
      }
    }
}
database\seeds\DatabaseSeeder.php
<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
  /**
  * Seed the application's database.
  *
  * @return void
  */
  public function run()
  {
    // $this->call(UsersTableSeeder::class);
    $this->call(SimpleCommentSeeder::class); //<-- 추가
  }
}

$ php artisan db:seed
Seeding: SimpleCommentSeeder
Database seeding completed successfully.


SELECT COUNT(*) FROM `simple_comments`;
=>
count(*)  
----------
        20

SELECT * FROM `simple_comments` ORDER BY sc_id DESC  LIMIT 3;
=>
 sc_id  sc_name  sc_pass                                                       sc_comment                                                                  created_at           updated_at  deleted_at  remember_token  
------  -------  ------------------------------------------------------------  -----------------------------------------------------------------  -------------------  -------------------  ----------  ----------------
    20  7777     $2y$10$FyKTT.7oaPEAFpgMJyGU5egWz8IBhIrWE/AqfMPkuNMuhOjh2qFM.  eccbc87e4b5ce2fe28308fd9f2a7baf3 c81e728d9d4c2f636f067f89cc14862c  2019-01-09 06:48:20  2019-01-09 06:48:20      (NULL)  (NULL)          
    19  4444     $2y$10$avpMbRS/cBxnxupZR7iIrOU3nz3PTWaESJyeHICCh8ZXy49s/IrHS  c4ca4238a0b923820dcc509a6f75849b 45c48cce2e2d7fbdea1afc51c7c6ad26  2019-01-09 06:48:20  2019-01-09 06:48:20      (NULL)  (NULL)          
    18  7777     $2y$10$cyva/pIVnpKe/Uu6qYM0teo9tuIQ6WEFbHpu9wZXMZ5uB41866tea  8f14e45fceea167a5a36dedd4bea2543 45c48cce2e2d7fbdea1afc51c7c6ad26  2019-01-09 06:48:20  2019-01-09 06:48:20      (NULL)  (NULL)          
                                                                                                                                                                                                                        


$ php artisan make:controller WebSimpleCommentController
Controller created successfully.

댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -