Php 雄辩而急切的加载不会';我无法使用我的命名约定
一天中的大部分时间里,当我在Laravel的控制器中请求产品时,我总是在渴望加载产品图像时获取一个空阵列Php 雄辩而急切的加载不会';我无法使用我的命名约定,php,laravel-5,eloquent,Php,Laravel 5,Eloquent,一天中的大部分时间里,当我在Laravel的控制器中请求产品时,我总是在渴望加载产品图像时获取一个空阵列 public function ProductImages() { return $this->hasMany('App\ProductImage', 'product_id'); // this matches the Eloquent model } 我更改了代码以进行FK“测试”,突然它开始返回我想要返回的相应数据。我将FK放回product_id,但再次返
public function ProductImages() {
return $this->hasMany('App\ProductImage', 'product_id'); // this matches the Eloquent model
}
我更改了代码以进行FK“测试”,突然它开始返回我想要返回的相应数据。我将FK放回product_id,但再次返回空数组。下面是我的产品模型ProductImages模型和两者的迁移,以及我在controlelr中拨打的电话
产品模型
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
}
}
产品形象模型
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
}
}
产品迁移
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();
});
}
}
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();
});
}
}
产品图像迁移
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();
});
}
}
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();
});
}
}
产品控制器代码段
public function index()
{
//
$Products = Product::with('ProductImages','productTypes')->get();
//dd($Products);
return response()->json( $Products, 200);
}
如果你能帮助我理解为什么会发生这种奇怪的行为,我将非常感激 仅更新使用product_id测试了多对多关系它起作用此更新在使用product_id时仍然不起作用。其a具有多个关系。仅更新使用product_id测试了多对多关系它起作用此更新在使用product_id时仍然不起作用。其a具有多个关系。