Attempting to Retrieve Property of Non-Object in Laravel 7’s belongsTo Relation

In order to access the properties of the relations, you can refer to the Laravel Relationship Documentation provided at https://laravel.com/docs/5.1/eloquent-relationships#one-to-one. Additionally, you can utilize the “belongsTo” method in Laravel to establish a relationship between two IDs. For further assistance, you can visit https://stackoverflow.com. Furthermore, the Eloquent Relationships section on the Laravel website at https://laravel.com provides valuable information. I have a code snippet that requires accessing the “categories” on the user object and returning a Laravel relation. Lastly, I am interested in achieving this relationship with both videos.


Question:

Prior to commencing, I have already reviewed the suggested “Similar questions” and while there are numerous queries resembling mine, I have yet to discover the solutions. Additionally, I am relatively new to learning Laravel, having only started a few days ago, so I kindly request your patience.

I am currently utilizing the most recent version of the Laravel framework (7.18.0), and I have a total of three tables.

pirates
----------
pirate_id serial NOT NULL, -- PK
pirate_name character varying(200) NOT NULL,
beard_size integer NOT NULL,
beard_color integer NOT NULL,
beard_sizes
----------
beard_size_id serial NOT NULL, -- PK
beard_size character varying(100) NOT NULL
beard_colors
----------
beard_color_id serial NOT NULL, -- PK
beard_color character varying(100) NOT NULL

I utilized
php artisan command
to create the model and attempted to specify the models.


Pirate.php

belongsTo('AppBeardSize', 'beard_size_id');
    }
    
    public function beardColor()
    {
        return $this->belongsTo('AppBeardColor', 'beard_color_id');
    }
}


BeardSize.php

hasMany('AppPirate', 'pirate_id');
    }
}


BeardColor.php

hasMany('AppPirate', 'pirate_id');
    }
}

This is the controller I used to display the data from my ‘pirates’ table.

The file named PirateController.php needs to be rephrased.

 $pirates]);
    }
}

and here is my view:


main.blade.php

@extends('pirate')
@section('main_nav', 'This is our ship')
@section('main_content')

List of our crew:

@foreach($pirates as $ind => $pirate) @endforeach
No. Name Beard size Beard color    
{{ ($ind + 1) . '.' }} {{ $pirate->pirate_name }} {{ $pirate->beardSize->beard_size }} {{ $pirate->beardColor->beard_color }} edit delete
{{ $pirates->links() }} @endsection

Whenever I attempted to retrieve the data from the relation table (

beard_sizes

and

beard_colors

), I encountered errors, specifically either

Trying to get property 'beard_size' of non-object

or

Trying to get property 'beard_color' of non-object

. It is worth noting that the columns

beard_size

and

beard_color

in the

pirates

table cannot be null.

In relation to the
field names
in the
database tables
, the documentation specifies the required format for primary keys, such as

id

. However, in my upcoming project, I am working with a pre-existing database that consists of over 100 tables, making it impossible to alter the column names.


Solution 1:

Based on the schema you have provided, the following relationships are required.

belongsTo('AppBeardSize', 'beard_size', 'beard_size_id');
    }
    
    public function beardColor()
    {
        return $this->belongsTo('AppBeardColor', 'beard_color', 'beard_color_id');
    }
}

The structure of a
belongsTo relationship
is as follows:
“””.

     return $this->belongsTo(Related::class, 'key_on_own_table', 'key_on_related_table');


Solution 2:


It is likely that

NOT NULL

is not enough to be used as a primary key. Instead, it should be set to

UNSIGNED

,

BIGINT

, and enabled with

AUTOINCREMENT

. The actual name of the column is not significant; typically, it can be defined as

protected $primaryKey = 'pirate_id';

, which you already have.

The representation of the one-to-many relationship of

AppPirate


should be as follows:

In reality, it’s a one-to-one ratio, meaning that each pirate possesses only one beard.

public function beardSize()
{
    return $this->hasOne('AppBeardSize', 'beard_size_id');
}
public function beardColor()
{
    return $this->hasOne('AppBeardColor', 'beard_color_id');
}

Frequently Asked Questions

Posted in Php