Laravel 8 Relation not working on null object but the relationship isn't null

Viewed 172

I have relation Products and to their related Products. Output shows the relation actually exists but the blade breaks. There is something I'm missing but I don't know what. Can you help?

Here is the ProductsRelations model:

...
class ProductsRelations extends Model
{
    use HasFactory;

    protected $table='products_relations';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'client_id'
      , 'product_id'
      , 'relation'

    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [];

    /**
     * Relations belong to Products relationship
     */

    public function product()
    {
        return $this->belongsTo(Products::class, 'product_id', 'id');
    }

    /**
     * Relations belong to Products relationship
     */

    public function relation()
    {
        return $this->belongsTo(Products::class, 'relation', 'id');
    }
...

Here is the Products model:

...
class Products extends Model
{
    use HasFactory;

    protected $table='products';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'title'
      , 'summary'
      , 'client_id'
      , 'category_id'
      , 'price'
      , 'seo_keywords'
      , 'seo_descriptions'
      , 'body'
      , 'published'
      , 'featured'
      , 'bestselling'
      , 'in_stock'
      , 'slug'
      
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [];
    
    protected $visible = [];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'published'=>'boolean'
      , 'featured'=>'boolean'
      , 'bestselling'=>'boolean'
      , 'in_stock'=>'boolean'
      
    ];


    /**
     * Products belong to Clients relationship
     */
    
    public function client()
    {
        return $this->belongsTo(Clients::class, 'client_id', 'id');
    }

    /**
     * Products belong to Categories relationship
     */
    
    public function category()
    {
        return $this->belongsTo(ProductsCategories::class, 'category_id', 'id');
    }
...

Here is what I'm using on the Controller:

...
return view('home.relations.index')
            ->with('relations', ProductsRelations::class)
            ->with('active_count',
                ProductsRelations::where([
                    'client_id'=>auth()->user()->client_id
                ])->count()
            )
            ->with('records',
                ProductsRelations::query()
                    ->with(['product'=>function($query)
                    {   
                        $query->select('id', 'title');
                    }])
                    ->with(['relation'=>function($query)
                    {   
                        $query->select('id', 'title');
                    }])
                    ->where(['client_id'=>auth()->user()->client_id])
                    ->get()
            );
...

This is a part of what I get once outputted:

Illuminate\Database\Eloquent\Collection {#310 ▼
  #items: array:2 [▼
    0 => App\Models\ProductsRelations {#304 ▼
      #table: "products_relations"
      #fillable: array:3 [▶]
      #hidden: []
      #casts: []
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:6 [▼
        "id" => 46
        "client_id" => 1
        "product_id" => 181
        "relation" => 190
        "created_at" => "2022-05-24 10:23:49"
        "updated_at" => "2022-05-24 10:23:49"
      ]
      #original: array:6 [▶]
      #changes: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:2 [▼
        "product" => App\Models\Products {#311 ▼
          #table: "products"
          #fillable: array:13 [▶]
          #hidden: []
          #visible: []
          #casts: array:4 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          +preventsLazyLoading: false
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #escapeWhenCastingToString: false
          #attributes: array:2 [▼
            "id" => 181
            "title" => "2427 Dickens Haven"
          ]
          #original: array:2 [▶]
          #changes: []
          #classCastCache: []
          #attributeCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #guarded: array:1 [▶]
        }
        "relation" => App\Models\Products {#314 ▼
          #table: "products"
          #fillable: array:13 [▶]
          #hidden: []
          #visible: []
          #casts: array:4 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          +preventsLazyLoading: false
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #escapeWhenCastingToString: false
          #attributes: array:2 [▼
            "id" => 190
            "title" => "388 Torp Throughway Suite 364"
          ]
          #original: array:2 [▶]
          #changes: []
          #classCastCache: []
          #attributeCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #guarded: array:1 [▶]
        }
      ]

What am I missing please, can you pin-point it out to me.

0 Answers
Related