I'm new on Resfully services and Lumen (Laravel micro-framework).
I want to pass the /books?limit=10&offset=5 parameters to the controller and set it on the json response and I can't figure out how.
web.php
$router->get('/books/', ['uses' => 'BooksController#index']);
BooksController.php
public function index()
{
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->offset(5) // This should have an default value until the user pass a value through url
->limit(30) // This should have an default value until the user pass a value through url
->orderBy('id', 'asc')
->get(['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
Could you please help me?
Thanks,
You could to use the Request object to do that:
use Illuminate\Http\Request;
public function index(Request $request)
{
$limit = $request->input('limit');
$offset = $request->input('offset');
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->offset($offset) // This should have an default value until the user pass a value through url
->limit($limit) // This should have an default value until the user pass a value through url
->orderBy('id', 'asc')
->get(['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
Related
Anyone can help me? How to add where condition from the with function model relation.
I tried ->where('driver.group_id',$group_id) but it does not work. please look my code below.
public function getDriversDailyEarnings()
{
$group_id = Auth::user()->group->id;
$today = Carbon::today();
$data = DailyEarnings::with(['payout_cost',
'status:id,name',
'driver' => function ($query) {
$query->with('user');
}])
->where('driver.group_id',$group_id)
->whereDate('created_at', $today)
->get();
return response()->json($data, 200);
}
You can try this. Haven't tested it yet.
DailyEarnings::with([
'payout_cost',
'status:id,name'
'driver' => function($query) {
$query->where('group_id', auth()->user()->group->id)->with('user');
}
])
->whereHas('driver', function($query) {
$query->where('group_id', auth()->user()->group->id);
})
->whereDate('created_at', now()->format('Y-m-d'))
->get();
To pass other clauses to a relation, just use it inside an array, where the key and the name of the relation and the value is a function that receives an instance of the query, something like this:
public function getDriversDailyEarnings()
{
$data = DailyEarnings::with([
'payout_cost' => function ($myWithQuery) {
$myWithQuery->where('column-of-relation-here', 'value-here')
->orWhere('name', 'LIKE', '%Steve%');;
}
])->get();
return response()->json($data, 200);
}
Perhaps something like this:
// ->where('driver.group_id',$group_id)
->whereHas('driver', fn($qry) => $qry->where('group_id', $group_id)) // if group_id a field on driver
When I am using a transform method after calling paginate(20) it will return regular results, without paginating that is not supposed to be.
and when I remove transform() method paginate work as it supposes to be.
$users =
User::whereUser_role(1)
->join('user_types', 'users.user_type', '=', 'user_types.user_type_id')
->when(request('user-type'), function ($query) {
$query->where('user_types.name', '=', request('user-type'));
})
->addSelect(
'users.*',
'user_types.name as user_type',
)
->orderBy('users.created_at', 'desc')
->paginate(20)
->appends(request()->all())
->getCollection()
->transform(function ($user) {
$user->user_status = $user->status ? "Active" : "InActive";
$user->email_verified = $user->email_verified ? "Yes" : "No";
return $user;
});
$users->makeHidden(['user_role']);
return response()->json([
'message' => 'Success',
'status' => 200,
'requestLocation' => request()->path(),
'success' => true,
'data' => $users
], 200);
You can use getCollection method for the same. As per laravel API documentaion:
getCollection() : Get the paginator's underlying collection.
For e.g.
$users = User::whereStatus(1)
->paginate(20);
// iterates paginated items and applies a transformation
$users->getCollection()->transform(function ($user) {
$user->email_verified = $user->email_verified ? "Yes" : "No";
return $user;
})
Reference:Laravel API -> AbstractPaginator -> getCollection()
I'm trying to filter products based on query string. My goal is to get products from a collection if it's given, otherwise get every product. Could someone help me what's wrong with the following code?
$products = \App\Product::where([
'collection' => (request()->has('collection')) ? request('collection') : '[a-z]+',
'type' => (request()->has('type')) ? request('type') : '[a-z]+'
])->get();
PS.: I've also tried with 'regex:/[a-z]+', it's not working...
$products = \App\Product::where(['collection' => (request()->has('collection')) ? request('collection') : 'regex:/[a-z]+'])->get();
What you can do is use when eloquent clause, so your where clause for collections will be triggered only when the request('collection') exists, same logis applie to type as well.
$products = \App\Product::
when(request()->has('collection'), function ($q) {
return $q->where('collection', request('collection'));
});
->when(request()->has('type'), function ($q) {
return $q->where('type', request('type'));
})
->get();
Or another way if you have your request values assigned to a variable something like:
$collection = request('collection');
$type= request('type');
$products = \App\Product::
when(!empty($collection), function ($q) use ($collection) {
return $q->where('collection', $collection);
});
->when(!empty($type), function ($q) use ($type) {
return $q->where('type', $type);
})
->get();
I have this code in Lumen 5.6 (Laravel microframework) and I want to have an orderBy method for several columns, for example, http://apisurl/books?orderBy=devices,name,restrictions,category also send asc or desc order.
Lumen's documentation says that we can use the orderBy like this
$books = PartnersBooks::all()->orderBy('device', 'asc')->orderBy('restrictions', 'asc')->get();
So, I made a function with a foreach to fill an array with different orderBy requests values and tried to put on eloquent queries without succeeding.
Can anybody help me?
use Illuminate\Http\Request;
public function index(Request $request)
{
$limit = $request->input('limit');
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->orderBy('id', 'asc')
->paginate($limit, ['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
You can do this:
// Get order by input
$orderByInput = $request->input('orderBy');
// If it's not empty explode by ',' to get them in an array,
// otherwise make an empty array
$orderByParams = !empty($orderByInput)
? explode(',', $orderByInput)
: [];
$query = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
});
// Foreach over the parameters and dynamically add an orderBy
// to the query for each parameter
foreach ($orderByParams as $param) {
$query = $query->orderBy($param);
}
// End the query and get the results
$result = $query->paginate($limit);
I want to make a filter with different parameters. One of parameter is $article_title. But it can be empty . My problem is even if its not empty i get null in return $comments. That is because this part of code:
$q->where('language_id', $default_language_id)->where('title','like',$article_title);
This is my function
public function getResultCommentsWithArticle($comment,$user_firstname,$article_title,$orderBy){
$default_language = Languages::where('default',1)->first();
$default_language_id = $default_language->id;
$comments = ArticleComments::orderBy($orderBy,'desc')
->with(['user', 'article', 'article.translations' => function($q) use($default_language_id,$article_title) {
$q->where('language_id', $default_language_id)->where('title','like',$article_title);
}])->paginate(10);
return $comments;
}
EDIT:
I also tried like this:
$comments = ArticleComments::orderBy($orderBy,'desc')
->with(['user', 'article', 'article.translations' => function($q) use($default_language_id,$article_title) {
$q->where([
['language_id', '=', $default_language_id],
['title', 'like', '%'. $article_title .'%'],
]);
}])->paginate(10);
But i get all comments and not comments with title="something"
$comments = ArticleComments::orderBy($orderBy,'desc')
->with(['user', 'article'])
->whereHas('article.translations', function($q)
use($default_language_id,$article_title) {
$q->where('language_id', $default_language_id)
->where('title','like',$article_title);
})->paginate(10);
Try this:
$comments = ArticleComments::orderBy($orderBy, 'desc')
->with(['user', 'article' => function ($q) use($default_language_id, $article_title) {
$q->with('translations')
->whereHas('translations', function ($q) use($default_language_id, $article_title) {
$q->where('language_id', $default_language_id)->where('title','like',$article_title);
});
}])->paginate();
This will select all comments and it will attach to it articles that match the translation.title and default language id.
In case you want to filter the comments instead of articles you would do this:
$comments = ArticleComments::orderBy($orderBy, 'desc')
->with(['user', 'article.translation'])
->whereHas('article', function ($q) use($default_language_id, $article_title) {
$q->whereHas('translations', function ($q) use($default_language_id, $article_title) {
$q->where('language_id', $default_language_id)->where('title','like',$article_title);
});
})->paginate();
return $comments;