【问题标题】:I have a belongs to relationship in laravel 8 and it returns null when I am trying to use that relationship in the tinker shell我在 laravel 8 中有一个属于关系,当我尝试在 tinker shell 中使用该关系时它返回 null
【发布时间】:2021-11-24 01:15:57
【问题描述】:

我正在使用 Laravel 8,我有以下非常简单的模型和迁移,

作者模型

<?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');
    }
}

个人资料模型

<?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 迁移

<?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 迁移

<?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');
    }
}

尽管有上述模型和迁移,如果我在 tinker shell 中创建一个新的Profile 模型并执行$profile-&gt;author(),它会返回null。我不明白问题出在哪里。

(我尝试将$this-&gt;belongsTo('App\Models\Author'); 更改为$this-&gt;belongsTo(Author::class);$this-&gt;hasOne('App\Models\Profile'); 更改为$this-&gt;hasOne(Profile::class); 并重新启动修补程序。问题仍然存在。我什至尝试将所有无符号整数键更改为无符号大整数,但问题仍然存在.)

【问题讨论】:

    标签: laravel eloquent laravel-8 laravel-relations laravel-models


    【解决方案1】:

    所以我发现了问题所在。解决方法很简单,我忘记在Profile 模型中的author() 方法中添加所有重要的return 语句。

    应该是, return $this-&gt;belongsTo('App\Models\Author'); 而不是 $this-&gt;belongsTo('App\Models\Author');

    【讨论】:

      【解决方案2】:

      试试这个 return $this->belongsTo(Author::class, 'author_id', 'id'); 代替 $this->belongsTo('App\Models\Author');

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-11
        • 2014-10-29
        • 1970-01-01
        • 2014-08-21
        • 1970-01-01
        • 2012-11-26
        相关资源
        最近更新 更多