【发布时间】:2020-07-14 08:31:33
【问题描述】:
我是 Laravel 的初学者。我的mysql有问题。
我有这个代码:
- 型号:
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', 'main_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', 'main_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('main_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';
});
- 存储库:
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);
}])->whereIn('category_id', $categories)->active()->get();
}
在 $categories 中我有数组:
array:1 [▼
0 => 12
]
我的产品被分配到这个类别。
我有错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_id' in 'where clause' (SQL: select `name`, `slug`, `products`.`id`, `small_description` from `products` where `category_id` in (12) and `enable` = 1)
我不知道;不知道为什么。我在 selected_product_categories 中有这个专栏
我该如何解决这个问题?
【问题讨论】:
-
嗨歌剧。为什么产品架构中带有“category_id”的行被注释掉了?
标签: php laravel laravel-5 laravel-7