That isn't a relationship? It's just a column
Create an actual table for features instead. What is the features model pointing at?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How do we load many to many relationships on API resources? I have two plot and feature models that are in a many to many relationship.
I am getting an array of items like this only instead of full objects.
"features": [ 29, 28, 23, 22, ]
My PlotsController:
$plots = Plot::latest()->get();
return PlotResource::collection($plots);
My Plot Model:
`protected $casts = [ 'images' => 'array', 'features' => 'array'
];`
public function features(){
return $this->belongsToMany(Feature::class);
}`
My Feature model:
` public function plots(){
return $this->belongsToMany(Plot::class);
}`
in my plot resource, I am calling the relations like this:
'features' => $this->features,
In my plots table, I am saving a features array in this field:
$table->text('features')->nullable(); (in other tables I am also using json as column type)
I want every feature in the relation to show like this:
{ "id": 31, "feature": "Lake Facing", "created_at": "2021-11-14T07:00:31.000000Z", "updated_at": "2021-11-14T07:00:31.000000Z" },
That isn't a relationship? It's just a column
Create an actual table for features instead. What is the features model pointing at?
Please or to participate in this conversation.