laravel eloquent 一对多关系 returns null

laravel eloquent relation one to many returns null

当我获取 incoming_goods 数据(属于)时,产品数据始终 return 为空。

这是我的产品型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;

    protected $guarded = [
        'id', 'created_at', 'updated_at', 'deleted_at',
    ];

    public function transaction_details()
    {
        return $this->hasMany('App\Transaction_detail');
    }

    public function incoming_goods()
    {
        return $this->hasMany('App\Incoming_good');
    }       
}

这是我的 Incoming_good 型号:

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Incoming_good extends Model
{
    protected $guarded = [
        'id', 'created_at', 'updated_at',
    ];

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

这是我对那两个 table:

的迁移

产品table迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\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', 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 迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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 数据和产品(关系 belongsTo):

$data = Incoming_good::select('id', 'stock', 'note', 'created_at')->with('product')->get();

我已经尝试使用 alies,但它仍然 return 产品数据为空。希望你能帮助我:)

为了匹配预先加载的 Products 与 Incoming_goods,Laravel 需要外键 selected。由于您没有在select列表中包含外键(product_id),因此Laravel检索后无法匹配相关记录。所以,你所有的 product 关系都将是空的。将外键添加到 select 列表中,应该就可以了。

$data = Incoming_good::select('id', 'product_id', 'stock', 'note', 'created_at')
    ->with('product')
    ->get();