laravel eloquent关系一对多返回null

时间:2017-10-30 13:37:27

标签: php mysql laravel relationship laravel-eloquent

当我获得incoming_goods数据(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');
    }       
}

这是我的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');
    }       
}

这是我对这两张桌子的迁移:

产品表迁移:

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

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

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

1 个答案:

答案 0 :(得分:4)

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

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