Category : polymorphism

I need to map multiple "types" pointing to same model over a polymorphic relationship. This because in my application i set a laravel-permission authentication package that fails if i don’t include his model type in AppServiceProvider.php boot method. I’ve got a Resource model that has a polymorphic relationship with User model and Equipment model. Every ..

Read more

I have problem. I use four tables (example marks, cars, motobikes, autotrains) in Laravel One To Many (Polymorphic) and i want order marks list by other tables price column. How? class Car extends Model implements TranslatableContract { public function marks() { return $this->morphMany(Mark::class, ‘marksable’); } } class Motobike extends Model implements TranslatableContract { public function ..

Read more

I have 2 tables related to creating credit notes which allows selecting multiple invoices. I was able to store the data into a database using postman(api controller), but having problems in API integration with the frontend. Database Credit Note id credit_note_group_id invoice_id code remarks amount 7 4 5 CN0006 cn remarks 150.00 8 4 8 ..

Read more

I’m trying to setup a polymorphic relation between Users & Organizations, to Repositories. Repository.php public function owner(): MorphTo { return $this->morphTo(); } User.php public function repositories(): MorphMany { return $this->morphMany(Repository::class, ‘owner’); } Organization.php public function repositories(): MorphMany { return $this->morphMany(Repository::class, ‘owner’); } repository_has_owners table: id repository_id owner_id owner_type created_at updated_at 1 1 1 AppUser However, ..

Read more

I have a challenge in laravel polymorph many to many relationship. i am using this database architecture products id – integer name – string price – float articles id – integer title – string description – string tags id – integer name – string taggables tag_id – integer taggable_id – integer taggable_type – string how ..

Read more

Is it possible to get the connected items in 1 query of all the relations in laravel, so this function gets all the releations but is it also possible to query the items with it public function bookmarkItems(Bookmark $bookmark): IlluminateHttpJsonResponse { return response()->json($bookmark->items()->get()->toArray()); } the functions returns this [ 0: {id: 1, bookmark_item_id: 2, bookmark_item_type: ..

Read more

I have a transactions table that has transaction types that morph to TopUpTransaction, DeductTransaction, etc. that holds their specific informations (as they have different fields). Now I have a transaction type SecurityDeposit that doesn’t need to have a dedicated model as it doesn’t need any extra information. I can make it work with default morph ..

Read more

I need a list of class names of all models, that have a OneToOne or OneToMany polymorphic relationship definition to another specific model. this list must not be based on the actual existing relations in the corresponding database table, but on the code definition. Example: Class A,B and C have a polymorphic one-to-one or one-to-many ..

Read more

I’d like to create a relation for a Group Model, which can have members of different morph types through a pivot table ‘group_members’. And this is what my structure currently looks like: Classes: Class Group { public function Members() { return $this->hasMany(GroupMember::class, ‘group_id’); } // Now here is where i want to return all the ..

Read more

This is my update method, I’m using simple polymorphic relation morphTo and morphOne. I’m having trouble in updating product model. public function update(Request $request, Product $product) { $product->name = $request->name; $product->price = $request->price; $product->discount = $request->discount; $product->category_id = $request->category; $product->brand_id = $request->brand; $product->is_active = $request->is_active; if($request->hasFile(‘url’)) { $image = $request->file(‘url’); $filename = $image->getClientOriginalName(); $image->move(public_path(‘storage/products’), $filename); ..

Read more