@Terroris337

Laravel ORM выводит данные в непраильном формате?

Есть две табилици:
products
additives
и pivot: additive_product

Модели:
Product
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = "products";

    public function additives()
    {
        return $this->belongsToMany(Additive::class);
    }
}

Additive
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Additive extends Model
{
    protected $table = "additives";

    public function products()
    {
        return $this->belongsToMany(Product::class);
    }
}


Получаю продукт с связью:
$product = Product::find($request->post('id'))->additives()->get();
        echo "<pre>";
        print_r($product);
        echo "</pre>";


При получении продукта с связью выводит огромный массив объектов:
<pre>Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => App\Models\Additive Object
                (
                    [table:protected] => additives
                    [connection:protected] => mysql
                    [primaryKey:protected] => id
                    [keyType:protected] => int
                    [incrementing] => 1
                    [with:protected] => Array
                        (
                        )

                    [withCount:protected] => Array
                        (
                        )

                    [perPage:protected] => 15
                    [exists] => 1
                    [wasRecentlyCreated] => 
                    [attributes:protected] => Array
                        (
                            [id] => 3
                            [name] => Test1
                            [image] => Test1
                            [visibility] => 1
                            [price] => 2
                            [created_at] => 
                            [updated_at] => 
                        )

                    [original:protected] => Array
                        (
                            [id] => 3
                            [name] => Test1
                            [image] => Test1
                            [visibility] => 1
                            [price] => 2
                            [created_at] => 
                            [updated_at] => 
                            [pivot_product_id] => 1
                            [pivot_additive_id] => 3
                        )

                    [changes:protected] => Array
                        (
                        )

                    [casts:protected] => Array
                        (
                        )

                    [classCastCache:protected] => Array
                        (
                        )

                    [dates:protected] => Array
                        (
                        )

                    [dateFormat:protected] => 
                    [appends:protected] => Array
                        (
                        )

                    [dispatchesEvents:protected] => Array
                        (
                        )

                    [observables:protected] => Array
                        (
                        )

                    [relations:protected] => Array
                        (
                            [pivot] => Illuminate\Database\Eloquent\Relations\Pivot Object
                                (
                                    [incrementing] => 
                                    [guarded:protected] => Array
                                        (
                                        )

                                    [connection:protected] => mysql
                                    [table:protected] => additive_product
                                    [primaryKey:protected] => id
                                    [keyType:protected] => int
                                    [with:protected] => Array
                                        (
                                        )

                                    [withCount:protected] => Array
                                        (
                                        )

                                    [perPage:protected] => 15
                                    [exists] => 1
                                    [wasRecentlyCreated] => 
                                    [attributes:protected] => Array
                                        (
                                            [product_id] => 1
                                            [additive_id] => 3
                                        )

                                    [original:protected] => Array
                                        (
                                            [product_id] => 1
                                            [additive_id] => 3
                                        )

                                    [changes:protected] => Array
                                        (
                                        )

                                    [casts:protected] => Array
                                        (
                                        )

                                    [classCastCache:protected] => Array
                                        (
                                        )

                                    [dates:protected] => Array
                                        (
                                        )

                                    [dateFormat:protected] => 
                                    [appends:protected] => Array
                                        (
                                        )

                                    [dispatchesEvents:protected] => Array
                                        (
                                        )

                                    [observables:protected] => Array
                                        (
                                        )

                                    [relations:protected] => Array
                                        (
                                        )

                                    [touches:protected] => Array
                                        (
                                        )

                                    [timestamps] => 
                                    [hidden:protected] => Array
                                        (
                                        )

                                    [visible:protected] => Array
                                        (
                                        )

                                    [fillable:protected] => Array
                                        (
                                        )

                                    [pivotParent] => App\Models\Product Object
                                        (
                                            [table:protected] => products
                                            [connection:protected] => mysql
                                            [primaryKey:protected] => id
                                            [keyType:protected] => int
                                            [incrementing] => 1
                                            [with:protected] => Array
                                                (
                                                )

                                            [withCount:protected] => Array
                                                (
                                                )

                                            [perPage:protected] => 15
                                            [exists] => 1
                                            [wasRecentlyCreated] =>


Я что-то делаю не правильно или чего-то не понимаю?
Как изменить формат получения данных?
  • Вопрос задан
  • 45 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы