There is a magical place called boot() in an Eloquent model where you can override default behavior:
1class User extends Model 2{ 3 public static function boot() 4 { 5 parent::boot(); 6 static::updating(function($model) 7 { 8 // do some logging 9 // override some property like $model->something = transform($something);10 });11 }12}
Probably one of the most popular examples is setting some field value at the moment of creating the model object. Let’s say you want to generate UUID field at that moment.
1public static function boot()2{3 parent::boot();4 self::creating(function ($model) {5 $model->uuid = (string)Uuid::generate();6 });7}
Eloquent has quite a few functions that combine two methods, like “please do X, otherwise do Y”. Example 1 – findOrFail(): Instead of:
1$user = User::find($id);2if (!$user) { abort (404); }
Do this:
1$user = User::findOrFail($id);
Example 2 – firstOrCreate(): Instead of:
1$user = User::where('email', $email)->first();2if (!$user) {3 User::create([4 'email' => $email5 ]);6}
Do just this:
1$user = User::firstOrCreate(['email' => $email]);
Instead of this:
1$article = Article::find($article_id);2$article->read_count++;3$article->save();
You can do this:
1$article = Article::find($article_id);2$article->increment('read_count');
Also these will work:
1Article::find($article_id)->increment('read_count');2Article::find($article_id)->increment('read_count', 10); // +103Product::find($produce_id)->decrement('stock'); // -1
What if you want to have User::all() always be ordered by name field? You can assign a global scope. Let’s go back to the boot() method, which we mentioned already above.
1protected static function boot()2{3 parent::boot();45 // Order by name ASC6 static::addGlobalScope('order', function (Builder $builder) {7 $builder->orderBy('name', 'asc');8 });9}
Read more about Query Scopes here.
If you use the Office 365 Business or Office 365 Enterprise editions, you can connect Outlook to over 750 other apps and streamline even more email workflows. For example, you can send new emails you get from a specific sender to Slack or create a new Trello card for new emails sent to a specific Outlook folder. See all the Office 365 integrations available on Zapier or use one of the workflow templates ("Zaps") to get started right away:
If you’ve had success with the previous Outlook tips and are interested in getting into more advanced email management, it’s time to take a look at Quick Steps. Quick Steps allow you to automatically perform long strings of actions based on a trigger. Say at the end of the month you receive a series of invoices from vendors or contractors. Your normal process is to review the invoices, forward the invoices to your accountant, and create a reminder to follow up with the accountant a week later to make sure the invoices were paid. Quick Steps can take care of the last two steps for you with fewer clicks than if you performed the tasks manually:
If you spend a lot of time typing the same answers to questions you get repeatedly, create Quick Parts to insert that text into email replies in just a few clicks. No need to keep typing the same thing over and over again or having to find an old response to copy and paste. When you’ve finished typing a response that you expect to need to send again, follow the steps below to save the text as a Quick Part:
Unfortunately, not all emails in the workplace come with an unsubscribe link. Day after day, emails arrive in your inbox that are just a distraction. Some common examples include:
Taking time off is wonderful until the moment you open your email after returning to work. If you work in an email-heavy company, you may have received hundreds of emails while you were out of the office, and now you’re faced with the difficult task of sorting through each one. With Outlook’s clean-up function (introduced in Outlook 2010), you can drastically reduce the number of emails you have to deal with in just a few clicks—whether you're coming back from vacation or you just have too many emails piled up. The clean-up function removes all email replies that are duplicated in a later thread, allowing you to read a single thread instead of dozens of individual emails. To clean up your inbox quickly:
Some emails don’t require an immediate response, but they do need to be handled before the time you have scheduled to review items in your to-do folder. These items can still be moved to your to-do folder so they’re not clogging your inbox, but you’ll want to make sure that you have a reminder to take care of them before a deadline. Create a reminder by adding these emails to Outlook’s task list: