Home > Mobile >  I have a belongs to relationship in laravel 8 and it returns null when I am trying to use that relat
I have a belongs to relationship in laravel 8 and it returns null when I am trying to use that relat

Time:10-03

I am using Laravel 8 and I have the following very simple models and migrations,

Author Model

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
class Author extends Model
{
    use HasFactory;
 
    public function profile()
    {
        return $this->hasOne('App\Models\Profile');
    }
}

Profile Model

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
class Profile extends Model
{
    use HasFactory;
 
    public function author()
    {
        $this->belongsTo('App\Models\Author');
    }
}

create_authors_table migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAuthorsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('authors', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('authors');
    }
}  

create_profiles_table migration

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
 
            $table->unsignedInteger('author_id')->unique();
            $table->foreign('author_id')->references('id')->on('authors');
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('profiles');
    }
}

Despite having the above models and migrations, if I create a new Profile model in tinker shell and do $profile->author() it returns null. I can not understand from where the problem is coming from.

(I tried changing $this->belongsTo('App\Models\Author'); to $this->belongsTo(Author::class); and $this->hasOne('App\Models\Profile'); to $this->hasOne(Profile::class); and restarting the tinker. The issue still persists. I even tried changing all the unsigned integer keys to unsigned big integers and the issue still persists.)

CodePudding user response:

So I figured out the problem. The solution is simple, I have forgotten to add the all important return statement inside the author() method inside the Profile model.

It should be, return $this->belongsTo('App\Models\Author'); instead of $this->belongsTo('App\Models\Author');

CodePudding user response:

try with this one return $this->belongsTo(Author::class, 'author_id', 'id'); instead of $this->belongsTo('App\Models\Author');

  • Related