Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel hasMany with where in a Polymorphic relationship

I have 3 tables, Cars, Flats and Shops. Each table has its photos. Photos is stored in database. I want to use only one table for photos, I don't want to create Photos table for each Cars, Flats and Shops.

Photos tables structe is like this;

| id |           photo_url        | type  | destination_id | ------------------------------------------------------------   1  |   http://example.com/1.jpg | Cars  |      1         |   2  |   http://example.com/2.jpg | Flats |      1         |   3  |   http://example.com/3.jpg | Flats |      2         |   4  |   http://example.com/4.jpg | Shops |      1         |   5  |   http://example.com/3.jpg | Shops |      2         | 

I need to define hasMany relationship with type in Shops, Flats and Cars model classes.

What is the correct way to do this?

like image 428
fobus Avatar asked Oct 17 '14 21:10

fobus


People also ask

How does Laravel count Hasmany relationships?

Step 1: Place this code inside your 'Post' model. Step 2: Place this code inside your 'PostController' controller. $posts= Post::all(); return view('home')->with('posts', $posts); Step 3: Then count comments for all posts using the below code.

What is polymorphic relationships in Laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.

What is belongsTo in Laravel?

BelongsTo relationship in laravel is used to create the relation between two tables. belongsTo means create the relation one to one in inverse direction or its opposite of hasOne. For example if a user has a profile and we wanted to get profile with the user details then we can use belongsTo relationship.


2 Answers

You can treat the relationship objects kind of like queries, in that you can call query building functions with them. The example below should get you going in the right direction.

class Cars extends Eloquent {     function photos()     {         return $this->hasMany('Photo')->where('photos.type', '=', 'Cars');     } } 
like image 118
Logan Bailey Avatar answered Sep 17 '22 23:09

Logan Bailey


You can make use of Eloquent's Polymorphic relationships. The example in the Laravel Documentation actually showcases setting up a common images table for multiple models, so that should point you in the right direction. In your case something your models would look something like this:

class Photo extends Eloquent {      public function imageable()     {         return $this->morphTo();     }  }  class Car extends Eloquent {      public function photos()     {         return $this->morphMany('Photo', 'imageable');     }  }  class Flat extends Eloquent {      public function photos()     {         return $this->morphMany('Photo', 'imageable');     }  }  class Shop extends Eloquent {      public function photos()     {         return $this->morphMany('Photo', 'imageable');     }  } 

And you could access the photos for, let's say a given Flat, like this:

Flat::find($id)->photos; 

For this to work you'd also need to add 2 additional columns to your photos table:

imageable_id: integer  <-- This will be the ID of the model imageable_type: string <-- This will be the model's class name (Car/Flat/Shop) 
like image 29
Bogdan Avatar answered Sep 20 '22 23:09

Bogdan