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

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)

Related

Memory usage not change after using redis cache - 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?

Laravel 5.2 - Returning two controllers to the same view

I have a page controller which simply returns a view with all of my users e.g. -
public function index()
{
$users = DB::table('users')->select('id', 'account_id', 'email', 'name')->paginate(10);
return view('admin/home')->with('users', $users);
}
On that page, I have a search input which I want to return to the same view with the same variable data -
public function searchUser(Request $request)
{
$searchTerm = $request->input('search');
$search = '%'.$searchTerm.'%';
$users = User::where('name', 'LIKE', $search)
->orwhere('email', 'LIKE', $search)
->orwhere('account_id', 'LIKE', $search)
->get();
return redirect()->route('admin.home')->with('users', $users);
}
The problem is when I search and use the SearchUser function it also runs the index() function which returns all the users. I'm assuming I need to append something to the url so it would have something like
/site?searchterm
Although I'm not sure how that would work.
I understand with a get request you can pass in an optional parameter but my search form is a post type.
In searchUser() return the same view:
return view('admin.home')->with('users', $users);
Instead of redirecting:
return redirect()->route('admin.home')->with('users', $users);

Eloquent - modify standard with() response for a particular model

I have a bunch of stuff I do in a closure whenever a relationship is brought in using the with() Eloquent method, and I'm having to repeat this code an awful lot:
'earmarks' => function($q) {
$q
->join('locations', 'locations.id', '=', 'earmarks.location')
->select('earmarks.*', 'locations.location AS em_location')
->orderBy('date', 'asc');
}
Since I always want to do the above whenever with() is called on that relationship, how can I add it to my model so it gets added automatically?
You could extract the callback function to a Query Scope or make a totally new relationship for earmarks with that query.
public function earmarksWithLocations()
{
return $this->earmarks()
->join('locations', 'locations.id', '=', 'earmarks.location')
->select('earmarks.*', 'locations.location AS em_location')
->orderBy('date', 'asc');
}
->with('earmarksWithLocations')
or
public function scopeWithLocations($q)
{
return $q
->join('locations', 'locations.id', '=', 'earmarks.location')
->select('earmarks.*', 'locations.location AS em_location')
->orderBy('date', 'asc');
}
->with(['earmarks' => function ($q) {
$q->withLocations();
}])

Laravel search mutiple fields

i currently have a search function on my website, i need it to search 3 fields - appid, mobilenumber, email .
Right now once the user enters the data in the search box it searches all 3 but its not working.
Data is collected using GET.
Here is my query
http://www.example.com?id=1&searchall=07853637362
$mobile = INPUT::get('searchall');
$email = INPUT::get('searchall');
$appid = INPUT::get('searchall');
$data = DB::table('leads')
->when($mobile, function($query) use ($mobile){
return $query->where('MobilePhone', $mobile);
})
->when($email, function($query) use ($email){
return $query->where('Email', $email);
})
->when($appid, function($query) use ($appid){
return $query->where('AppID', $appid);
})
->get();
So I need it to search each field until it finds the correct field value.
$mobile = INPUT::get('searchall');
$email = INPUT::get('searchall');
$appid = INPUT::get('searchall');
$data = DB::table('leads')
->when($mobile, function($query) use ($mobile){
return $query->orWhere('MobilePhone', $mobile);
})
->when($email, function($query) use ($email){
return $query->orWhere('Email', $email);
})
->when($appid, function($query) use ($appid){
return $query->orWhere('AppID', $appid);
})->get();
To search data try with like
->when($mobile, function($query) use ($mobile){
return $query->where('MobilePhone', 'like', '%'. $mobile . '%');
})
to actually search each field, use orWhere
$keywords = Input::get('searchall');
$data = DB::table('leads')
->where('mobilephone', '=', $keywords)
->orWhere('email', '=', $keywords)
->orWhere('AppID', '=', $keywords)
->get();
I don't understand why you need the same value from the user using 3 variables, so the following is the simplified version:
$searched = Input::get('searchall');
$matched = DB::table('leads')
->where('MobilePhone', $searched)
->orWhere('Email', $searched)
->orWhere('AppID', $searched)
->get();

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