【问题标题】:laravel one to many relationships returning nulllaravel 一对多关系返回 null
【发布时间】:2019-07-24 03:53:51
【问题描述】:

有两个model.product和image 在我的产品模型中:

// one to many   relationship with images table 
public function images()
{
    return $this->hasMany('App\image');
}

图像模型

public function product()
{
    return $this->belongsTo('App\product');


}

产品控制器

public function productDetail($slug)
{
    $product = product::where([
      ['slug',$slug],
      ['seller_id' ,Auth::id()],
    ])->first();
    //$storagePath = Storage::get(['images']);
    //get the image of that product 
    //$image   = asset('storage/product_images'.$product->images);



    if($product)
    {
      $image    = Storage::url($product->images); // give the image path from product table

      //give images from the image table 
      $product_image   = \App\product::find(11)->images;
         $arr = array();

          foreach(\App\product::find($product->id)->images() as $i)
          {
            array($arr,$i->image);
          }

          dd($arr);  // problem returning always null 






      return view('backEnd.seller.product_detail',compact('product','image')); 
    }

问题陈述: 在我的控制器中,当我尝试获取特定产品的所有图像时,我得到了 Null 。我想在一天前解决这个问题。请帮助我,我错过了什么?

镜像表迁移

public function up()
{
    Schema::create('images', function (Blueprint $table){
        $table->increments('id');
        $table->unsignedInteger('product_id');
        $table->string('image');
        $table->timestamps();
    });
}

产品表迁移

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('seller_id');
        $table->unsignedInteger('category_id');
        $table->string('product');
        $table->text('discription');
        $table->string('type')->nullable();
        $table->date('date');
        $table->string('images');
        $table->string('slug');
        $table->integer('sold_qty')->default(0);
        $table->timestamps();
    });
}

注意: 我已确保在我的图像表中有 5 条 product_id 11 记录。请帮助谢谢

【问题讨论】:

    标签: database null foreign-keys relationship laravel-5.6


    【解决方案1】:

    您必须在数据库中建立关系。您可以通过将其添加到您的图像迁移来做到这一点:

    $table->foreign('product_id')->references('id')->on('product');
    

    【讨论】:

      【解决方案2】:

      我假设您的型号名称是 ProductImage

      请检查以下更改是否会给您想要的...

      return $this->hasMany('App\Image');
      

      请注意,型号名称以大写字母开头,

      return $this->belongsTo('App\Product');
      

      和@steve-trap 提到的数据库约束是不必要的。无论如何,它会引入一个约束,因此您无法为不存在的产品添加图像。

      然后在控制器中:

      foreach (App\Product::find($product->id)->images as $image) {
          $arr[] = $image->image;
      }
      

      【讨论】:

        【解决方案3】:

        我通过以下方式解决了这个问题:

        1. 将模型名称更改为大写。
        2. 将产品表列名称图像更改为覆盖。
        3. 将方法 images() 更改为 Product Model 中的图片

        结论: 如果您使用列名,则不要使用该列名来建立关系。 并且总是写以大写开头的模型名称。

        【讨论】:

          猜你喜欢
          • 2012-11-26
          • 2013-10-21
          • 2018-04-11
          • 2014-11-24
          • 1970-01-01
          • 1970-01-01
          • 2019-01-12
          • 2014-10-23
          • 2014-08-21
          相关资源
          最近更新 更多