Laravel hasMany with where

后端 未结 2 1717
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 02:56

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 fo

2条回答
  •  爱一瞬间的悲伤
    2021-02-01 03:55

    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');
        }
    }
    

提交回复
热议问题