目前,我正在创建我的产品链接到我的类别,直接从我的代码在我的种子做->categories()->attach(1)在每个产品的末尾。
从我的数据库中,我可以创建一个产品,但我不能将它们与外键链接到一个已经在category_product_table中的类别。
我有三张桌子:products,categories和category_product。
2020_04_09_073846_create_products_table
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->string('name');
$table->string('slug');
$table->string('category');
$table->string('description');
$table->string('releaseDate');
$table->float('price');
$table->timestamps();
});
}ProductSeeder
<?php
use Illuminate\Database\Seeder;
use App\Product;
class ProductSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Product::create([
'name' => 'Halo 5',
'slug' => 'halo-5',
'category_id' => '1',
'category' => 'Xbox One',
'description' => "Halo 5: Guardians sur Xbox One est un FPS mettant en scène les aventures du Master Chief et d'un nouveau personnage, le Spartan Jameson Locke. ",
'releaseDate' => '27 octobre 2015',
'price' => '54.99'
]);2020_05_02_201337_create_categories_table
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('slug')->unique();
$table->timestamps();
});
}CategorieSeeder
<?php
use Carbon\Carbon;
use App\Category;
use Illuminate\Database\Seeder;
class CategorieSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$now = Carbon::now()->toDateTimeString();
DB::table('categories')->insert(
array(
array(
'name' => 'Xbox',
'slug' => 'xbox',
),
array(
'name' => 'Playstation',
'slug' => 'playstation',
),
array(
'name' => 'PC',
'slug' => 'pc',
),
array(
'name' => 'Switch',
'slug' => 'switch',
),
)
);
}
}2020_05_03_105839_create_category_product_table
public function up()
{
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->nullable();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}Product.php
class Product extends Model
{
public function categories()
{
return $this->AsOne('App\Category');
}
}Category.php
class Category extends Model
{
public function products()
{
return $this->belongsToMany('App\Product');
}
}HomeController
public function public(){
if (request()->category) {
$home = Product::with('categories')->whereHas('categories', function ($query){
$query->where('slug', request()->category);
})->get();
$categories = Category::all();
$categoryName = $categories->where('slug', request()->category)->first()->name;
} else {
$home = Product::inRandomOrder()->paginate(9);
$categories = Category::all();
$categoryName = 'Featured';
}
return view('home.index')->with([
'home' => $home,
'categories' => $categories,
'categoryName' => $categoryName,
'mode' => 'public'
]);如果有人能帮我,谢谢你的帮助!
发布于 2022-10-16 20:39:27
php make: create_products_table迁移--create=products迁移:直接使用此命令并添加
https://stackoverflow.com/questions/61645691
复制相似问题