“Astuces éloquentes de Laravel” Réponses codées

Astuces éloquentes de Laravel

public function author()
{
    return $this->belongsTo(Author::class)->withDefault();
}
Unsightly Unicorn

Astuces éloquentes de Laravel

// whereRaw
$orders = DB::table('posts')
    ->whereRaw('COUNT(views) > ?', [200])
    ->get();
Unsightly Unicorn

Astuces éloquentes de Laravel


$post = Post::find($id);
$post->views++;
$post->save();
Unsightly Unicorn

Astuces éloquentes de Laravel

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

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

$user->getOriginal('title');    // Something title
Unsightly Unicorn

Astuces éloquentes de Laravel

public function author()
{
    return $this->belongsTo(Author::class)->withDefault([
        'name' => 'Someone'
    ]);
}
Unsightly Unicorn

Astuces éloquentes de Laravel

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

if($post->wasRecentlyCreated) {
  // do something
}
Unsightly Unicorn

Astuces éloquentes de Laravel

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

// or you can decrease using
$post = Post::find($id);
$post->decrement('views');
Unsightly Unicorn

Réponses similaires à “Astuces éloquentes de Laravel”

Questions similaires à “Astuces éloquentes de Laravel”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code