laravel Model 라라벨 모델 관련

모델 파일 기본형


<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
// protected $table ='my_task_table'; //없을 경우 Class 명이 변경되서 사용됨 (주의: 중간에 대문자가 있으면 '_소문자' 처럼 변경됨)
// protected $conntection = 'sqlite'; //config/database.php 속의 커넥션 설정
// protected $primaryKey ='task_id'; //없을 경우 id 가 기본값
// public $timestamps = 'false'; //Carbon 클래스  사용유무, false 면 문자열로 처리함
// protected $dates =['due_date','assigned_date']; // Carbon 클래스 적용 필드명, created_at, updated_at 은 자동
// protected $dateFormat = 'Y-m-d H:i:s'; //날짜형태
// protected $fillable = ['name','project_id']; //insert, update 할 수 있는 필드를 설정한다.  ['*'] 으로 전체를 풀 수 있다. $garded 와 중복 사용 불가
->['*'] 는 안된다. 하고 싶으면 $guarded=[]; 를 대신 사용하자
// protected $guarded= ['description']; //insert, update 할 수 없는 필드를 설정한다. $fillable  와 중복 사용 불가
}

모델 사용법

use App\Task;

$tasks = Task::all();
// => select * from tasks;
$task = Task::find($id); //값이 없으면 null
$task = Task::findOrFail($id);  //값이 없으면 예외 발생(에러)
// => select * from tasks where id='id';

$task = Task::where('field','=','val')->orderBy('field','desc')->take(3)->skip(2); 
//$task->toSql() //select * from `tasks` where `field` = ? order by `field` desc limit 3 offset 2
//$task->getBindings() //array:1 [ 0 => "val"]





escape 방법

DB::connection()->getPdo()->quote("string to quote");
될 수 있으면 bind 하라

raw 쿼리

$results = DB::select('select * from users where id = :id', ['id' => 1]); 
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
$affected = DB::update('update users set votes = 100 where name = ?', ['John']);
$deleted = DB::delete('delete from users');
DB::statement('drop table users');

쿼리로그

버전별로 사용법 차이가 크다!

$task = new Task;
$task->getConnection()->enableQueryLog();
print_r($task->getConnection()->getQueryLog());
Array
(
    [0] => Array
        (
            [query] => insert into `tasks` (`name`, `project_id`, `updated_at`, `created_at`) values (?, ?, ?, ?)
            [bindings] => Array
                (
                    [0] => 예제 작성
                    [1] => 45
                    [2] => 2019-01-07 15:00:57
                    [3] => 2019-01-07 15:00:57
                )

            [time] => 6.16
        )

    [1] => Array
        (
            [query] => update `tasks` set `name` = ?, `updated_at` = ? where `id` = ?
            [bindings] => Array
                (
                    [0] => 예제 수정
                    [1] => 2019-01-07 15:00:59
                    [2] => 25
                )

            [time] => 4.7
        )



soft deletes

namespace App;

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

class Flight extends Model
{
    use SoftDeletes;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}
댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
제목 작성자 날짜
공대여자
공대여자
mins01
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자