Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Eloquent Scope Classes

Eloquent Scope Classes

Presentation from the Laravel Omaha Meetup

January 18th, 2018

Jordan Hoff

Avatar for Jordan Hoff

Jordan Hoff

January 18, 2018

More Decks by Jordan Hoff

Other Decks in Programming

Transcript

  1. Initial request Veterinary hospital needs a backend system to track

    business Users ( employees ) tied to authentication Customers Animals Invoices, Appointments, etc
  2. Then, the plan changes The client decides that they want

    more Rebuild their external website Allow customers to login Give customers access to see invoices, schedule appointments, etc
  3. Eloquent Scope Classes Create a base model with it’s own

    table Extend base model for each “type” and apply a Global Query Scope Scope classes will behave like an isolated model
  4. Things to consider Eloquent is smart. Maybe too smart. Table

    names are automatically assumed based on the current class name This applies to not only the model, but relationships as well
  5. Add a global scope Override the boot method to register

    a scope that will differentiate Remember to call the parent boot method!
  6. Gotcha! Relationships defined on the base class may need tables

    and keys explicitly defined in order to work properly when called from a scope class context