The issue seems to be with the user_id field in the NewsList type schema. It is defined as an AppUsers type, but the subquery in the NewsList model is returning a BelongsTo relationship. To fix this, you can update the user_id field in the NewsList type schema to be a User type (assuming you have a User type defined in your schema) and update the subquery in the NewsList model to return a belongsTo relationship to the AppUsers model.
Here's an updated version of the code:
Type Schema:
type NewsList {
id: ID!
is_active: Boolean!
title: String!
body: String!
user: User!
created_at: DateTime!
updated_at: DateTime
}
extend type Query {
newsByPage: [NewsList!]! @paginate(defaultCount: 10) @orderBy(column: "created_at", direction: DESC)
}
NewsList Model:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class NewsList extends Model
{
protected $fillable = [
'id',
'is_active',
'title',
'body',
'user_id'
];
public function user(): BelongsTo
{
return $this->belongsTo(AppUsers::class);
}
}
AppUsers Model:
use Illuminate\Database\Eloquent\Model;
class AppUsers extends Model
{
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = [
'id',
'is_active',
'firstname',
'lastname',
'gender',
'city',
'country',
'phone',
'email',
'leadership',
'user_group',
'user_zone',
'level'
];
}
Note that I removed the id() method from the AppUsers model as it doesn't seem to be necessary.
With these changes, the user field in the NewsList type schema should now return the expected user details.