Boost Your Laravel Development With These Tips
Web Development 148

Boost Your Laravel Development With These Tips

Boost Your Laravel Development With These Tips

Community is growing day by day. There will be more resources available to assist you in learning Laravel. That’s fantastic! But on what bases you decide which one is the best? Which will be a better use of your time, reading or watching? I’m sure these questions are on your mind if you’re new to the framework, so here are seven tips to help you learn Laravel more effectively.

You can use certain tips and tricks when working with any programming language or framework to improve your coding level and make your application more performant and your code more elegant. The more you apply certain design principles and best practises, the better your developer skills will become.

Every Laravel development company employs some methods and recommendations to help them succeed. These are also considered as tips and tricks. Some of these tips are specific. While some are general which can help to learn any programming language. I’ll show you a few of them, and perhaps you’ll be able to use them to boost your Laravel apps. Let’s get started!

1- Use Eloquent Query to make code more elegant: When working with data with Eloquent ORM in our Laravel apps, we frequently need to match certain requirements. E-g:

$active_administrators = User::where(‘active’, ‘=’, 1)->where(‘is_admin’, ‘=’, 1)->get();

These conditions could be used in a variety of places across our program. We can utilize query scopes to make our code more legible and avoid repetition. Like we can use it as:

publicfunctionscopeActive($query)
{
return$query->where('active','=',1);}
publicfunctionscopeAdmin($query)
{
return$query->where('is_admin','=',1);}

Now original will look like this:

$active_administrators=User::active()->admin()->get();

2- Use automatically set Colum value: You can accomplish this by using the model’s creation event. Which is within the model’s boot method.

classInvoiceextendsModel{
protectedstaticfunctionboot()
{
parent::boot();
Invoice::creating(function($model){
$model->paid=0;
});
}}

3. use potential helper to avoid errors:

When object value is accessing it might show error. For example:

return$invoice->total;

Using the optional Laravel helper is an easy method to avoid errors:

return optional($invoice)->total;

Your code will now return null instead of issuing an error. If the invoice object is null.

You may also utilise the optional assistance with a closure.If the first argument is not null and receives the closure as its second argument.

return optional(Invoice::find($id), function ($invoice) {
    return $invoice->total;
});

4. Automatically delete related records:

It is common to build linked tables while designing application databases.

A good database design guideline recommends to remove child related records when removing parent information.We can use the model’s deleting event. Which is inside the boot& method of the model.

class Invoice extends Model
{  public function items()
   {
      return $this->hasMany('\App\Models\Item');
    }
    public static function boot() {
        parent::boot();
        static::deleting(function($invoice) 
             foreach ($invoice->items as $item) {
                 $item->delete()   }
        });
      }
}

5. Create or Update:

It’s usual to see if a specific record already exists and update. Or create a new record if it doesn’t exist. Something along these lines:

$invoice = Invoice::where('active', 1)
    ->where('client', 'My client')
        ->where('price', 49)
    ->first();
if ($invoice) {
    $invoice->update(['price' => 29]);
}

Use Eloquent’s updateOrCreate method instead:

$invoice = Invoice::updateOrCreate(
    ['active' => '1', 'client' => 'My client'],
    ['price' => 29]
);

You must be logged in to post a comment Login

Leave a Reply

Web Development 148

I live in Chicago and work in HAWTHORNE as a Software Developer/Architect. I’ve been a professional developer for more than 15 years. I’ve mostly worked professionally with Java, C, C++ and Python.