728x90

라라벨의 기능중에서 DB관련 파사드 지원해 줍니다.

SQL 삽입 과 같은 악의적 공격 원천 차단하기 위해서 파사드를 사용합니다.

이를 이용해서 실제 DB에 데이터를 넣고 테스트를 할 수 있습니다.

그중에서 tinker의 사용법을 알아보겠습니다


1. Mysql 유저 생성 및 DB 생성하기 (mysql DB서버에 접속할 로그인 정보를 만든다고 생각하세요)

hanui-MacBook-Pro:NationalPension hanchiro$ mysql -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 15

Server version: 5.7.16 MySQL Community Server (GPL)



Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.



Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.



Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.



 

 

mysql > use mysql;
mysql > create database posts;
-- posts 라는 데이터베이스를 만들었습니다.

mysql > create USER '유저' IDENTIFIED BY '비밀번호';
-- 유저로 '유저' 라는 아이디를 만들어서 '비밀번호'와 함께 생성해줍니다.

mysql > grant all privileges on posts.* to '유저';
-- posts 로 시작하는 DB에 대해서 posts라는 데이터베이스 를 찾아서 '유저'에 대해 권한을 부여합니다.

mysql > flush privileges;
-- 해당 설정값이 mysql 에 적용이 되도록 해줍니다.

mysql > quit;
-- 나옵니다.

 

mysql > use mysql;
mysql > create database posts;
mysql > create USER 'postsID' IDENTIFIED BY 'postsPASSWORD';
mysql > grant all privileges on posts.* to 'postsID';
mysql > flush privileges;
mysql > quit;

 

생성한 DB명 : posts

생성한 USER아이디 : postsID

생성한 PASSWORD : postsPASSWORD

 

를 만들어 뒀습니다.(꼭 수정해서 mysql DB 를 만들어 쓰세요 ^^)

그리고 이 DB 로그인 정보는 꼭 기억해 두세요.

 

 

2. posts 테이블 만들기

mysql  서버에 접속해서 posts DB에 접속한 다음에  "use posts"  로 posts DB로 이동합니다.

hanui-MacBook-Pro:NationalPension hanchiro$ mysql -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 15

Server version: 5.7.16 MySQL Community Server (GPL)



Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.



Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.



Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use posts

 

아래와 같이 posts  데이터베이스 안에 posts라는 테이블을 만들어 줍니다.

create table posts(
	id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	title varchar(255) NOT NULL default '',
	contents TEXT NOT NULL,
	regDate datetime NOT NULL
)ENGINE=InnoDB DEFAULT Charset=UTF8 Collate=utf8_unicode_ci;

 

 


3. laravel 설정파일 .env 파일에 파란박스 부분에 DB 를 생성할때의 내용을 입력 해줍니다.

1. 번에서 생성한

생성한 DB명 : posts

생성한 USER아이디 : postsID

생성한 PASSWORD : postsPASSWORD

를 .env 파일에 똑같은 쌍으로 입력 해주세요

 

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=생성한 DB명
DB_USERNAME=생성한 USER아이디
DB_PASSWORD=생성한 PASSWORD

 

 

 

 


4. php artisan tinker 사용하기!! 

hanui-MacBook-Pro:NationalPension hanchiro$ php artisan tinker

Psy Shell v0.9.12 (PHP 7.1.1 — cli) by Justin Hileman

 

DB::insert

>>> DB::insert('insert into posts(title,contents,regDate)values(?,?,now())', ['hello chiro','고마워 tinker 야']);

>>> DB::insert('insert into posts(title,contents,regDate)values(?,?,now())', ['잘지내고 있나요?','잘 지내고 있나요? 전 요즈음 영어공부를 하고 있어요']);

>>> DB::insert('insert into posts(title,contents,regDate)values(?,?,now())', ['how do you today?','how do you today? those day I have been study english']);

 

