Laravel 类别
Laravel Categories
我正在使用 laravel 学习 php 并尝试在我的项目中为多个项目实施类别和子类别。
例如:我的项目中有书籍、手机
书籍有自己的类别和子类别。手机也是如此。
我添加了另一个 table 关系
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->unique(array('product_id', 'category_id'));
// foreign key constraints are optional (but pretty useful, especially with cascade delete
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
类别数据库架构
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('parent_id')->nullable()->index();
$table->string('title')->unique();
$table->string('slug')->unique();
$table->string('description')->nullable();
$table->string('keywords')->nullable();
$table->timestamps();
});
category.php(型号)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
class Category extends Model
{
use Sluggable;
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable()
{
return [
'slug' => [
'source' => 'title'
]
];
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'categories';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = [
'parent_id', 'title', 'description', 'slug'
];
public function parent()
{
return $this->belongsTo('Category', 'parent_id');
}
public function children()
{
return $this->hasMany('Category', 'parent_id');
}
public function categoryProduct(){
return $this->belongsToMany('CategoryProduct');
}
public function product(){
return $this->belongsToMany('Product');
}
}
我的方法是否正确,因为我没有找到适合这种方法的教程。我需要创建一个 CategoryProduct.php 模型并引用
public function categories(){
return $this->belongsToMany('Category');
}
public function products(){
return $this->belongsToMany('Product');
}
不,您不需要为枢轴创建模型 table。 Eloquent 有许多方法可以使使用数据透视表 tables 变得轻而易举。在您的情况下,您不需要模型。
但有时,当您使用枢轴 table 很多其他列时,最好为枢轴 table 创建一个模型。
我正在使用 laravel 学习 php 并尝试在我的项目中为多个项目实施类别和子类别。 例如:我的项目中有书籍、手机 书籍有自己的类别和子类别。手机也是如此。
我添加了另一个 table 关系
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->unique(array('product_id', 'category_id'));
// foreign key constraints are optional (but pretty useful, especially with cascade delete
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
类别数据库架构
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('parent_id')->nullable()->index();
$table->string('title')->unique();
$table->string('slug')->unique();
$table->string('description')->nullable();
$table->string('keywords')->nullable();
$table->timestamps();
});
category.php(型号)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
class Category extends Model
{
use Sluggable;
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable()
{
return [
'slug' => [
'source' => 'title'
]
];
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'categories';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = [
'parent_id', 'title', 'description', 'slug'
];
public function parent()
{
return $this->belongsTo('Category', 'parent_id');
}
public function children()
{
return $this->hasMany('Category', 'parent_id');
}
public function categoryProduct(){
return $this->belongsToMany('CategoryProduct');
}
public function product(){
return $this->belongsToMany('Product');
}
}
我的方法是否正确,因为我没有找到适合这种方法的教程。我需要创建一个 CategoryProduct.php 模型并引用
public function categories(){
return $this->belongsToMany('Category');
}
public function products(){
return $this->belongsToMany('Product');
}
不,您不需要为枢轴创建模型 table。 Eloquent 有许多方法可以使使用数据透视表 tables 变得轻而易举。在您的情况下,您不需要模型。
但有时,当您使用枢轴 table 很多其他列时,最好为枢轴 table 创建一个模型。