laravel eloquent tricks

Solutions on MaxInterview for laravel eloquent tricks by the best coders in the world

showing results for - "laravel eloquent tricks"
Johanna
31 Jan 2020
1
2$post = Post::find($id);
3$post->views++;
4$post->save();
5
Sara
27 May 2018
1$post = Post::first();
2$post->title;                   // Something title
3
4$post->title = "new title";    // new title
5
6$user->getOriginal('title');    // Something title
Gianluca
08 Sep 2017
1// whereRaw
2$orders = DB::table('posts')
3    ->whereRaw('COUNT(views) > ?', [200])
4    ->get();
Luciana
19 Apr 2020
1// you can increase using
2$post = Post::find($id);
3$post->increment('views');
4
5// or you can decrease using
6$post = Post::find($id);
7$post->decrement('views');
8
Manuel
05 Jun 2016
1public function author()
2{
3    return $this->belongsTo(Author::class)->withDefault();
4}
Niko
30 Jul 2020
1$post = Post::create($attributes);
2
3if($post->wasRecentlyCreated) {
4  // do something
5}
Ariadne
12 Jan 2017
1public function author()
2{
3    return $this->belongsTo(Author::class)->withDefault([
4        'name' => 'Someone'
5    ]);
6}
similar questions
queries leading to this page
laravel eloquent tricks