Support the ongoing development of Laravel.io →
posted 5 years ago
Eloquent

In api.php

Route::get('/abc', 'App\UserController@abc');

In UserController.php

public function abc()
{
    $user = User::find('123456');

    dd($user);
}

In User.php

public function __construct($attributes = array())
{
    echo (' hello ');
}

Use postman to make HTTP REQUEST GET call

/abc

Return result

hello hello hello

User {#833
  #fillable: array:4 [
    0 => "first_name"
    1 => "last_name"
    2 => "email"
}

Thanks!!

Last updated 2 years ago.
0

are you using auth for your request? checking for auth user ?

0

No, the /abc route place at the top of api.php without any middleware auth

0

That is how Laravel works I suppose. It creates the blank object (User) and then query the data from DB to it (I am not sure what is the 3rd time for to be honest)

I never use constructor in Laravel app inside Model class.. there are surely other ways to accomplish what you need

0

Dear all,

I feel sorry about that. Today I repeat the experience again. The constructor have been called 2 times not 3 times.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.