php - 当数据库中有 1 条记录时,Laravel 显示重复
问题描述
我是 Laravel 的初学者。我对 Laravel 有一个小问题。
我有这个代码:
$categories = $categoryRepository->descendantsAndSelf($slug ?? null);
这个回报:
Illuminate\Support\Collection {#535 ▼
#items: array:17 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 6
6 => 7
7 => 8
8 => 9
9 => 10
10 => 11
11 => 12
12 => 13
13 => 14
14 => 15
15 => 16
16 => 17
]
}
还有我的存储库:
public function getProductFromIdCategories($categories)
{
return $this->model->select('name', 'slug', 'products.id', 'small_description')->with(['selectedCategory', 'frontImage'])->active()->leftJoin('selected_product_categories', function ($join) {
$join->on('products.id', 'selected_product_categories.product_id');
})->whereIn('selected_product_categories.category_id', $categories)->get();
}
当我运行我的代码时:
dd($this->model->getProductFromIdCategories($categories->toArray()));
我有结果(重复):
Illuminate\Database\Eloquent\Collection {#1484 ▼
#items: array:3 [▼
0 => App\Models\Product {#1497 ▼
#fillable: array:12 [▶]
#quarded: array:1 [▶]
+timestamps: false
#connection: "mysql"
#table: "products"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [▼
"name" => "Nazwa"
"slug" => "nazwa"
"id" => 1
"small_description" => "mini opis"
]
#original: array:4 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▶]
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => App\Models\Product {#1496 ▼
#fillable: array:12 [▶]
#quarded: array:1 [▶]
+timestamps: false
#connection: "mysql"
#table: "products"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [▼
"name" => "Nazwa"
"slug" => "nazwa"
"id" => 1
"small_description" => "mini opis"
]
#original: array:4 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▶]
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
2 => App\Models\Product {#1493 ▼
#fillable: array:12 [▶]
#quarded: array:1 [▶]
+timestamps: false
#connection: "mysql"
#table: "products"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [▼
"name" => "Nazwa"
"slug" => "nazwa"
"id" => 1
"small_description" => "mini opis"
]
#original: array:4 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▶]
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
我的模型:
class Product extends Model
{
use ScopeActiveTrait;
use Slugable;
public function setNameAttribute($value)
{
$this->attributes['name'] = $value;
$this->attributes['slug'] = $this->makeSlug($value);
}
protected $fillable = ['delivery_time', 'product_type', 'name', 'title', 'description', 'keywords', 'content', 'vat_id', 'category_id', 'enable', 'slug', 'small_description'];
protected $quarded = ['id'];
public $timestamps = false;
public function vat()
{
return $this->belongsTo('App\Models\VAT', 'vat_id');
}
public function category()
{
return $this->belongsTo('App\Models\Category', 'category_id');
}
public function selectedCategory()
{
return $this->hasMany('App\Models\SelectedProductCategory', 'product_id', 'id');
}
public function related()
{
return $this->belongsTo('App\Models\RelatedProduct');
}
public function features()
{
return $this->hasMany('App\Models\SelectedProductFeature');
}
public function frontImage()
{
return $this->hasMany('App\Models\UploadImage', 'file_id', 'id')->orderBy('order', 'ASC')->where('file_type', 'products');
}
}
class SelectedProductCategory extends Model
{
protected $fillable = ['product_id', 'category_id'];
protected $quarded = ['id'];
}
和架构:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name', 155);
$table->string('title', 155);
$table->string('description', 155)->nullable();
$table->string('keywords', 155)->nullable();
$table->longText('content')->nullable();
$table->string('delivery_time', 155)->nullable();
$table->string('small_description', 155)->nullable();
$table->smallInteger('vat_id')->unsigned()->default(1);
$table->foreign('vat_id')->references('id')->on('vat');
$table->bigInteger('category_id')->unsigned()->default(0);
//$table->foreign('category_id')->references('id')->on('categories');
$table->char('enable', 1)->default(0);
$table->char('product_type', 1)->default(0);
$table->string('promo_desc', 3)->nullable();
$table->string('slug', 160)->nullable();
$table->engine = "InnoDB";
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('category_name', 155);
$table->string('description', 155)->nullable();
$table->string('keywords', 155)->nullable();
$table->longText('content')->nullable();
$table->char('enable', 1)->default(0);
$table->bigInteger('order')->default(0);
$table->string('slug', 160)->nullable();
NestedSet::columns($table);
$table->engine = "InnoDB";
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
Schema::create('selected_product_categories', function (Blueprint $table) {
$table->id();
$table->bigInteger('product_id')->unsigned()->default(0);
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->bigInteger('category_id')->unsigned()->default(0);
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
在数据库中,我只有 1 条记录:
Laravel 还给我 3 条记录。为什么我有重复?我该如何修复它?
请帮我。
解决方案
你有关系“selectedCategory”。那么为什么要使用左连接呢?使用此代码
public function getProductFromIdCategories($categories)
{
return $this->model->select('name', 'slug', 'products.id', 'small_description')
->with(['selectedCategory', 'frontImage', 'selectedCategory' => function($q) use ($categories){
$q->whereIn('category_id', $categories);
}])->active()->get();
}
推荐阅读
- javascript - 如何迭代特定值,如“card-type”:仅使用 javascript
- android - 不支持同时从多个进程中使用具有相同数据目录的 WebView。https://crbug.com/558377
- python - 当我用 python Openpyxl 编写 Countif 公式时出现了一些意想不到的结果
- html - 将网格中的文本与 svg 对齐
- python - start_background_task 在执行其他异步方法时卡住
- sql - 基于另一个表插入到第一个表
- bash - 使用“Windows 默认控制台窗口”重新安装 git bash 后找不到命令
- visual-studio - 无法为 .NET 4.7.2 Framework 项目安装 Microsoft.EntityFrameworkCore.SqlServer
- protocol-buffers - 在 loadrunner 或 C 中将 JSON 消息序列化和反序列化为 Protobuf 消息
- flutter - Flutter Qs:在 null 上调用了 getter 'length'