php - helper vs controller performace in laravel 5.*

185

Lets assume I have ahelper called engine.

if( ! function_exists('engine') )
{
    function engine($user_id_1, $user_id_2, $league, $log = true)
    {
        /*
         * variables
         */
        $questionsLevel = 1;

        $user1 = \App\User::where('id', $user_id_1)->first();
        $user2 = \App\User::where('id', $user_id_2)->first();

        $league = \App\Models\League::where('id', $league)->first();

        $users = $league->all_users;

        /*
         * check if users joined to league correctly
         */
         $user1_ok = $user2_ok = false;
         foreach($users as $user)
         {

             if( $user->id == $user_id_1 )
             {
                 $user1_ok = true;
             }
             if( $user->id == $user_id_2)
             {
                 $user2_ok = true;
             }

             $check_users = [
                 $user1_ok,
                 $user2_ok
             ];
         }

        if( in_array(false, $check_users) )
        {
            return [
                'errcode' => 404,
                'errmessage' => 'one ro both user/s did not joined to league'
            ];
        }

       //DO SOME STUFF

    }//function engine
}

As you know, I can write acontroller to do same.

Does anyone know Which is faster based on Laravel architecture? and how can I test performance in both cases?

I'm usinglaravel 5.7.*

2

Answer

Solution:

Fastest would be a solution that does not load unnecessary data:

if(! function_exists('engine'))
{
    function engine(int $user1, int $user2, int $league)
    {
        return League::query()
            ->where('id', $league)
            ->whereHas('users', function($query) use ($user1) {
                $query->where('id', $user1);
            })
            ->whereHas('users', function($query) use ($user2) {
                $query->where('id', $user2);
            })
            ->exists();
    }
}

In general, I don't think such a function should return complex results like an error message though. For this particular job, you should use a controller action. The helper method should only returntrue orfalse in my opinion. You could also place the same logic in a service class though (probably the best solution).

157

Answer

Solution:

About the performance, I do not think there will be any changes as same query will be executed as well as same number of operation will be done, for example theforeach() loop it isO(n) in both the cases, so what will be difference, but you may like to change your code to something like below or may use ternary operator also. The difference will be in organization of codes. In controller the testing will be easier.

Note: But howid will be both$user1 and$user2, so let's make it OR in stead of AND

foreach($users as $user)
 {
     if( $user->id == $user_id_1 )
     {
         $user1_ok = true;
     }elseif( $user->id == $user_id_2)
     {
         $user2_ok = true;
     }

     $check_users = [
         $user1_ok,
         $user2_ok
    ];
}

to

foreach($users as $user)
{
    if( in_array($user->id, [$user_id_1, $user_id_2])
    {
        $user1_ok = true;
    }

    $check_users = [
        $user1_ok,
        $user2_ok
    ];
}

or

foreach($users as $user)
{
    if( $user->id == $user_id_1 )
    {
        $check_users['user1_ok'] = $user->id == $user_id_1 ? true : false;
    }
    if( $user->id == $user_id_2)
    {
        $check_users['user2_ok'] = true;
    }
}

If you want to get enginees withuser1_ok,user2_ok, you may like to run a query as @Namoshek has suggested.

or you may like to change the query to something like

League::query()->where('id', $league)
        ->whereHas('users', function($query) use ($user1, $user2) {
            $query->where('id', $user1)->where('id', $user2);
    })->exists();

but, how id will be both $user1 and $user2?so, if it is OR instead of AND.

League::query()->where('id', $league)
    ->whereHas('users', function($query) use ($user1, $user2) {
        $query->whereIn('id', [$user1, $user2]);
})->exists();

or even$users = [$user1, $user2]; and

League::query()->where('id', $league)
    ->whereHas('users', function($query) use ($users) {
        $query->whereIn('id', $users);
})->exists();

By the way, if you want this using these query, you just may put it in Engine model and user or reuse it when ever required. Also for helper, it can be reusable where as in controller it is not.

People are also looking for solutions to the problem: string - Words syllables in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.