>>> DB::insert('insert into posts(title,contents,stDate)values(?,?,now()), ['hello chiro','고마워 tinker 야']);

PHP Parse error: Syntax error, unexpected T_STRING, expecting ')' on line 1

>>> DB::insert('insert into posts(title,contents,stDate)values(?,?,now()), ['hello chiro','고마워 tinker 야']');

PHP Parse error: Syntax error, unexpected T_STRING, expecting ')' on line 1

>>> DB::insert('insert into posts(title,contents,stDate)values(?,?,now())', ['hello chiro','고마워 tinker 야']);

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'stDate' in 'field list' (SQL: insert into posts(title,contents,stDate)values(hello chiro,고마워 tinker 야,now()))'


>>> DB::insert('insert into posts(title,contents,regDate)values(?,?,now())', ['hello chiro','고마워 tinker 야']);

=> true

>>> DB::table('posts')->get();

=> Illuminate\Support\Collection {#2910

     all: [

       {#2900

         +"id": 1,

         +"title": "hello chiro",

         +"contents": "고마워 tinker 야",

         +"regDate": "2020-02-12 16:12:02",

       },

     ],

   }


>>> DB::insert('insert into posts(title,contents,regDate)values(?,?,now())', ['잘지내고 있나요?','잘 지내고 있나요? 전 요즈음 영어공부를 하고 있어요']);

=> true

>>> DB::insert('insert into posts(title,contents,regDate)values(?,?,now())', ['how do you today?','how do you today? those day I have been study english']);

=> true

>>> DB::table('posts')->get();

=> Illuminate\Support\Collection {#2914

     all: [

       {#2913

         +"id": 1,

         +"title": "hello chiro",

         +"contents": "고마워 tinker 야",

         +"regDate": "2020-02-12 16:12:02",

       },

       {#2896

         +"id": 2,

         +"title": "잘지내고 있나요?",

         +"contents": "잘 지내고 있나요? 전 요즈음 영어공부를 하고 있어요",

         +"regDate": "2020-02-12 16:12:39",

       },

       {#2908

         +"id": 3,

         +"title": "how do you today?",

         +"contents": "how do you today? those day I have been study english",

         +"regDate": "2020-02-12 16:14:16",

       },

     ],

   }

 

DB::select

>>> $posts = DB::select('select * from posts');

>>> $posts[0]->title;

>>> $posts[1]->title;

>>> $posts[3]->title;

>>> $posts[2]->title;

>>> $posts = DB:select('select * from posts');

PHP Parse error: Syntax error, unexpected ':' on line 1

>>> $posts = DB::select('select * from posts');

=> [

     {#2915

       +"id": 1,

       +"title": "hello chiro",

       +"contents": "고마워 tinker 야",

       +"regDate": "2020-02-12 16:12:02",

     },

     {#2907

       +"id": 2,

       +"title": "잘지내고 있나요?",

       +"contents": "잘 지내고 있나요? 전 요즈음 영어공부를 하고 있어요",

       +"regDate": "2020-02-12 16:12:39",

     },

     {#2912

       +"id": 3,

       +"title": "how do you today?",

       +"contents": "how do you today? those day I have been study english",

       +"regDate": "2020-02-12 16:14:16",

     },

   ]

>>> $posts[0]->title;

=> "hello chiro"

>>> $posts[1]->title;

=> "잘지내고 있나요?"

>>> $posts[3]->title;

PHP Notice:  Undefined offset: 3 in Psy Shell code on line 1

>>> $posts[2]->title;

=> "how do you today?"

>>> $post = DB:selectOne('select * from posts');

PHP Parse error: Syntax error, unexpected ':' on line 1

 

DB::selectOne

>>> $post = DB::selectOne('select * from posts');

>>> $post = DB::selectOne('select * from posts');



=> {#2909

     +"id": 1,

     +"title": "hello chiro",

     +"contents": "고마워 tinker 야",

     +"regDate": "2020-02-12 16:12:02",

   }

>>> $post->title;

=> "hello chiro"

>>> 

 

DB::table('posts')->first();

>>> DB::table('posts')->last();

>>> DB::table('posts')->first();
=> {#2920
     +"id": 1,
     +"title": "hello chiro",
     +"contents": "고마워 tinker 야",
     +"regDate": "2020-02-12 16:12:02",
   }

 

DB::table('posts')->last(); 는 안되네요

>>> DB::table('posts')->last();

BadMethodCallException with message 'Call to undefined method Illuminate/Database/Query/Builder::last()'

 

 

 

 

>>> DB::table('posts')->find(); 는 안되네요

>>> DB::table('posts')->find();
TypeError: Too few arguments to function Illuminate/Database/Query/Builder::find(), 0 passed in Psy Shell code on line 1 and at least 1 expected

 

DB::table('posts')->find(2);

>>> DB::table('posts')->find(2);

=> {#2918

     +"id": 2,

     +"title": "잘지내고 있나요?",

     +"contents": "잘 지내고 있나요? 전 요즈음 영어공부를 하고 있어요",

     +"regDate": "2020-02-12 16:12:39",

   }

 

>>> DB::table('posts')->find(1,2); 안되요

>>> DB::table('posts')->find(1,2);

TypeError: Argument 1 passed to Illuminate/Database/Grammar::columnize() must be of the type array, integer given, called in /Users/hanchiro/dev/workspace_php/NationalPension/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 131

 

DB::table('posts')->find([1,2]); 이건 좀 이상하네요.

>>> DB::table('posts')->find([1,2]);

=> {#2914

     +"id": 1,

     +"title": "hello chiro",

     +"contents": "고마워 tinker 야",

     +"regDate": "2020-02-12 16:12:02",

   }

>>> DB::table('posts')->find([2,3]);

=> {#2920

     +"id": 2,

     +"title": "잘지내고 있나요?",

     +"contents": "잘 지내고 있나요? 전 요즈음 영어공부를 하고 있어요",

     +"regDate": "2020-02-12 16:12:39",

   }

 

DB::table('posts')->find([2]);

>>> DB::table('posts')->find([2]);

=> {#2900

     +"id": 2,

     +"title": "잘지내고 있나요?",

     +"contents": "잘 지내고 있나요? 전 요즈음 영어공부를 하고 있어요",

     +"regDate": "2020-02-12 16:12:39",

   }

 

DB::table('posts')->find([1-2]); --> null 이 나오네요

>>> DB::table('posts')->find([1-2]);

=> null

 

 

DB::table('posts')->where('id=1); 은 안되요

>>> DB::table('posts')->where('id=1);

PHP Parse error: Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 1

 

DB::table('posts')->where('id','=',1); 에서 ->get()을 해줘야 해요.

>>> DB::table('posts')->where('id','=',1);

=> Illuminate\Database\Query\Builder {#2924

     +connection: Illuminate\Database\MySqlConnection {#2904},

     +grammar: Illuminate\Database\Query\Grammars\MySqlGrammar {#2903},

     +processor: Illuminate\Database\Query\Processors\MySqlProcessor {#2902},

     +bindings: [

       "select" => [],

       "join" => [],

       "where" => [

         1,

       ],

       "having" => [],

       "order" => [],

       "union" => [],

     ],

     +aggregate: null,

     +columns: null,

     +distinct: false,

     +from: "posts",

     +joins: null,

     +wheres: [

       [

         "type" => "Basic",

         "column" => "id",

         "operator" => "=",

         "value" => 1,

         "boolean" => "and",

       ],

     ],

     +groups: null,

     +havings: null,

     +orders: null,

     +limit: null,

     +offset: null,

     +unions: null,

     +unionLimit: null,

     +unionOffset: null,

     +unionOrders: null,

     +lock: null,

     +operators: [

       "=",

       "<",

       ">",

       "<=",

       ">=",

       "<>",

       "!=",

       "<=>",

       "like",

       "like binary",

       "not like",

       "ilike",

       "&",

       "|",

       "^",

       "<<",

       ">>",

       "rlike",

       "regexp",

       "not regexp",

       "~",

       "~*",

       "!~",

       "!~*",

       "similar to",

       "not similar to",

       "not ilike",

       "~~*",

       "!~~*",

     ],

     +useWritePdo: false,

   }

 

 

DB::table('posts')->where('id','=',1)->get();

DB::table('posts')->where('id',1)->get();

DB::table('posts')->whereId('1')->get();

 

id가 1인 테이블 정보를 가져오는 똑같은 표현식입니다.

>>> DB::table('posts')->where('id','=',1)->get();

=> Illuminate\Support\Collection {#2932

     all: [

       {#2930

         +"id": 1,

         +"title": "hello chiro",

         +"contents": "고마워 tinker 야",

         +"regDate": "2020-02-12 16:12:02",

       },

     ],

   }

>>> DB::table('posts')->where('id',1)->get();

=> Illuminate\Support\Collection {#2919

     all: [

       {#2899

         +"id": 1,

         +"title": "hello chiro",

         +"contents": "고마워 tinker 야",

         +"regDate": "2020-02-12 16:12:02",

       },

     ],

   }


>>> DB::table('posts')->whereId('1')->get();

=> Illuminate\Support\Collection {#2924

     all: [

       {#2932

         +"id": 1,

         +"title": "hello chiro",

         +"contents": "고마워 tinker 야",

         +"regDate": "2020-02-12 16:12:02",

       },

     ],

   }

 

 

DB::table('posts')->where(function($query){ $query->whereId(1); })->get();

콜백 함수로 where 절에 대한 조건문을 입력할 수 있어요.

 

>>> DB::table('posts')->where(function($query){ $query->whereId(1); })->get();

=> Illuminate\Support\Collection {#2937

     all: [

       {#2936

         +"id": 1,

         +"title": "hello chiro",

         +"contents": "고마워 tinker 야",

         +"regDate": "2020-02-12 16:12:02",

       },

     ],

   }

>>> 

 

+ Recent posts