Laravel 一对一关系获取对象结果为 null

发布于 2025-01-10 20:34:32 字数 1280 浏览 0 评论 0原文

移民人员表:

Schema::create('persons', function (Blueprint $table) {
        $table->id();
        $table->string("firstname");
        $table->string("lastname");
        $table->timestamps();
    });

移民护照表:

Schema::create('passports', function (Blueprint $table) {
        $table->id();
        $table->string("identifystring")->unique();
        $table->unsignedBigInteger("person_id");
        $table->timestamps();
        $table->foreign("person_id")->references("id")->on("persons")->onDelete("cascade");
    });

人员模型:

class Person extends Model
{
    use HasFactory;

    protected $table ="persons";

    protected $fillable = [
        "firstname",
        "lastname"
    ];

    public function passport() {
        $this->hasOne(\App\Models\Passport::class, "passports.person_id","persons.id");
    }
}

护照模型:

class Passport extends Model
{

use HasFactory;

    protected $table = "passports";

    protected $fillable = [
        "identifystring",
        "person_id"
    ];
}

执行代码:

$person = \App\Models\Person::findOrFail(2);
dd($person->passport());

结果为:null

Migration persons Table:

Schema::create('persons', function (Blueprint $table) {
        $table->id();
        $table->string("firstname");
        $table->string("lastname");
        $table->timestamps();
    });

Migration passports Table:

Schema::create('passports', function (Blueprint $table) {
        $table->id();
        $table->string("identifystring")->unique();
        $table->unsignedBigInteger("person_id");
        $table->timestamps();
        $table->foreign("person_id")->references("id")->on("persons")->onDelete("cascade");
    });

Person model:

class Person extends Model
{
    use HasFactory;

    protected $table ="persons";

    protected $fillable = [
        "firstname",
        "lastname"
    ];

    public function passport() {
        $this->hasOne(\App\Models\Passport::class, "passports.person_id","persons.id");
    }
}

Passport Model:

class Passport extends Model
{

use HasFactory;

    protected $table = "passports";

    protected $fillable = [
        "identifystring",
        "person_id"
    ];
}

execute Code:

$person = \App\Models\Person::findOrFail(2);
dd($person->passport());

Result is: null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

芸娘子的小脾气 2025-01-17 20:34:32
class Person extends Model
{
    use HasFactory;

    protected $table ="persons";

    protected $fillable = [
        "firstname",
        "lastname"
    ];

    public function passport() {
        $this->hasOne(\App\Models\Passport::class, "person_id", "id");
    }
}
class Person extends Model
{
    use HasFactory;

    protected $table ="persons";

    protected $fillable = [
        "firstname",
        "lastname"
    ];

    public function passport() {
        $this->hasOne(\App\Models\Passport::class, "person_id", "id");
    }
}
嗼ふ静 2025-01-17 20:34:32

您没有返回该关系。只需返回它:

public function passport() {
    return $this->hasOne(\App\Models\Passport::class, "passports.person_id","persons.id");
}

You did not return the relation. just return it:

public function passport() {
    return $this->hasOne(\App\Models\Passport::class, "passports.person_id","persons.id");
}
我乃一代侩神 2025-01-17 20:34:32

根据所写的迁移。在 Person 模型中编写

  public function passport() {
        return $this->hasOne(\App\Models\Passport::class, "person_id","id");
    }

并在 Passport 模型中编写

public function person() {
    return $this->belongsTo(\App\Models\Person::class, 'id', 'person_id');
}

执行代码

$person = \App\Models\Person::findOrFail(2)->passport;
dd($person);

As per the migrations written. In the Person model write

  public function passport() {
        return $this->hasOne(\App\Models\Passport::class, "person_id","id");
    }

and in the Passport model write

public function person() {
    return $this->belongsTo(\App\Models\Person::class, 'id', 'person_id');
}

Execute the code

$person = \App\Models\Person::findOrFail(2)->passport;
dd($person);
野味少女 2025-01-17 20:34:32

使用此代码。

在你的个人模型中

public function passport() {
        return $this->hasOne(\App\Models\Passport::class);
    }

在你的护照模型中

public function person() {
    return $this->belongsTo(\App\Models\Person::class, 'person_id');
}

然后尝试通过做同样的事情再次传递关系

$person = \App\Models\Person::findOrFail(2);
dd($person->passport());

Use this code.

In your Person Model

public function passport() {
        return $this->hasOne(\App\Models\Passport::class);
    }

In your passport model

public function person() {
    return $this->belongsTo(\App\Models\Person::class, 'person_id');
}

Then try to pass the relation again by doing the same thing

$person = \App\Models\Person::findOrFail(2);
dd($person->passport());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文