laravel雄辩的关系一对多返回null

Dim*_*rea 1 php mysql relationship laravel laravel-eloquent

当我收到传入商品数据(belongsTo)时,产品数据始终返回null。

这是我的产品型号:

<?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');
    }       
}
Run Code Online (Sandbox Code Playgroud)

这是我的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');
    }       
}
Run Code Online (Sandbox Code Playgroud)

这是我对这两个表的迁移:

产品表迁移:

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

entry_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');
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码,用于显示incomong_goods数据和产品(relationshipToTo关系):

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

我尝试使用alies,但是仍然返回产品数据null。希望你能帮我 :)

pat*_*cus 5

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

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