laravel雄辩的关系一对多返回null [英] laravel eloquent relation one to many returns null

查看:64
本文介绍了laravel雄辩的关系一对多返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The product data always return null when i get the incoming_goods data (belongsTo).

这是我的产品型号:

<?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模型:

Here is my Incoming_good Model:

<?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');
    }       
}

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

And here is my migration of that two 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');
    }
}

进来的商品迁移:

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

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 s匹配,Laravel需要外部要选择的键.由于您没有在选择列表中包含外键( product_id ),因此Laravel在检索相关记录后无法将它们匹配.因此,您所有的 product 关系将为空.将外键添加到选择列表中,您应该会很好.

In order to match up the eager loaded Products with the Incoming_goods, 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