Top 4 Laravel Tips and Tricks

PHP Laravel Framework is one of the most popular web frameworks nowadays. In this article, we will share some of the top Laravel tips and tricks with you.

#Laravel Tips 1 – Push Method

Instead of saving all relationships separately, you can save all the relationships in a single line code. Save the model and all of its relationships at once using the push method.

 * Save the model and all of its relationships.
 * @return bool
public function push()
    if ( ! $this->save()) return false;

    // To sync all of the relationships to the database, we will simply spin through
    // the relationships and save each model via this "push" method, which allows
    // us to recurse into all of these nested relations for the model instance.

    foreach ($this->relations as $models)
        foreach (Collection::make($models) as $model)
            if ( ! $model->push()) return false;

    return true;

It just shows that push() will update all the models related to the model, so if you change any of the relationships, then call push() It will update that model and all its relations.

$user = User::find(32);
$user->name = "TestUser";
$user->state = "Texas";
$user->location->address = "123 test address"; //This line is a pre-defined relationship
// Save all the data into User and Address model.

It will save all the data, and also save the address into the address table/model, because you defined that relationship in the User model.

push() will also update all the updated_at timestamps of all related models of whatever user/model you push().

You can read more about laravel eloquent relationships from this doc.

#Laravel Framework Tips 2 – getOriginal and getRawOriginal Method

The getRawOriginal method will actually grab the value from the $original array.

#3 – foreignIdFor Method

Basically, it creates a foreign key based on the class’s snake case appended by the primary key column.

#4 – Route Helper Function

By default, the route function is returning an absolute path, the full URL. If you pass in false, you only get the path back. This can be useful if you want to pass the URL to your frontend or if you want to display the URL in your application without the domain.

Best Laravel Packages for Development

Now a days PHP Laravel framework is one of the most popular web framework for web application development. In this article we will list some best laravel packages for development.

Developer like you and me what to develop the application first and full of features. Write code for different reuse features takes time to develop and tasting.

To meet the project deadline and meet the DRY principal Laravel framework comes with package feature.

You can includes different types of package into your current project or new project.

You need install this tools before adding this packages into your Laravel project.

Tools: Composer, PhpStorm or Visual Studio Code  ( VS Code), Laragon or XAMPP

List of best Laravel packages from Github links are listed for you.

10 10 minutes

Faker is a PHP library that generates fake data for you. 

Module Management In Laravel

Laravel Debugbar (Integrates PHP Debug Bar)

A PHP library for generating universally unique identifiers (UUIDs). by using this package you can easily crate unique id. this package has lot of options.

Laravel Excel Package this package is very useful to enable the excel operation in your laravel project.

Blog Packages is one of the best Laravel Blog Package. full admin feature enabled blog package.

Ecommerce Package – Aimeos is one of the best Laravel eCommerce packages. – An Opensource eCommerce

Admin Package this is one of the best Laravel admin package based on adminlte theme.

Time to time we will update the package list.

Hope this article help you in your next Laravel project.

How to crate your own package for Laravel and share with others.

Exit mobile version