【问题标题】:Call to a member function detail() on bool laravel 8在 bool laravel 8 上调用成员函数 detail()
【发布时间】:2021-01-04 05:11:47
【问题描述】:

我的产品型号

    public function detail(){

    return $this->hasOne('App\Models\ProductDetails');
}

我的产品详情模型

protected $table='product_details';
public $timestamps=false;
protected $fillable=['product_id' , 'show_slider','show_opportunity_day','show_featured','show_bestseller','show_discounted'];

public function detail(){

    return $this->belongsTo('App\Models\Products');
}

我的 ProductTableSeeder

       DB::table('products')->truncate();
    DB::table('product_details')->truncate();
    $products=[
        ["product_name"=>"Domates Çorbası", "slug"=>'Domates Çorbası', 'content'=>"en iyi ürün","price"=>20 ],
        ["product_name"=>"Yayla Çorbası", "slug"=>"Yayla Çorbası", "content"=>"en iyi ürün","price"=>30],
        ["product_name"=>"Kelle Paça Çorbası", "slug"=>"Kelle Paça Çorbası", "content"=>"en iyi ürün","price"=>20],
        ["product_name"=>"Tarhana Çorbası","slug"=>"Tarhana Çorbası", "content"=>"en iyi ürün","price"=>40 ],
        ["product_name"=>"Tavuk Çorbası", "slug"=>"Tavuk Çorbası", "content"=>"en iyi ürün","price"=>10  ],
        ["product_name"=>"Mercimek Çorbası", "slug"=>"Mercimek Çorbası", "content"=>"en iyi ürün","price"=>15 ],
        ["product_name"=>"Kereviz Çorbası", "slug"=>"Kereviz Çorbası", "content"=>"en iyi ürün","price"=>25 ],
        ["product_name"=>"Şehriyeli Çorba", "slug"=>"Şehriyeli Çorba", "content"=>"en iyi ürün","price"=>22 ],
        ["product_name"=>"Havuçlu Kabak Çorbası", "slug"=>"Havuçlu Kabak Çorbası", "content"=>"en iyi ürün","price"=>14],
        ["product_name"=>"Ayran Çorbası", "slug"=>"Ayran Çorbası", "content"=>"en iyi ürün","price"=>20 ]
    ];
    foreach($products as $product){
    DB::table('products')->insert($products);
    $detail=$product->detail()->create([
        'show_slider'=>rand(0,1),
        'show_opportunity_day'=>rand(0,1),
        'show_featured'=>rand(0,1),
        'show_bestseller'=>rand(0,1),
        'show_discounted'=>rand(0,1)
    ]);
    }

$ 产品未在播种器部分定义,当我保存并刷新页面时出现错误---> 在 bool 上调用成员函数 detail()

【问题讨论】:

    标签: laravel-8


    【解决方案1】:

    像这样编辑种子文件

     foreach($products as $product){
        $product=Products::create($product);
        $detail=$product->detail()->create([
            'show_slider'=>rand(0,1),
            'show_opportunity_day'=>rand(0,1),
            'show_featured'=>rand(0,1),
            'show_bestseller'=>rand(0,1),
            'show_discounted'=>rand(0,1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2022-01-08
      • 2021-05-04
      • 2021-07-20
      • 2021-03-20
      相关资源
      最近更新 更多