0
votes

Im starting with Laravel/Eloquent short ago. Coming from Java/Hibernate, but never mastered.Trying to open new ways. Using Laravel 8, latest version right now. AFAIK, in java hibernate, you can declare id attribute in Model, link it via Hibernate annotations to DB builder, and, when you deploy the app it creates a bunch of tables and restrictions such as PK, FK, UNIQUE, etc (keeping it simple stupid).

But not in Laravel or so i think. I get this error I get this error

from User Model created by artisan, but slightly modified with some random attributes, but no static function id() insidestatic function id()???

This, the seeder Seeder

This, the Factory in trouble, I guess, in $vend->id, and $comp->id. Sellers and buyers extends User. All Users can be both sellers and buyers, but can´t buy to themselves

enter image description here

What am I loosing? How to statically call an attribute of instantiated object? shoul I implement a static function like return self::id? but no id inside just in the tables seeded!!! Then should i put inside my Models an id attribute?

Sorry if it sounds dumb, so I am, and newbie too. Thanks in advance

1
you do need to put a static method inside your model, but what you want to do with this id() methodbhucho
I´m not sure what I want to make with it, it just claims, but i didn´t generate the call. It´s automatically loaded by Eloquent.Vidal M.
Can't see any line in the seeder which could throw the exception. Go through the factories and check if you have any reference to id() method in any of those factories.Donkarnash
try to post the code here instead of images, though in this question in does not matter much if code is in image or posted herebhucho
After $vend=Seller::has('products')->get()->random(); $vend is a Seller record. So in the next statement $vendor->id->random() doesn't make sense. $vendor->id is a single value and doesn't have random method. Also except method on collection should be defined with key, value. Comp=User::all()->except('id', $vend->id). Still can't find the line which would throw an error for id() on UserDonkarnash

1 Answers

0
votes

From what i understand you want to get the user with Id X using laravel eloquent.

You are almost there. Laravel eloquent has build in functions for you so for example if you want to get the user with Id = 5 you can do:

$user = App\Models\User::find(5);

Find filters based on id or you can create a normal query statement like:

$user = App\Models\User::where('id','=',5)->first();

If you want to use a different column in users model, for example the ones you added yourself the second option fits best for you.