提问者:小点点

拉雷维尔5号雄辩的哈斯曼尼思


我有三种型号。展会、类别和内容。集市有很多内容,内容属于一个范畴。

我需要检索与属于博览会的内容相关的所有类别。

e、 gContent::with('category','subCategory','fair','fair.coordinates','fair.categories'))-

从Laravel文档来看,这似乎可以满足我的需要:http://laravel.com/docs/5.1/eloquent-relationships#has-许多通过

尝试时:返回$this-

我得到这个错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.category_id' in 'field list' (SQL: select `contents`.*, `categories`.`category_id` from `contents` inner join `categories` on `categories`.`id` = `contents`.`category_id` where `categories`.`category_id` in (1))

这有可能吗?

模型:

类别

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = ['type', 'name', 'description'];
    protected $hidden = ['created_at', 'updated_at'];

    public function subCategories()
    {
        return $this->hasMany('App\SubCategory');
    }
}

内容

namespace App;

use Illuminate\Database\Eloquent\Model;

class Content extends Model
{
    protected $fillable = [
        'type',
        'title',
        'location',
        'latitude',
        'longitude',
        'date',
        'price',
        'position',
        'vip',
        'schedule',
        'content',
        'image',
        'fair_id',
        'category_id',
        'sub_category_id',
    ];

    protected $hidden = ['updated_at', 'category_id', 'sub_category_id', 'fair_id'];

    public function fair()
    {
        return $this->belongsTo('App\Fair');
    }

    public function category()
    {
        return $this->belongsTo('App\Category');
    }

    public function subCategory()
    {
        return $this->belongsTo('App\SubCategory');
    }
}

公平:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Fair extends Model
{

    public function content()
    {
        return $this->hasMany('App\Content');
    }


    public function categories()
    {
        return $this->hasManyThrough('App\Content', 'App\Category', 'category_id');
    }

    public function coordinates()
    {
        return $this->belongsTo('App\Coordinate', 'coordinate_id')->select(['id', 'longitude', 'latitude']);
    }
}

见下表结构:

集市

+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| name          | varchar(255)     | NO   |     | NULL                |                |
| city          | varchar(255)     | NO   |     | NULL                |                |
| startAt       | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| stopAt        | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| availableAt   | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| color         | varchar(255)     | NO   |     | NULL                |                |
| link          | varchar(255)     | NO   |     | NULL                |                |
| image         | varchar(255)     | NO   |     | NULL                |                |
| ads           | varchar(255)     | NO   |     | NULL                |                |
| created_at    | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at    | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| coordinate_id | int(11)          | YES  |     | NULL                |                |
+---------------+------------------+------+-----+---------------------+----------------+

类别

+-------------+------------------+------+-----+---------------------+----------------+
| Field       | Type             | Null | Key | Default             | Extra          |
+-------------+------------------+------+-----+---------------------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| type        | varchar(255)     | NO   |     | NULL                |                |
| name        | varchar(255)     | NO   |     | NULL                |                |
| description | text             | NO   |     | NULL                |                |
| created_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+-------------+------------------+------+-----+---------------------+----------------+

内容

+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| type            | varchar(255)     | NO   |     | NULL                |                |
| title           | varchar(255)     | YES  |     | NULL                |                |
| location        | varchar(255)     | NO   |     | NULL                |                |
| latitude        | varchar(255)     | YES  |     | NULL                |                |
| longitude       | varchar(255)     | YES  |     | NULL                |                |
| date            | timestamp        | YES  |     | NULL                |                |
| price           | varchar(255)     | YES  |     | NULL                |                |
| position        | varchar(255)     | YES  |     | NULL                |                |
| vip             | varchar(255)     | YES  |     | NULL                |                |
| schedule        | varchar(255)     | YES  |     | NULL                |                |
| content         | text             | NO   |     | NULL                |                |
| image           | varchar(255)     | YES  |     | NULL                |                |
| fair_id         | int(11)          | NO   |     | NULL                |                |
| category_id     | int(11)          | NO   |     | NULL                |                |
| sub_category_id | int(11)          | NO   |     | NULL                |                |
| created_at      | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at      | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+-----------------+------------------+------+-----+---------------------+----------------+

共2个答案

匿名用户

HasManyThrough仅在存在两种hasMany关系时有效,例如:Fair hasMany content=

匿名用户

@RDelorier是正确的,但您还有另一个问题。

在hasManyThrough()调用中切换模型。第一个参数是您试图加载的模型,因此其中一个参数应该是App\Category;第二个参数是中间模型,因此其中一个应该是App\Content。

如果它真的要工作,它会这样工作:

return $this->hasManyThrough('App\Category', 'App\Content', 'fair_id', 'content_id');

@RDelorier所描述的剩余问题是,此定义要求您的类别模型具有content_id属性,与hasMany()关系一致。

实现您所追求的目标的最干净的方法就是扩展点符号链,使用“fair.content.category”而不是“fair.categories”。

对于您的模型,如果您需要一组类别模型,则必须手动解析生成的公平和内容集合。