英文:
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 :
<?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 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()->categories->name
, laravel give an error like this Attempt to read property "name" on null in D:\BelajarProgram\CODINGAN\Laravel\application\coba-laraveleval()'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
<?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, 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->belongsTo(Category::class, 'category_id', 'id');
}
Let me know if it works or not.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论