Trucks tips

Model boot() method

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}

XorY methods

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]);

Increments and Decrements

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

Default ordering in global scope

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.

Automate Outlook 365 and Your Other Favorite Apps

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:

Use Quick Steps to Take Instant Action on New Email

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:

  • Click the "Create New" button in the Quick Steps section of the Home ribbon.
  1. Give the new Quick Step a descriptive name, and select the first action: "Forward."
  2. Type your accountant’s email address into the "To" field.
  3. Click the "Add Action" button to select the second action: "Create a task with attachment."
  4. Click "Finish" to create the Quick Step. After you review an invoice, you can now execute the Quick Step to forward the email and create a reminder task. Select an email containing an invoice, and click the appropriate Quick Step in the Home ribbon. The email forward and task windows will open automatically. Click Send to forward the email, and set a due date and reminder for the task before saving, if you'd like. There are many different ways to use Quick Steps to speed up email processing. Spend some time thinking about the repetitive actions you take on emails, and set up Quick Steps to make managing your emails so much quicker and almost painless.

Create Quick Parts for Default Responses to Common Questions

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:

  • Highlight the text you want to save in the email, and click the "Insert" tab.
  1. Within the "Insert" menu, click "Quick Parts," and select "Save Selection to Quick Part Gallery."
  2. Give your new Quick Part a descriptive name, and click "OK." With this Quick Part saved, you can now add that text to any email with just a couple clicks. With your cursor in the body section of the email, click the "Insert" tab. Click "Quick Parts," and then select the Quick Part you want to insert. The prewritten text will automatically appear in the body of your email, and you can edit and customize it as needed for your new response.

Use Rules to Automatically Sort Emails and Stop Receiving Irrelevant Emails

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:

  • Automatic Notifications: If you’re assigned to a team on one of the platforms you use at work—such as customer relationship management (CRM) software—that platform may send an email every time a change is made in the system.
  • "Fun" Emails: Sometimes a team leader or department will send daily/weekly emails with trivia, memes, or jokes designed to make work feel more fun.
  • Threads You Shouldn’t Have Been Included On: Occasionally, someone will send a mass email that gets dozens of replies—none of which have anything to do with you or your job. These emails keep you from focusing on your important work. Use Outlook’s rules function to skip the inbox and send those emails straight to the trash—or to another folder for review. You can also use Outlook rules to automatically sort incoming emails to the appropriate folder, such as always sending emails that are from your boss with a certain subject line to the To-Do folder and emails from a newsletter you regularly read to the Someday folder.
  • Right-click the email, hover over "Rules," and select "Create Rule."
  1. Choose the criteria for the rule:
  • "From [Sender]" will execute the rule any time you receive an email from a specific email address or contact.
  • "Subject contains" will execute the rule any time a specified subject line is used.
  1. Select what happens when the rule runs. To sort emails out of your inbox and into the appropriate folder, check "Move the item to folder," and select the folder where you want the emails to be sent. You can choose the "Deleted Items" folder if you never want to see them, or you can create a separate archive folder if you may need to review the emails in the future. Click "OK" to apply the rule. Want to prevent getting future emails on a specific thread? In that case, instead of creating a rule, you can just use Outlook's ignore function. Right-click the email, select "Ignore," and confirm the action. Ignore automatically sends future replies to that thread to your "Deleted Items" folder so you don’t receive them in your inbox.

Clean Up Your Inbox in One Click

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:

  • While viewing your inbox, click the "Clean Up" button, and select "Clean Up Folder."
  1. Click the "Clean Up Folder" button in the popup to confirm the action. Outlook will automatically remove all duplicate emails, leaving you with significantly fewer emails to sort through. The clean-up function can be a little disconcerting to use initially. What if it deletes something important like a reply that contained an attachment that was removed in a later thread? Rest assured: the tool is sophisticated and will not delete any emails with attachments or text that aren’t exactly duplicated in later threads, and you can always review deleted items in the trash folder if needed.

Use Outlook’s Task List Instead of Clogging Your inbox

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:

  • Drag the email and drop it on Outlook’s task list icon.
  1. Add a due date and set a date and time to receive a reminder. Save the task. You can now hover your cursor over the task list icon to see a quick view of your task list, organized by due date. Click on the task list icon to open your to-do list and review the respective tasks. If you set a reminder, the task will pop up like a meeting reminder at the specified time. Once the task is complete, mark it as such to remove the task from your to-do list. Open the task by double-clicking it, and click the "Mark Complete" button—or just press the Insert key to quickly mark the item complete. Outlook’s task list is also useful if you’re in the habit of copying yourself on emails as a reminder to follow up, or if you’re emailing someone who is prone to ignoring your requests. Instead of keeping reminders as emails in your inbox, just move them to your to-do folder and add them to your task list.
;