Answer the question
In order to leave comments, you need to log in
How to return the store data and the last 3 posts of the store correctly?
there is a Shop model - id, name, logo
class Shop extends Model
{
use HasFactory;
protected $table = 'shops';
public function latestPost()
{
$this->hasMany(ShopPost::class);
}
}
public function getShopInfo(int $shopId){
$shop = Shop::find($shopId);
if (!$shop) return response()->json(['error' => true, 'message' => 'Shop not found'], 404);
return response()->json([
'error' => false,
'shop' => $shop,
'posts' => $shop->posts()->latest()->take(3)->get()
], 200);
}
Answer the question
In order to leave comments, you need to log in
Before $this->hasMany write return!
You even said it in a mistake!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question