1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LaravelのクエリビルダとEloquent

Last updated at Posted at 2022-04-27

クエリビルダとEloquent

クエリビルダはSQLを作成し、実行します。
戻り値はDBの取得値をコレクションにしたものみたいです。

Eloquentの戻り値はEloquentモデルオブジェクトみたいです。

両者の書き方

⚫︎クエリビルダ書き方

FolderController.php
public function index()
    {
$folder = DB::table('folders')->where('id',1)->first();
        return $folder->id;
}

whereの書き方も参考にしてください。

⚫︎Eloquent

FolderController.php
public function index()
    {
     $folder = Folder::where("user_id", "=",11)->first();
     return $folder->id;
}

出力は両者ともです。

どういうデータが入っているのか

FolderController.php
public function index(){
$folders = Folder::get();//データを全取得
return view('folder.index', ['folders' => $folders]);
}
folder/index.blade.php
#都合により省略
            
{{-- <html> --}}
<head>
<title>フォルダ一覧</title>
</head>
 <table border = "1"> 
    <tr>
        <th>id</th>
        <th>user_id</th>
        <th>title</th>
        <th>created_at</th>
        <th>updated_at</th>
    </tr>
    
    @foreach($folders  as $folder) 
    <tr>
        <td>{{$folder->id }}</td>  
        <td>{{$folder->user_id}}</td>
        <td>{{$folder->title}}</td>
         <td>{{$folder->updated_at}}</td>
        <td>{{$folder->created_at}}</td> 
    </tr> 
      @endforeach
</table>

{{-- </html> --}}

スクリーンショット 2022-04-27 9.58.45.png

資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?