問題描述
當我得到incoming_goods數據(belongsTo)時,產品數據總是返回null.
The product data always return null when i get the incoming_goods data (belongsTo).
這是我的產品型號:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
class Product extends Model
{
use SoftDeletes;
protected $guarded = [
'id', 'created_at', 'updated_at', 'deleted_at',
];
public function transaction_details()
{
return $this->hasMany('AppTransaction_detail');
}
public function incoming_goods()
{
return $this->hasMany('AppIncoming_good');
}
}
這是我的 Incoming_good 模型:
Here is my Incoming_good Model:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Incoming_good extends Model
{
protected $guarded = [
'id', 'created_at', 'updated_at',
];
public function product()
{
return $this->belongsTo('AppProduct');
}
}
這是我對那兩個表的遷移:
And here is my migration of that two table:
產品表遷移:
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->integer('price');
$table->integer('stock')->nullable();
$table->integer('available');
$table->string('image1', 190)->nullable();
$table->string('image2', 190)->nullable();
$table->string('image3', 190)->nullable();
$table->string('image4', 190)->nullable();
$table->string('image5', 190)->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
incoming_goods 遷移:
incoming_goods migration:
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateTableIncomingGoods extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('incoming_goods', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id');
$table->integer('stock');
$table->text('note')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('incoming_goods');
}
}
這是我的代碼,用于顯示 incomong_goods 數據和產品(關系屬于):
Here is my code to show incomong_goods data and product (relation belongsTo):
$data = Incoming_good::select('id', 'stock', 'note', 'created_at')->with('product')->get();
我嘗試使用 alies,但它仍然返回 null 的產品數據.希望你能幫助我:)
I've try to use alies, but still it return the product data null. Hope you can help me :)
推薦答案
為了將預先加載的 Product
與 Incoming_good
相匹配,Laravel 需要外部要選擇的鍵.由于您沒有在選擇列表中包含外鍵 (product_id
),Laravel 在檢索它們后無法匹配相關記錄.因此,您所有的 product
關系都將為空.將外鍵添加到選擇列表中,你應該就好了.
In order to match up the eager loaded Product
s with the Incoming_good
s, Laravel needs the foreign key to be selected. Since you did not include the foreign key (product_id
) in the select list, Laravel can't match up the related records after retrieving them. So, all your product
relationships will be empty. Add in the foreign key to the select list, and you should be good.
$data = Incoming_good::select('id', 'product_id', 'stock', 'note', 'created_at')
->with('product')
->get();
這篇關于laravel 雄辯的一對多關系返回 null的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!