--level used: maxFor that test I oriented at the documentation of Laravel, if I want to update data.
$user = Users::find($id);
$user->name = $name;
$user->save();
Messages
Cannot access property $name on (Illuminate\Database\Eloquent\Collection&iterable<static(App\Users)>)|static(App\Users)|null.
Cannot call method save() on (Illuminate\Database\Eloquent\Collection&iterable<static(App\Users)>)|static(App\Users)|null.
If I would use this one, there is no message by Larastan.
$user->update([
'name' => $name
]);
Good to know that the update method has no issue as it's my preferred syntax π
Does your model has docblock for $name property?
@canvural If you mean something like this, it makes no difference whether you have it or not.
/**
* Get the user's name.
*
* @param string $value
* @return string
*/
public function getNameAttribute($value)
{
return $value;
}
/**
* Set the user's name.
*
* @param string $value
* @return void
*/
public function setNameAttribute($value)
{
$this->attributes['name'] = trim($value);
}
The problem is that you're using the maximum level and the object can be null. If you used lower level or if you made first sure that it cannot be null, this wouldn't be reported.
The reason why I use the maximum level is because I want to fix everything I can, if I have any mistakes and in my case a name, for example, can never be null, because when requests, he will be always required.
How can you be so sure? In $user = Users::find($id);, the $id can be nonexistent.
Because it's my way to work with hidden fields at ids
What if someone's rewrite the ID in your hidden field and submits the form?
Ok there I would have no solution immediately at hand
Thatβs why the error reported by PHPStan is correct.
On Mon, 30 Jul 2018 at 12:03, ForzaSFerrari notifications@github.com
wrote:
Ok there I would have no solution immediately at hand
β
You are receiving this because you commented.Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/103#issuecomment-408812874,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuNJ165Xfu2Qi0OeAqb4hZ4VZ3tu5ks5uLtnygaJpZM4VlBkt
.>
OndΕej Mirtes
I would never doubt that the error would be wrong. As I said I just want to fix everything I can. On the same side I want to understand with such tools, where my own errors are and would like to improve myself for the future.
If you mean something like this, it makes no difference whether you have it or not.
I think @canvural meant something like this:
<?php
/**
* @property string $name
*/
class User extends Model
{
// ...
}
Could you check if that fixes it? If so, you can use barryvdh/laravel-ide-helper to add those annotations automagically π
@svenluijten @canvural Tried it out. Nothing has been changed.
That's because the problem isn't the $name part, but the null part... You can verify that by running level 6, not 7.
@ondrejmirtes one of the errors @ForzaSFerrari is getting is this, and it looks like this certainly has something to do with the $name property...
Cannot access property $name on (Illuminate\Database\Eloquent\Collection&iterable<static(App\Users)>)|static(App\Users)|null.
So on level 6 I get this result Access to private property $name of parent class Illuminate\Database\Eloquent\Model.
Actually, it would be interesting now, what will be checked on the different levels.
@svenluijten Trust me, I'm developing PHPStan ;)
@ForzaSFerrari You can check the configs (https://github.com/phpstan/phpstan/tree/master/conf) and also levels integration tests (https://github.com/phpstan/phpstan/tree/master/tests/PHPStan/Levels).
If I understand it correctly, it's better to check various levels and fix level 6 before level 7 for example.
@ondrejmirtes I also got another idea for phpstan in that case. Because I thought if you let run the maximum level you would get all errors from the levels below.
You wouldn't bump into this problem if you followed how the tool is supposed to be used. Check this out:

(from Reddit: https://www.reddit.com/r/laravel/comments/8zp759/introducing_larastan_alpha_version/e2m0aqq/)
I acknowledge it's a bit weird that level 7 reports a different error than level 6 but in practice this is not a problem if you first fixed stuff from level 6.
As I learned you have to fix things first, who are easy. Things, who are difficult and you don't know, how they happen or you never heard before, need to be researched, especially if you hit on a following error.
Likewise, I do not follow everything and everywhere.
@ForzaSFerrari Hey buddy, to fix this issue:
$user = Users::find($id);
if ($user) {
$user->name = $name;
$user->save();
}
Larastan is doing is job warning you that the user may not exists, so you should check it first with the if statement.
It only fixes Cannot call method save() on (Illuminate\Database\Eloquent\Collection&iterable<static(App\Users)>)|static(App\Users)|null. but Cannot access property $name on (Illuminate\Database\Eloquent\Collection&iterable<static(App\Users)>)|static(App\Users)|null. stays on the last level.
On level 6, you have to fix Access to private property $name of parent class Illuminate\Database\Eloquent\Model. before.
I am having a similar error:
$ticketType = Models\TicketType::findOrFail(
data_get($data, 'ticket_type_id')
);
$tiersList = $ticketType->getBestPricingTierCombination($quantity, $data);
// some more code
return [
'ticket_type_id' => $ticketType->id,
];
This results in:
$ ./artisan code:analyse -l max
43/43 [ββββββββββββββββββββββββββββ] 100%
------ ---------------------------------------------------------------------------------------------------------------------------------------------------
Line app/Http/GraphQL/Queries/PriceOffering.php
------ ---------------------------------------------------------------------------------------------------------------------------------------------------
32 Call to an undefined method
(Illuminate\Database\Eloquent\Collection&iterable<static(App\Models\TicketType)>)|static(App\Models\TicketType)::getBestPricingTierCombination().
36 Access to private property $id of parent class App\Models\BaseModel.
------ ---------------------------------------------------------------------------------------------------------------------------------------------------
I can't see why this would be expected behaviour.
The only thing I can think of that causes this issue, is that Larastan has some magic exceptions for Eloquent models, but because I extend my models from BaseModel (which extends from Illuminate\Database\Eloquent\Model).
I would however not expect this error since it still extends the eloquent model, just one level deeper.
Relates to #187
laravel-ide-helper does it automatically)/**
* @property string $name
*/
class User extends Model
{
// ...
}
@var when you use firstOrFail():/** @var User $user */
$user = User::findOrFail($id);
Unfortunately, for the time being, it's not possible to infer the model with firstOrFail() or first(). laravel phpdocs required? :thinking:
It should be easy to do in Larastan automatically.
On Mon, 12 Aug 2019 at 00:15, Pablo Reyes notifications@github.com wrote:
Solution for Access to private property [...] Model problem
- Define your phpdoc block on your model (laravel-ide-helper does it
automatically)/**
- @property string $name
*/
class User extends Model
{
// ...
}
- You need put @var when you use firstOrFail():
/** @var User $user */
$user = User::findOrFail($id);Unfortunately, for the time being, it's not possible to infer the model
with firstOrFail() or first(). laravel phpdocs required? π€β
You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/103?email_source=notifications&email_token=AAAZTOGVTZAQAIEFJTIHQ2LQECFRRA5CNFSM4FMUDEW2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4BJ6PQ#issuecomment-520265534,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAZTOGXJ2SDJWYEKNEBDCDQECFRRANCNFSM4FMUDEWQ
.>
OndΕej Mirtes
Hello friends,
can someone help me with this
Cannot access property $token on mixed.
Cannot call method extension() on array
Please open a new issue.