I am developing an application using Angular-7 as frontend and Laravel-5.8. I am using Laravel Spatie for User Management. I have these three tables:
Table
CREATE TABLE `client` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`client_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`client_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `trips` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`dispatch_datetime` datetime DEFAULT NULL,
`loading_date` date DEFAULT NULL,
`loaded_from` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`destination` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`client_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`client_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
From the tables above, I have three classes: User, Client and Trip. I have used Laravel Spatie to enable users to only have access to data they are given permission. This is working perfectly.
Now, I want to achieve the following from the ApiController:
The Logged-in user should be able to view only the trips that his client embark on.
If the Logged-In user belongs to the Client that has client_id as MBB, he should be able to view all the trips for all the clients in the database, except he doesn't have permission to view trips.
public function index(Request $request){
$user = Auth::user();
$userClientId = Auth::user()->client_id;
if(!Auth::user()->hasPermissionTo('View Trip')){
return response()->json([ "message" => 'User do not have permission'], 401);
}
try{
if(Auth::user()->client_id == 'MBB')
{
if(($request->get('sort')!='null' && $request->get('sort')!='') && $request->get('search')){
$trip = Trip::where("trip_number", "LIKE", "%{$request->get('search')}%")->orderby($request->get('sort'), $request->get('order'))->paginate(10);
} else if(($request->get('sort')!='null' && $request->get('sort')!='')){
$trip = Trip::orderby($request->get('sort'), $request->get('order'))->paginate(10);
}
else if($request->get('search'))
$trip = Trip::where("trip_number", "LIKE", "%{$request->get('search')}%")->paginate(10);
else
$trip = Trip::paginate(10);
}
else
{
$trip = Trip::where('client_id', $userClientId)
if(($request->get('sort')!='null' && $request->get('sort')!='') && $request->get('search')){
$trip = $trip->where("trip_number", "LIKE", "%{$request->get('search')}%")->orderby($request->get('sort'), $request->get('order'))->paginate(10);
} else if(($request->get('sort')!='null' && $request->get('sort')!='')){
$trip = $trip->orderby($request->get('sort'), $request->get('order'))->paginate(10);
} else if($request->get('search')) {
$trip = $trip->where("trip_number", "LIKE", "%{$request->get('search')}%")->paginate(10);
} else{
$trip = $trip->paginate(10);
}
}
return response()->json($trip, 200);
}
catch(QueryException $e)
{
$errorCode = $e->errorInfo[1];
return response()->json($errorCode);
}
}
When I loaded the page, I got this error:
syntax error, unexpected 'if' (T_IF)
Then when I clicked on the error in the console, it took me to this page on Laravel:
So, I observe that the error is from the else statement (Line 56 from the diagram, where I highlighted yellow) where the client_id is not MBB. I looked at the statement and couldn't see anything wrong. How do I resolve this error?
