Как получить из таблицы доставки все данные из таблицы продуктов и таблицы элементов в laravel eloquent - PullRequest
1 голос
/ 08 февраля 2020

// вот таблица поставок

Schema::create('deliveries', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('product_id');
            $table->unsignedInteger('qty');
            $table->string('person_name');
            $table->string('designation');
            $table->string('Office');
            $table->string('mobile');
            $table->date('date');
            $table->timestamps();
});

// здесь таблица продуктов

 Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('category_id');
            $table->unsignedInteger('item_id');
            $table->unsignedInteger('brand_id');
            $table->string('item_model')->nullable();
            $table->unsignedInteger('qty');
            $table->unsignedInteger('item_price');
            $table->unsignedInteger('total_price');
            $table->timestamps();
});

// здесь таблица товаров

Schema::create('items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('category_id');
            $table->string('item_name')->unique();;
});

как получить данные таблицы товаров и изделий из таблицы поставок по красноречивой модели? У меня есть 3 файла модели: Delivery. php, Item. php и Product. php Я хочу получить доступ к DeliveryController. php индексным методом. Но я не могу получить доступ к item_name

public function index()
{
    return Delivery::with('user')
                    ->with('product')
                    ->orderby('id', 'desc')->get();
}

<tr role="row" class="odd" v-for="(delivery, index) in deliveries" :key="index">
                        <td>{{ index+1 }}</td>
                        <td>{{ delivery.product.item_name}}</td>
</tr>

Ответы [ 2 ]

0 голосов
/ 09 февраля 2020

Я думаю, что вы забыли определить это отношение в Модели доставки, добавьте это отношение в Модель доставки

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

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

после добавления вы можете получить доступ к user и product при загрузке ресурсов доставки

Delivery::with(['user','product'])->orderby('id', 'desc')->get();
0 голосов
/ 08 февраля 2020

Прежде всего вы должны добавить ограничения внешнего ключа для таблиц. Вот так

Schema::create('deliveries', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('product_id');
            $table->unsignedInteger('qty');
            $table->string('person_name');
            $table->string('designation');
            $table->string('Office');
            $table->string('mobile');
            $table->date('date');
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users');

            $table->foreign('product_id')
                ->references('id')
                ->on('products')
});

И для всех таблиц вот так. После этого и при условии, что вы правильно определили отношения на Доставка Модель, которую вы можете сделать следующим образом:

public function index()
{
    return Delivery::with(['user','product'])
                    ->orderby('id', 'desc')->get();
}
...