Eloquent ORM & Query Builder

Eloquent ORM is slower than Query Builder but we can make relationship with Models and easier to build & less code on the other side, the Query Builder is a little bit hard but more faster.

 

Eloquent ORM

Insert:

//Method 01

$article = new Article;

$article->user_id = Auth::user()->id;

$article->content = $request->content;

$article->live = (boolean)$request->live;

$article->post_on = $request->post_on;

$article->save();

//Method 02

Article::create($request->all());

At Model:

protected $fillable = [

'user_id', 'content', 'live', 'post_on'

];

N.B, protected $guarded = ['']; or protected $fillable [];

public function setLiveAttribute($value){

$this->attributes['live'] = (boolean)($value);

}

//Method 03

Article::create([

'user_id' => Auth::user()->id,

'content' =>$request->content,

'live' => $request->live,

'post_on' =>$request->post_on

]);

 

Select:

//All Records

Method 01:

$articles = Article::all();

return view('articles.index', compact('articles'));

Method 02:

$articles = Article::whereLive(1)->get();

$return articles;

//Paginated Records

$articles = Article::paginate(10);

return view('articles.index', compact('articles'));

//With soft deleted records

$articles = Article::withTrashed()->paginate(10);

return view('articles.index', compact('articles'));

//Only soft deleted records

$articles = Article::onlyTrashed()->paginate(10);

return view('articles.index', compact('articles'));

//Find By ID

$article = Article::findOrFail($id);

return $article;

N.B, If not found it will fail, hence we can create an error page at views/errors/404.blade.php

Update:

$article = Article::findOrFail($id);

$article->update($request->all());

Delete:

//Deleting

$article = Article::findOfFail($id);

$article->delete();

return redirect('/articles');

//OR

Article::destroy($id);

return redirect('/articles');

//For Multiple Deleting

Article::destroy([1,2,3,4]);

return redirect('/articles');

//Soft deleting

$article = Article::findOfFail($id);

$article->delete();

return redirect('/articles');

 

N.B, For soft deleting we need an extra field to our table called deleted_at. Hence we have to go to the model and import the class like this: use Illuminate\Database\Eloquent\SoftDeletes; and finally we have to add an attribute and trait inside the model like this: use SoftDeletes; protected $dates = ['post_on','deleted_at']; And then we have to go to the migration file and add like this: $table->softDeletes(); and we have to refresh like this: php artisan migrate:refresh

 

//Force deleting or Hard deleting

$article = Article::findOrFail($id);

$article->forceDelete();

return redirect('/articles');

 

Query Builder

Insert:

Method 01:

DB::table('articles')->insert($request->all());

Method 02: All but except _token field

DB::table('articles')->insert($request->except('_token'));

Method 03:

DB::table('articles')→insert([

'user_id' => Auth::user()->id,

'content' =>$request->content,

'live' => $request->live,

'post_on' =>$request->post_on

]);

N.B, Query Builder don’t support accessor and mutator.

Select:

Method 01:

$articles = DB::table('articles')->get();

dd($article);

 

Method 02:

$articles = DB::table('articles')->whereLive(1)->get();

dd($article);

Method 03: Only one article return

$article = Article::table('articles')->whereLive(1)->first();

dd($article);

  • 249
  • 308
  • By Bablu Ahmed
  • Posted 11 months ago