7 quick Laravel Tips

Published in Originals on Mar 3, 2022

Here are 7 quick tips for your next Laravel project.

Based on experience I’ve Tweeted some of these tips and though, why don’t I gather them into a blogpost. I hope these come in handy when writing your applications, in any case I use them quite often.

1. Eager Loading

When loading model relationships we could use eager loading in order to reduce the amount of queries that are performed while fetching data.
Only fetching those relation columns that are actually needed is better them pulling them all in at once.

$cars = Car::with('brand:id,name')->get();

We could also eager load a relationship by default by adding it to the model in a protected "$with" variable. By doing so the specified relationship will eagerly loaded during a regular fetch.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Car extends Model {
    /**
	* The relationships that should always be loaded.
	*/
    protected $with = ['brand'];

    /**
	* Get the brand that builds the car.
	*/
    public function brand() {
        return $this->belongsTo(Brand::class);
    }
}

2. Sharing data across views

if you want to share some variables across all views in a controller you could add use View::share in the constructor of the controller.
By doing so you can use the variable in all the views generator based from this controller.

namespace App\Http\Controllers;

use Illuminate\Support\Facades\View;

class CarController extends Controller
{
    /**
    *  The variable $sharedData will be accessible
    *  in both the index.blade.php and detail.blade.php.
    */
    public function __construct(){
        View::share('sharedData',['foo' => 'bar']);
    }

    public function index(){   
        return view('index');
    }

    public function detail(){   
        return view('detail');
    }
}

3. Route restrictions

Restricting route variables can be of use to add some extra security to your routes or split routes with numeric variables from those with alphanumeric variables.
For convenience reasons you can now use “WhereNumber, WhereAlpha, …” in stead of a regex style route restrictions.

/**
*   In Laravel vou can now use expressive Route restrictions ✨
*   Such as: WhereNumber, whereAlpha, whereUuid,
*   whereAlphaNumeric, ...
*/

// ❌ without restrictions
Route::get('/user/fid}', function($id) {
    //
});

// ❌ with a regex style check
Route::get('/user/{id}', function($id){
    //
})->where('id', '[0-9]+');

// ✅ Only numbers allowed
Route::get('/user/{id}', function($id) {
    //
})->whereNumber('id');

// ✅ Only alpha allowed
Route::get('/user/{name}', function ($id) {
    //
})->whereAlpha('name'):

4. Fetching Models without a relation

Fetching data based on the absence on a relation make easy with the doenstHave method. For example fetching all Orders that don’t have an Invoice yet.

/**
*   Fetch models that don't have a certain relationship
*   For example: Get all orders without an invoice.
*/

$orders = Order::doesntHave('invoice')->get();

/**
*   You can also chain "take" to this, F.E. take only 10
*/

$orders = Order::doesntHave('invoice')->take(10)->get():

5. Copying models correctly

Duplicating models, can be a mess in your code. Especially when the model has a lot of items there is a risk that one of the columns isn’t copied due to an human error. This can be prevented by using the replicate method that comes with Laravel for free.

/**
*   Copying models done correctly.
*   Using the replicate function.
*/

// ❌ How you could do it.
$newCar = new Car();
$newCar->model = $car->model;
$newCar->brand = $car->brand;
$newCar->catalogPrice = $car->catalogPrice;
$newCar->hp = $car->hp;
// ...
$newCar->save();

// ✅ How you should do it.
$newCar = $car->replicate();
$newCar->save();

6. Saving data quietly

Sometimes we need to update some data on a model, just some small things.
But in those cases we don’t want to trigger any Model Events, if there are any such as notifications or other automated processes.
We can save all our changes to a model without triggering those events by using the saveQuietly method. This way our updates will be stored quietly, as the name of the method indicates, thus without triggering any events.

/**
*   Updating model data without triggering
*   the Model Events? Save them Quietly. ?
*/

$user = User::find($id);
$user->firstname = "John";
$user->saveQuietly();

Fallback route

When a user visits a route that doesn’t exist the will be a 404 error code.
We could actually prevent the default 404 error page from rendering and replace it with our own, or provide some custom logic that we want to perform whenever this happens.

/**
*   Show a custom page when a user runs into a 404,
*   instead of throwing the regular 404.
*   With or without custom logic to spice it up. ?‍
*/

Route::fallback(function(){
    // Your custom 404 logic or custom view goes here
});


#laravel, #tips