I have 2 table on my laravel app, and they are related.
Table 1:
class User extends Model {
protected $fillable = ['name', 'email', 'password'];
public function post() {
return $this->hasMany(Post::class);
}
}
Table 2:
class Post extends Model {
protected $fillable = ['title', 'info', 'image'];
pretected $appends = ['post_by'];
public function user() {
return $this->belongsTo(User::class);
}
public function getPostByAttribute() {
return $this->user->name;
}
}
Now, when i fetch the post table it return with user object which is the related user to the post.
This is the result look like:
data: {
title: "Test",
info: "Test Info",
image: null,
post_by: "Test User", // this is only i want
user: {name: "Test User", email: "user@test.com"} // this is i want to exclude
}
If the question already exist, please give me reference to it.