duanpei4455 2016-03-03 17:25
浏览 31

雄辩的急切加载不适用于我的命名约定

I have been stuck for most of the day getting an empty array any time I eager loaded product images while requesting products in my controller in Laravel.

public function ProductImages() {
        return $this->hasMany('App\ProductImage', 'product_id'); // this matches the Eloquent model
  }

I changed my code to make the FK 'test' and suddenly it has started returning the appropriate data I want back. I put the FK back to product_id but again am back to an empty array. Below are My product Model ProductImages Model and the migrations for both with the call Im making in the controlelr

Product Model

class Product extends Model
{
 protected $fillable = array('name', 'url_name', 'sku', 'description', 'short_description', 'enabled', 'track_inventory', 'stock_level', 'allow_backorder', 'updated_user_id' );

//protected $hidden = array('id');


// LINK THIS MODEL TO OUR DATABASE TABLE ---------------------------------
// Database table is not called my_products
protected $table = 'products';

// each  product HAS many product images
public function ProductImages() {
    return $this->hasMany('App\ProductImage', 'productId'); // this matches the Eloquent model
}
}

Product Images Model

class ProductImage extends Model
{
 protected $fillable = array('name', 'description', 'path', 'sort_order', 'product_id');
// DEFINE RELATIONSHIPS --------------------------------------------------
// each attribute HAS one product
public function Product() {
    return $this->belongsTo('App\Product', 'id'); // this matches the Eloquent model
}
}

Product Migration

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');

            $table->string('name');
            $table->string('url');
            $table->string('sku');
            $table->string('description');
            $table->string('short_description');
            $table->integer('enabled');
            $table->integer('track_inventory');
            $table->integer('stock_level');
            $table->integer('allow_backorder');
            $table->dateTime('updated_user_id');



            $table->timestamps();
        });
    }
}

Product Images Migration

class CreateProductImagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_images', function (Blueprint $table) {
            $table->increments('id');


            $table->string('name');
            $table->string('description');
            $table->string('path');
            $table->integer('sort_order');
            $table->integer('product_id')->unsigned();
            $table->foreign('product_id')->references('id')->on('products');

            $table->timestamps();
        });
    }
}

Product Controller Snippet

 public function index()
    {
        //
        $Products = Product::with('ProductImages','productTypes')->get();

        //dd($Products);

        return response()->json( $Products, 200);

    }

If you could help me understand why this strange behavior is happening i would be very grateful.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作
    • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
    • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
    • ¥50 mac mini外接显示器 画质字体模糊