T
T
Terroris3372020-10-09 12:47:26
Laravel
Terroris337, 2020-10-09 12:47:26

Laravel ORM outputting data in wrong format?

There are two tables:
products
additives
and pivot: additive_product

Models:
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);
    }
}


I receive a product with a connection:
$product = Product::find($request->post('id'))->additives()->get();
        echo "<pre>";
        print_r($product);
        echo "</pre>";


When getting a product with a link, outputs a huge array of objects:
<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] =>


Am I doing something wrong or do I not understand something?
How to change the format for receiving data?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question