Answers for "Laravel eloquent tricks"

PHP
0

Laravel eloquent tricks

public function author()
{
    return $this->belongsTo(Author::class)->withDefault();
}
Posted by: Guest on September-27-2021
0

Laravel eloquent tricks

$post = Post::create($attributes);

if($post->wasRecentlyCreated) {
  // do something
}
Posted by: Guest on September-27-2021
0

Laravel eloquent tricks

public function author()
{
    return $this->belongsTo(Author::class)->withDefault([
        'name' => 'Someone'
    ]);
}
Posted by: Guest on September-27-2021
0

Laravel eloquent tricks

$post = Post::find($id);
$post->views++;
$post->save();
Posted by: Guest on September-27-2021
0

Laravel eloquent tricks

$post = Post::first();
$post->title;                   // Something title

$post->title = "new title";    // new title

$user->getOriginal('title');    // Something title
Posted by: Guest on September-27-2021
0

Laravel eloquent tricks

// whereRaw
$orders = DB::table('posts')
    ->whereRaw('COUNT(views) > ?', [200])
    ->get();
Posted by: Guest on September-27-2021
0

Laravel eloquent tricks

// you can increase using
$post = Post::find($id);
$post->increment('views');

// or you can decrease using
$post = Post::find($id);
$post->decrement('views');
Posted by: Guest on September-27-2021

Browse Popular Code Answers by Language