Laravel Eloquent关联函数名称

huangapple go评论60阅读模式
英文:

Laravel eloquent relationship function name

问题

I have 2 tables, which are posts and categories. The relationship between posts and categories is many-to-one. So, the code in the post class model is like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

With that code, the application runs successfully.

The problem is, when I tried to change the category function name to "categories" and access the name of the category of the post like this:
Post::first()->categories->name, Laravel gives an error like this: Attempt to read property "name" on null in D:\BelajarProgram\CODINGAN\Laravel\application\coba-laravel (edited)

The "Post::first()->category" returns the category of the post when the eloquent function name is "category." But when I tried to change the name of the function to "categories" and access it with "Post::first()->categories," it returns null.

I have tried to change the name of the function of the eloquent relationship in the category class model with a random name, but I can still access it, and Laravel doesn't give an error. The code of the category class model is like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;
    protected $guarded = ['id'];

    public function posts(){
        return $this->hasMany(Post::class);
    }
}

Can someone help me understand why I can change the name of the eloquent function in the category class model but can't in the post class model?

英文:

I have 2 table, which is posts and categories. The relationship between posts and categories is many to one. So the code in post class model is like this :

&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    protected $guarded = [&#39;id&#39;];

    public function category()
    {
        return $this-&gt;belongsTo(Category::class);
    }
}

With that code the application running successfully.

The problem is, when i tried to change the category function name to categories, and i tried to access the name of category of the post like this :
Post::first()-&gt;categories-&gt;name, laravel give an error like this Attempt to read property &quot;name&quot; on null in D:\BelajarProgram\CODINGAN\Laravel\application\coba-laraveleval()&#39;d code.

(edited

the "Post::first()->category" return the category of the post when the eloquent function name is category.
But when i tried to change the name of function like categories and accessing it with "Post::first()->categories", it return a null

)

I have tried to change the name of the function of eloquent relationship in category class model with random name, but i still can accessing it and laravel doesn't give an error. The code of category class model is like this

&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;
    protected $guarded = [&#39;id&#39;];

    public function posts(){
        return $this-&gt;hasMany(Post::class);
    }
}

Can someone help me, why i can change the name of eloquent function in category class model, but can't in post class model ?

答案1

得分: 0

以下是已翻译的内容:

public function categories()
{
    return $this->belongsTo(Category::class, 'category_id', 'id');
}

Let me know if it works or not.
英文:

Doing the following should work:

public function categories()
{
    return $this-&gt;belongsTo(Category::class, &#39;category_id&#39;, &#39;id&#39;);
}

Let me know if it works or not.

huangapple
  • 本文由 发表于 2023年7月18日 13:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定