Guide: Laravel Developer
- Add phpdoc on controller functions
/**
* <brief_summary>
*
* @type <request_type> <route_name>
* @param <data_type> <data_name>
* @return <data_type>
*/
Example:
/**
* Get Subcategory Slug with Product Title
*
* @type GET admin.get.subcategory.slug
* @param Request $request
* @return Response
*/
- Change the JSON response code to following format
return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]);
- Make all JSON response structure in following:
{
"success": "true", // if the check was success
"message": "Added successfully", // Any error or info message to be shown on frontend
"data": [
"var1": "value1"
] // For any series of data to be consumed in frontend
}
- Avoid
echoin controller function rather return Response type. For debugging use logs orvar_dump($var). - Always aim for 100% test coverage.
- Always use a try - catch statement around Database Transactions and log error
Log::error($e);. - Change all request from fetch or jquery request to axios.
- Use snake case (param_name) for variables and lowerPascalCase for function.
- Avoid usage of
withSumandwhereHasand use ratherleftJoinandjoin.
Sample Usage whereHas:
$combinations = $combinations->whereHas(
'product',
function ($q) use ($id) {
$q
->where('subcategory_id', $id)
->where('status', '=', 'active');
}
);
to
$combinations = $combinations->join('products', 'product_combinations.product_id', '=', 'products.id')
->where('products.subcategory_id', $id)
->where('products.status', '=', 'active')
->whereNull('products.deleted_at');
Sample Usage withSum:
$combinations = ProductCombination::withSum(
[
'order_products' => function (Builder $q) {
$q->where('payment', '!=', 'NOT_COMPLETE');
}
],
'quantity'
);
to
$combinations = ProductCombination::select('product_combinations.*', DB::raw('SUM(order_products.quantity) as order_products_sum_quantity'))
->leftJoin('order_products', function ($join) {
$join->on('product_combinations.id', '=', 'order_products.combination_id')
->where('order_products.payment', '!=', 'NOT_COMPLETE')
->whereNull('order_products.deleted_at');
})
->groupBy('product_combinations.id');