красноречивое отношение laravel один ко многим возвращает 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');
}
}

входящая миграция товаров:

<?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. Надеюсь, вы можете помочь мне 🙂

0

Решение

Чтобы соответствовать загруженному Productс Incoming_goods, Laravel нужно выбрать внешний ключ. Так как вы не включили внешний ключ (product_id) в списке выбора, Laravel не может сопоставить связанные записи после их получения. Итак, все ваши product отношения будут пустыми. Добавьте в список выбора внешний ключ, и все будет хорошо.

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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector