Memory usage not change after using redis cache - laravel - laravel

Iam using laravel debugger for checking cache views and query and its work no template were rendered and 0 statement were executed but take a look
before
after
Controller
public function search(Request $request)
{
if($request->has('page')) {
$page = $request->q.'&page='.$request->query('page');
} else {
$page = $request->q;
}
$q = Cache::remember($page, 30, function() use($request){
$user = User::when($request->q, function($query) use ($request) {
$query->select(
'users.id', 'users.first_name', 'users.last_name',
'profiles.location', 'profiles.occupation', 'profiles.url_photo_profile')
->leftJoin('profiles', 'users.id', '=', 'profiles.member_id')
->where('first_name', 'LIKE', '%'.$request->q.'%')
->orWhere('last_name', 'LIKE', '%'.$request->q.'%');
})->paginate(1);
$user->appends($request->only('q'));
if ($request->ajax()) {
return Response::json(View::make('search.result', compact('user'))->render());
}
return view('search.result', compact('user'))->render();
});
return $q;}
no different between before and after why this happening ? any suggestion?

Related

Cache remember specific parameter in get request end up the same result - Laravel 5

trying too search any matching name get request with different parameter for example: search?q=anhar showing the result with the name anhar then when I change anhar with aslam its not querying again and endup with the same result
here's my controller
public function search(Request $request)
{
$q = $request->get('q');
$q = Cache::remember('q', 30*60, function() use ($q) {
$user = User::select('users.id', 'users.first_name', 'users.last_name', 'profiles.location', 'profiles.occupation')
->leftJoin('profiles', 'users.id', '=', 'profiles.member_id')
->where('first_name', 'LIKE', '%'.$q.'%')
->orWhere('last_name', 'LIKE', '%'.$q.'%')
->paginate(10);
return $user;
});
return view('search.result', compact('q'));
}
and this route
Route::get('/search', 'PublicController#search')->name('search.result');
I have found the answer with replacing Cache::remember('q') with Cache::remeber($q)

How can i check request comes or not in laravel?

I want to get $data['student'] if request get (like: student-guardian?reg_no=0001 in URL).
if no any parameter on url $data['student'] don't needed.
My StudentGuardian function Like:
public function studentGuardian(Request $request)
{
$data = [];
if(!empty($request)){
$data['student'] = Student::select('id','reg_no','reg_date', 'first_name', 'middle_name', 'last_name','faculty', 'semester','status')
->where(function ($query) use ($request) {
if ($request->has('reg_no')) {
$query->where('students.reg_no', 'like', '%'.$request->reg_no.'%');
$this->filter_query['students.reg_no'] = $request->reg_no;
}
})
->get();
}
return view(('studentguardian.index'), compact('data'));
}
This is what you need:
public function studentGuardian(Request $request)
{
$data = [];
// check request has any parameter
if($request->all()){
// query and get list student
$data['student'] = Student::select('id','reg_no','reg_date', 'first_name', 'middle_name', 'last_name','faculty', 'semester','status')
->where(function ($query) use ($request) {
if ($request->has('reg_no')) {
$query->where('students.reg_no', 'like', '%'.$request->reg_no.'%');
$this->filter_query['students.reg_no'] = $request->reg_no;
}
})
->get();
} else {
//do something else
}
// return data to view
return view('studentguardian.index', compact('data'));
}
First, your if(!empty($request)) is useless, $request is always a Request object, not empty.
Second, you can use just if($request->all()) { ... } to check if request array is empty.

Laravel - passing array of columns in 'where' clause

I have a search query like this:
$data = User::where('first_name', 'like', '%'.$query.'%')
->orWhere('last_name', 'like', '%'.$query.'%')
->get();
Now, I have many models, each with different column names. Instead of defining a search() function into every controller, I want to do this:
// User
public static function searchableFields()
{
return ['first_name', 'last_name'];
}
// Some other model
public static function searchableFields()
{
return ['name', 'description'];
}
And put the search logic in a shared controller, something like this:
$data = $classname::
where($classname::searchableFields(), 'like', '%'.$query.'%')
->get();
How can I achieve this?
Thanks a lot.
You can loop over the fields and add them to your Eloquent query one by one.
$data = $classname::where(function ($query) use ($classname) {
foreach ($classname::searchableFields() as $field)
$query->orWhere($field, 'like', '%' . $query . '%');
})->get();
I would use scope for that.
You can create base model that all the models should extend (and this model should extend Eloquent model) and in this model you should add such method:
public function scopeMatchingSearch($query, $string)
{
$query->where(function($q) use ($string) {
foreach (static::searchableFields() as $field) {
$q->orWhere($field, 'LIKE', '%'.$string.'%');
}
});
}
Now you can make a search like this:
$data = User::matchingSearch($query)->get();
Just to avoid confusion - $query parameter passed to matchingSearch becomes $string parameter in this method.
You can try something like this.
// Controller
function getIndex(Request $request)
{
$this->data['users'] = User::orderBy('first_name','asc')->get();
if ($request->has('keyword')) {
$results = User::search($request->keyword);
$this->data['users'] = collect([$results])->collapse()->sortBy('first_name');
}
}
// Model
function search($keyword)
{
$results = [];
$search_list = ['first_name','last_name'];
foreach ($search_list as $value)
{
$search_data = User::where($value,'LIKE','%'.$keyword.'%')->get();
foreach ($search_data as $search_value) {
$exist = 0;
if (count($results)) {
foreach ($results as $v) {
if ($search_value->id == $v->id) {
$exist++;
}
}
if ($exist == 0) {
$results[] = $search_value;
}
} else{
$results[] = $search_value;
}
}
}
return $results;
}

How to filter results in Laravel using Eloquent Models

I am attempting to use the following
$orders = \App\Order::with([
'Customer' => function ($query) use ($filterFirst, $filterLast, $filterstate)
{
if($filterFirst)
{
$query->where('customers.first', 'LIKE', "{$filterFirst}%");
}
if($filterLast)
{
$query->where('customers.last', 'LIKE', "{$filterLast}%");
}
if ($filterstate)
{
$query->where('customers.state', '=', $filterstate);
}
},
'Group' => function ($query) use ($filtercategory)
{
if($filtercategory)
{
$query->where('order_groups.groupid', '=', $filtercategory);
}
},
'Group.ProductGroups' => function ($query) use ($filteractualcat)
{
if($filteractualcat)
{
$query->where('productgroups.cat', '=', $filteractualcat);
}
},
'Group.ProductGroups.Category',
'Doctor',
'Status' => function ($query) use ($filterstatus)
{
if ($filterstatus)
{
$query->whereIn('statuses.id', $filterstatus);
}
},
'Signed'
])->where(function ($query) use ($id, $filterlab, $filterStart, $filterEnd, $filterRep) {
if ($id)
{
$query->where('orders.id', $id);
}
if ($filterlab)
{
$query->where('orders.labid', $filterlab);
}
if ($filterStart || $filterEnd)
{
if ($filterStart && $filterEnd)
{
$query->whereBetween('orders.created_at', [$filterStart, $filterEnd]);
}
else
{
if ($filterStart && !$filterEnd)
{
$query->where('orders.created_at', '>=', $filterStart);
}
else
{
$query->where('orders.created_at', '<=', $filterEnd);
}
}
}
if ($filterRep)
{
$query->where('salesrep', $filterRep);
}
})->orderBy('orders.created_at', 'ASC');
However, when I run ->get on $orders I get the entire table of orders, and I just want to get the orders that match the Filters...
Any ideas on how I can accomplish this?
Use whereHas() to filter orders by customer filters:
$orders = \App\Order::whereHas('Customer', function ($query) use ($filterFirst, $filterLast, $filterstate) {
if ($filterFirst) {
$query->where('first', 'like', $filterFirst . '%');
}
if ($filterLast) {
$query->where('last', 'like', $filterLast . '%');
}
if ($filterstate) {
$query->where('state', $filterstate);
}
})
->get();
If you also want to load customers for orders, add with to the query.

Laravel 4 Eager Loading constraints

I want to get all Items (topics) WITH their comments, if comments user_id = $id. I try something like this, but it isn't working. So if Item hasn't got any comment with user_id = $id, then I don't need this Item.
In DiscussionsItem model I have methode:
public function discussionsComments() {
return $this->hasMany('DiscussionsComment', 'discussionsitem_id');
}
My query in controller is like this:
$items = DiscussionsItem::whereBrandId($brand_id)
->whereBrandCountryId($country_id)
->with(['discussionsComments' => function($query) use ($id) {
$query->where('user_id', '=', $id);
}])
->whereHas('discussionsComments', function($query) use ($id) {
$query->where('user_id', '=', $id);
})
->with(['views' => function($query) use ($id) {
$query->where('user_id', $id)->count();
}])
->orderBy('created_at', 'DESC')->get();
My problem is that I get items with comments, where comments user_id != $id.
P.S. I need to take 5 comments, but I cant imagine how to do that, because ->take(5) in my eager load is not working.
I figure it out (working code):
$items = DiscussionsItem::whereBrandId($brand_id)
->whereBrandCountryId($country_id)
->whereHas('discussionsComments', function($query) use ($id) {
$query->where('user_id', '=', $id);
})
->with(['views' => function($query) use ($id) {
$query->where('user_id', $id)->count();
}])
->orderBy('created_at', 'DESC')->get();
$items->each(function($topic) use ($id, 5){
$topic->comments = $topic->discussionsComments()->where('user_id', '=', $id)->get()->take(5);
});
You can do a custom scope, or just limit the amount returned by your relation:
public function discussionsComments() {
return $this->hasMany('DiscussionsComment', 'discussionsitem_id')
->latest()
->take(5);
}

Resources