laravel - why function call with no parentheses?
Laravel uses the magic function __get
to handle arbitrary attributes.
This calls Illuminate\Database\Eloquent\Model
's getAttribute
function, which checks the model's relations and returns the related item(s) if a relationship is present with that name.
The parentheses are not needed because getAttribute
automatically executes the function items()
when the attribute items
is requested. You can, by the way, request Auth::user()->item();
which will return a query builder you can work with.
The method item()
is setting up a relationship for the Eloquent ORM on how to prepare a query. calling ->item
is telling Eloquent through its Dynamic Properties that you want Item and then Eloquent will use the method. You can only call the method directly if it is compatible with Query Builder. The example you give should work either way but there may be something I am missing.