Eloquent get()

use App\Post;
Route::get('/findwhere', function(){
	$posts = Post::where('id', 1)->orderBy('id', 'desc')->take(1)->get();
	return  $posts;

});

[{“id”:1,”title”:”Update tile”,”content”:”Laravel is the best thing that happen to PHP”,”created_at”:null,”updated_at”:null,”is_admin”:0}]

確かにraw queryのselect * fromよりもeasy to understand
DB::select(‘select * from posts where id = ?’, [1]);

FindOrFail()とも書ける

Route::get('/findmore', function(){

	$posts = Post::findOrFail(4);
	return $posts;

});

eloquentって何?

Laravelのドキュメントを読んでいると、eloquentってワードが何度も出てきます。
なんとなく雰囲気でわかったような気でいましたが、eloquentって何でしょうか?
tinker・controllerでやっている以下のようなことでしょうか?

$post = new Post();
$post->title = $request->title;
$post->body = $request->body;
$post->save();

Model:model is a class that deal with database

モデルの作成
$ php artisan make:model Post
Model created successfully.

appフォルダにPost.phpが作られる
eloquentとは下のコードからもわかる様に、Modelのclass

databaseのtable nameがpostsなら、model nameはPost

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //
    posts 
}
use App\Post;
Route::get('/read', function(){

	$posts = Post::all();

	foreach($posts as $post){
		return $post->title;
	}
});

update & delete

update with raw query

Route::get('update', function(){
	$update = DB::update('update posts set title ="Update tile" where id = ?',[1]);

	return $update;
});

mysql> select * from posts;
+—-+——————+———————————————-+————+————+———-+
| id | title | content | created_at | updated_at | is_admin |
+—-+——————+———————————————-+————+————+———-+
| 1 | Update tile | Laravel is the best thing that happen to PHP | NULL | NULL | 0 |
| 2 | php with laravel | Laravel is the best thing that happen to PHP | NULL | NULL | 0 |
+—-+——————+———————————————-+————+————+———-+
2 rows in set (0.00 sec)

Route::get('delete', function(){
	$delete = DB::delete('delete from posts where id = ?', [2]);
	return $delete;
});

mysql> select * from posts;
+—-+————-+———————————————-+————+————+———-+
| id | title | content | created_at | updated_at | is_admin |
+—-+————-+———————————————-+————+————+———-+
| 1 | Update tile | Laravel is the best thing that happen to PHP | NULL | NULL | 0 |
+—-+————-+———————————————-+————+————+———-+
1 row in set (0.00 sec)

absolutely I godit.

Raw SQL Queryでselect * from

DB classでselectして$resultに格納する
あれ、exceptionになる。。

Route::get('/read', function(){
	$result = DB::select('select * from posts where id = ?', [1]);
	return $result->title;

});

あれ、これなら上手くいく

Route::get('/read', function(){

	$results = DB::select('select * from posts where id = ?', [1]);

	foreach($results as $post){
		return $post->title;
	}

});

debugしながらやらないと駄目のようです。

Route::get('/read', function(){
	$results = DB::select('select * from posts where id = ?', [1]);
	return $results;
});

[{“id”:1,”title”:”php with laravel”,”content”:”Laravel is the best thing that happen to PHP”,”created_at”:null,”updated_at”:null,”is_admin”:0}]

ここまでくれば、update, deleteの想像がつきます。

save();ではなく、DB::insertでinsert

Raw SQL Query

Route::get('/insert', function(){

	DB::insert('insert into posts(title, content) values(?, ?)', ['php with laravel', 'Laravel is the best thing that happen to PHP']);
});

mysql> select * from posts;
+—-+——————+———————————————-+————+————+———-+
| id | title | content | created_at | updated_at | is_admin |
+—-+——————+———————————————-+————+————+———-+
| 1 | php with laravel | Laravel is the best thing that happen to PHP | NULL | NULL | 0 |
+—-+——————+———————————————-+————+————+———-+
1 row in set (0.00 sec)

以下のようにclassに対して値を入れてsave();とする方法とは異なる

public function store(Request $request){
    	$post = new Post();
    	$post->title = $request->title;
    	$post->body = $request->body;
    	$post->save();
    	return redirect('/');
    }

sqlに慣れていると、Raw SQL Queryの方が馴染みがあります。