本文介绍了Laravel有很多在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张桌子,汽车,公寓和商店。每张桌子都有照片。照片存储在数据库中。我只想使用一张桌子的照片,我不想为每个汽车,公寓和商店创建照片表。



照片表格结构就是这样; / p>

  | id | photo_url |类型| destination_id | 
---------------------------------------------- --------------
1 | http://example.com/1.jpg |汽车| 1 |
2 | http://example.com/2.jpg | Flats | 1 |
3 | http://example.com/3.jpg | Flats | 2 |
4 | http://example.com/4.jpg |商店| 1 |
5 | http://example.com/3.jpg |商店| 2 |

我需要在商店,平面和汽车模型类中定义hasMany关系类型。



正确的方法是什么?

解决方案

可以使用Eloquent的。 Laravel文档中的示例实际上展示了为多个型号设置一个通用图像表,以便您指向正确的方向。在你的情况下,你的模型看起来像这样:

  class Photo延伸Eloquent {

public function imageable()
{
return $ this-> morphTo();
}

}

class Car扩展Eloquent {

public function photos()
{
return $ this-> morphMany('Photo','imageable');
}

}

class Flat Eloquent {

public function photos()
{
return $ this-> morphMany('Photo','imageable');
}

}

class Shop延伸Eloquent {

public function photos()
{
return $ this-> morphMany('Photo','imageable');
}

}

您可以访问照片我们假设一个给定的 Flat ,如下所示:

 找到($ ID) - >照片; 

为此,您还需要为照片表:

  imageable_id:integer<  - 这将是模型的ID 
imageable_type:string< - 这将是模型的类名(Car / Flat / Shop)


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?

解决方案

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)

这篇关于Laravel有很多在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 02:24