I'm working on a project using Laravel 10 with mysql and I have these tables:
users=====id | username | email---------------------culinaries==========id | title | cover_photo | status | type | user_id--------------------------------------------------paradises=========id | title | cover_photo | status | type | user_id--------------------------------------------------notifications=============id | type | post_id | post_type------------------------------- * type: enum('user_created_culinary', 'user_created_paradise', 'admin_approved_culinary', 'admin_rejected_culinary', 'admin_approved_paradise', 'admin_rejected_paradise', 'other_notif_types') * post_type : string (only contains either 'culinary' or 'paradise' for now) * post_id : unsignedBigInteger as foreign key to either 'culinaries' or 'paradises' tables based on the 'post_type'.
I want to read the data from notifications
table using this code in my API Controller file:
$culinarySubquery = Culinary::select('id', 'title', 'cover_photo', DB::raw('"culinary" as module_name')) ->where('status', 'published') ->where('type', 'public'); $paradiseSubquery = Paradise::select('id', 'title', 'cover_photo', DB::raw('"paradise" as module_name')) ->where('status', 'published') ->where('type', 'public'); $subquery = $culinarySubquery->unionAll($paradiseSubquery); $notifications = Notification::select('n.id', 'n.user_id', 'users.username', 'n.post_type', 'n.post_id') ->leftJoin('users', 'users.id', '=', 'n.user_id') ->leftJoinSub($subquery, 'tbl', function ($join) { $join->on('tbl.id', '=', 'n.post_id') ->whereColumn('tbl.module_name', '=', 'n.post_type'); }) ->orderBy('n.user_id') ->orderBy('n.id') ->get(); dd($notifications);
but I'm getting the error message below when I try to run it on Postman:
"message": "Method Illuminate\\Http\\JsonResponse::first does not exist.","exception": "BadMethodCallException",
what am I doing wrong? what is the solution? any help is appreciated.