繁体   English   中英

Laravel 5 Eloquent hasManyThrough

[英]Laravel 5 Eloquent hasManyThrough

我有三个模型。 展会、类别和内容。 博览会有很多内容,内容属于一个类别。

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

例如Content::with('category', 'subCategory', 'fair', 'fair.coordinates', 'fair.categories')->get()->toArray();

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

尝试时: return $this->hasManyThrough('App\\Content', 'App\\Category', 'category_id');

我收到此错误:

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 |                |
+-----------------+------------------+------+-----+---------------------+----------------+

HasManyThrough 仅在有两个 hasMany 关系时才有效,例如:公平的 hasMany 内容 => 内容有很多类别 然后你可以跳转内容但没有第二个 hasMany 它将不起作用

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

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

如果它可以正常工作,它将像这样工作:

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

@RDelorier 描述的剩余问题是,此定义希望您的 Category 模型具有 content_id 属性,与 hasMany() 关系一致。

完成您所追求的最简洁的方法是扩展点符号链,使用“fair.content.category”而不是“fair.categories”。

对于您的模型,如果您需要类别模型集合,则必须手动解析生成的 Fair 和 Content Collection。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM