Laravel关系 - 不是唯一的表别名

时间:2018-04-18 08:02:50

标签: php sql laravel eloquent

我收到以下错误

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'contacts' (SQL: select * from (select `item_meta`.*, max(case when `item_meta`.`id` = test then 150 else 0 end + case when `item_meta`.`id` like test% then 50 else 0 end + case when `item_meta`.`id` like %test% then 10 else 0 end + case when `item_meta`.`name` = test then 120 else 0 end + case when `item_meta`.`name` like test% then 40 else 0 end + case when `item_meta`.`name` like %test% then 8 else 0 end + case when `contacts`.`name` = test then 45 else 0 end + case when `contacts`.`name` like test% then 15 else 0 end + case when `contacts`.`name` like %test% then 3 else 0 end + case when `contacts`.`name` = test then 45 else 0 end + case when `contacts`.`name` like test% then 15 else 0 end + case when `contacts`.`name` like %test% then 3 else 0 end) as relevance from `item_meta` left join `contact_manufacturer_to_meta` on `contact_manufacturer_to_meta`.`meta_id` = `item_meta`.`id` left join `contacts` on `contact_manufacturer_to_meta`.`contact_id` = `contacts`.`id` left join `contact_vendor_to_meta` on `contact_vendor_to_meta`.`meta_id` = `item_meta`.`id` left join `contacts` on `contact_vendor_to_meta`.`contact_id` = `contacts`.`id` where (`item_meta`.`id` like %test% or `item_meta`.`name` like %test% or `contacts`.`name` like %test% or `contacts`.`name` like %test%) and `item_meta`.`company_id` = 1 group by `item_meta`.`id`) as `item_meta` where `relevance` >= 6.00 and `item_meta`.`deleted_at` is null order by `relevance` desc limit 10)

这是由于我为两个数据透视表创建了相同的表,数据库结构如下所示。

Schema::create('contacts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('contact_manufacturer_to_item', function (Blueprint $table) {
        $table->integer('item_id')->unsigned();
        $table->integer('contact_id')->unsigned();
        $table->string('manufacturer_part_number')->nullable();
        $table->timestamps();

        $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
        $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');

        $table->primary(['item_id', 'contact_id']);
    });

Schema::create('contact_vendor_to_item', function (Blueprint $table) {
        $table->integer('item_id')->unsigned();
        $table->integer('contact_id')->unsigned();
        $table->string('vendor_part_number')->nullable();
        $table->timestamps();

        $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
        $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');

        $table->primary(['item_id', 'contact_id']);
    });

导致问题的控制器代码基于搜索的Eloquence包

return Item::with('vendors')->with('manufacturers')->search($query, [
                'id' => 10,
                'name' => 8,
                'manufacturers.name' => 3,
                'vendors.name' => 3,
            ])
            ->limit($limit)
            ->get();

型号代码

protected $table = 'item_meta';

public function vendors() {
    return $this->belongsToMany('App\Contact', 'contact_vendor_to_meta', 'meta_id', 'contact_id');
}

public function manufacturers() {
    return $this->belongsToMany('App\Contact', 'contact_manufacturer_to_meta', 'meta_id', 'contact_id');
}

我是否可以采取任何措施来解决此问题,而无需为搜索执行RAW SQL查询?

1 个答案:

答案 0 :(得分:0)

这是您雄辩的查询的字面翻译:

return Item::with('vendors' => function ($query) {
  $query->where('name', 3);
}])
  ->with('manufacturers' => function ($query) {
    $query->where('name', 3);
})
  ->where([['id' => 10], ['name', 8]])
  ->limit($limit)
  ->get();

但那也不会奏效。您可能希望实现的目标是:

return Item::with('vendors' => function ($query) {
  $query->where('id', 3); //$vendor_id or $vendor_name?
}])
  ->with('manufacturers' => function ($query) {
    $query->where('id', 3); //$manufacturer_id or $manufacturer_name?
})
  ->where('id', 10) //$item_id or $item_name?
  ->where('name', 8)
  ->limit($limit)
  ->get();

目前还不清楚您要搜索的内容。如果您需要ID(或名称)或项目,则无需在供应商或制造商中进行搜索。你的name令人困惑,因为它是一个整数,而不是一个字符串。迁移架构中没有名称。这些名称是manufacturersvenders吗?这令人困惑。