首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel :在数据库中创建带有类别的新产品

Laravel :在数据库中创建带有类别的新产品
EN

Stack Overflow用户
提问于 2020-05-06 21:26:04
回答 1查看 3.2K关注 0票数 1

目前,我正在创建我的产品链接到我的类别,直接从我的代码在我的种子做->categories()->attach(1)在每个产品的末尾。

从我的数据库中,我可以创建一个产品,但我不能将它们与外键链接到一个已经在category_product_table中的类别。

我有三张桌子:productscategoriescategory_product

2020_04_09_073846_create_products_table

代码语言:javascript
复制
    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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->timestamps();
        });
    }

CategorieSeeder

代码语言:javascript
复制
<?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

代码语言:javascript
复制
    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

代码语言:javascript
复制
class Product extends Model
{
    public function categories()
    {
        return $this->AsOne('App\Category');
    }
}

Category.php

代码语言:javascript
复制
class Category extends Model
{  
    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
}

HomeController

代码语言:javascript
复制
    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'
        ]);

如果有人能帮我,谢谢你的帮助!

EN

回答 1

Stack Overflow用户

发布于 2022-10-16 20:39:27

php make: create_products_table迁移--create=products迁移:直接使用此命令并添加

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61645691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档