Posts

Showing posts from June, 2022

Notifiable trait trong Laravel

Image
khi 1 class sử dụng Notifiable , tức là class đó có thể gọi method  notify Ví dụ như sau :  use App\Notifications\ InvoicePaid ;   $user -> notify ( new InvoicePaid ( $invoice )); Trong ví dụ trên InvoicePaid là 1 class được khai báo ở App\Notifications Trong Laravel, notification có thể gửi dưới dạng Channel gồm Mail Slack Database Telegram ....  

Laravel query builder conditional-clauses

Image
 

Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean?

 Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean? With with()  is for  eager loading . That basically means, along the main model, Laravel will preload the relationship(s) you specify.  With đi kèm với khái niệm eager loading, tức là đi kèm với main model, Laravel sẽ load thêm dữ liệu của model có quan hệ với model chính. Ví dụ User > hasMany > Post $users = User :: with ( 'posts' )-> get (); foreach ( $users as $user ){ $users ->post; // posts is already loaded and no additional DB query is run } Has If you just use   has('relation')   that means you only want to get the models that have at least one related model in this relation. --> là 1 dạng filter (sử dụng where), khi muốn lấy các model chính có ít nhất 1 model con User > hasMany > Post $users = User :: has ( 'posts' )-> get (); // only users that have at least one post are contained in the collection   WhereHas whereHas()  wor...