I have a table of results where there is adm_no, subject_id, exam_id and score fields. I inserted the data using the updateOrCreate method in laravel 5.4.
When I fetch the data I still get duplicates. e.g. when a user inserts marks for instance 2 students for a given subject and repeats it for the same students and subject I should get the latest row of all results.
Here is a sample of my query:
public function searchStudent()
{
$exam = Exam::all();
$term = Term::all();
$a = Input::get('adm_no');
$e = Input::get('exam_id');
$t = Input::get('term_id');
$y = Input::get('year');
$results = DB::table('results')
->select(DB::raw('DISTINCT(subject_id)'))
->where('adm_no', 'LIKE', '%' . $a . '%')
->where('exam_id','LIKE', '%' . $e . '%')
->where('term_id', 'LIKE', '%' . $t . '%')
->where('year', 'LIKE', '%' . $y . '%')
->orderBy('created_at', 'desc')
->get();
dd($results);
}
Try the following query:
$results = DB::table('results')
->groupBy('subject_id')
->where('adm_no', 'LIKE', '%' . $a . '%')
->where('exam_id','LIKE', '%' . $e . '%')
->where('term_id', 'LIKE', '%' . $t . '%')
->where('year', 'LIKE', '%' . $y . '%')
->orderBy('created_at', 'desc')
->get();
i did more research on the question and this is what i got working for me.
Question
public function searchStudent(){
$a= Input::get( 'adm_no' );
$e= Input::get( 'exam_id' );
$t= Input::get( 'term_id' );
$y= Input::get( 'year' );
$results=DB::table('results')->distinct('subject_id')
->where( 'adm_no', $a)
->where( 'exam_id', $e)
->where( 'term_id',$t)
->where( 'year',$y)
->groupBy('subject_id')
->latest('subject_id')
->get();
dd($results);
}
Related
I'm trying to make a server-side searching and came up with this eloquent query...
public function search(Request $request) {
$data = User::with('instruments')
->where('user_type', 'teacher')
->orWhere('fname', 'like', '%' . $request->term . '%' )
->orWhere('fname', 'like', $request->term . '%' )
->orWhere('lname', 'like', '% ' . $request->term . '%' )
->orWhere('lname', 'like', $request->term . '%' )
->orWhere('email', 'like', '% ' . $request->term . '%' )
->orWhere('email', 'like', $request->term . '%' )
->orWhere('profession', 'like', '%' . $request->term . '%' )
->orWhere('profession', 'like', $request->term . '%' )
->limit(10)
->get();
return response()->json($data);
}
I almost achieved what I wanted. For example, when I search john it gives me all the users with john in their name. The problem is that, tho the search is correct and knowing I declared where('user_type', 'teacher'), the result set still gives me users with student user_type
The problem is that the OrWhere nullifies the user_type condition, that's why it selects users that are not teachers when one of the OrWhere conditions applies.
By using a second where with a closure, it will always only select users with a user_type teacher:
public function search(Request $request)
{
$data = User::with('instruments')
->where('user_type', 'teacher')
->where(function ($query) use ($request) {
$query->where('fname', 'like', '%' . $request->term . '%')
->orWhere('fname', 'like', $request->term . '%')
->orWhere('lname', 'like', '% ' . $request->term . '%')
->orWhere('lname', 'like', $request->term . '%')
->orWhere('email', 'like', '% ' . $request->term . '%')
->orWhere('email', 'like', $request->term . '%')
->orWhere('profession', 'like', '%' . $request->term . '%')
->orWhere('profession', 'like', $request->term . '%');
})
->limit(10)
->get();
return response()->json($data);
}
On my search method in the Models\Article::class I'm able to get all the results, even in the multiple relations with Categories and Tags, but the issue, that I've been trying to solve, unsuccessfully, is that I just want the results for the published articles – my db column for this is a bool 'active'.
I've tried different approaches by switching the ->where('active', 1) clause around but the results are the same. It brings all the active and non active.
I have in development 8 articles but only 7 with active = 1. When I perform the search, the response sends all the 8, ignoring the where('active', 1) clause. Probably I'm missing or messing something here in the following method:
// Method in the Article Model class
public function getSearchResults($query)
{
return $this->where('active', 1)->where('start_publishing', '<=', Carbon::now())
->where('external_reference', 'LIKE', '%' . $query . '%')
->orWhere('byline', 'LIKE', '%' . $query . '%')
->orWhere('published_by', 'LIKE', '%' . $query . '%')
->orWhere('title', 'LIKE', '%' . $query . '%')
->orWhere('subhead', 'LIKE', '%' . $query . '%')
->orWhere('lead_story', 'LIKE', '%' . $query . '%')
->orWhere('content', 'LIKE', '%' . $query . '%')
->orWhere('meta_keywords', 'LIKE', '%' . $query . '%')
->with('categories')->orWhereHas('categories', function ($q) use ($query) {
$q->where('title', 'LIKE', '%' . $query . '%');
})
->with('documents')->orWhereHas('documents', function ($q) use ($query) {
$q->where('doc_title', 'LIKE', '%' . $query . '%');
//$q->where('doc_description', 'LIKE', '%'.$query.'%');
})
->with('tags')->orWhereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%' . $query . '%');
})
->with('images')
->orderBy('type', 'desc')
->orderBy('updated_at', 'desc')
->paginate(15);
}
Thanks in advance fr any help here on this issue.
Result is because you are use orWhere Criteria. For fix it use this
$this->where('active', 1)
->where('start_publishing', '<=', Carbon::now())
->where(function($q) use ($query) {
$q->where('external_reference', 'LIKE', '%' . $query . '%') // the $q here is required
->orWhere('byline', 'LIKE', '%' . $query . '%')
->orWhere('published_by', 'LIKE', '%' . $query . '%')
->orWhere('title', 'LIKE', '%' . $query . '%')
->orWhere('subhead', 'LIKE', '%' . $query . '%')
->orWhere('lead_story', 'LIKE', '%' . $query . '%')
->orWhere('content', 'LIKE', '%' . $query . '%')
->orWhere('meta_keywords', 'LIKE', '%' . $query . '%');
})
->with('categories')->orWhereHas('categories', function ($q) use ($query) {
$q->where('title', 'LIKE', '%' . $query . '%');
})
->with('documents')->orWhereHas('documents', function ($q) use ($query) {
$q->where('doc_title', 'LIKE', '%' . $query . '%');
//$q->where('doc_description', 'LIKE', '%'.$query.'%');
})
->with('tags')->orWhereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%' . $query . '%');
})
->with('images')
->orderBy('type', 'desc')
->orderBy('updated_at', 'desc')
->paginate(15);
Ok, after Davit's suggestion, which at the beginning seemed like to work, but it was the query I was doing that was "faking" the results, I finally managed to make it work.
Taking the code David suggested, I made a few changes, through the logic of how the query gets constructed and ended up with a final working method that will return only the active Articles matching the "query" criteria from the request. Below the final method code:
// Method in the Article Model class
public function getSearchResults($query)
{
return $this->where('active', 1)
->where('start_publishing', '<=', Carbon::now())
->where(function ($q) use ($query) {
$q->where('external_reference', 'LIKE', '%' . $query . '%')
->orWhere('byline', 'LIKE', '%' . $query . '%')
->orWhere('published_by', 'LIKE', '%' . $query . '%')
->orWhere('title', 'LIKE', '%' . $query . '%')
->orWhere('subhead', 'LIKE', '%' . $query . '%')
->orWhere('lead_story', 'LIKE', '%' . $query . '%')
->orWhere('content', 'LIKE', '%' . $query . '%')
->orWhere('meta_keywords', 'LIKE', '%' . $query . '%');
$q->with('categories')->orWhereHas('categories', function ($q) use ($query) {
$q->where('title', 'LIKE', '%' . $query . '%');
});
$q->with('documents')->orWhereHas('documents', function ($q) use ($query) {
$q->where('doc_title', 'LIKE', '%' . $query . '%');
//$q->where('doc_description', 'LIKE', '%'.$query.'%');
});
$q->with('tags')->orWhereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%' . $query . '%');
});
})
->with('categories')
->with('tags')
->with('images')
->distinct()
->orderBy('type', 'desc')
->orderBy('updated_at', 'desc')
->paginate(15);
}
I have a problem with search form on my website. Basically, when user will type multiple keywords to search form (e.g. business card ) then search result will show all images from keyword 1 (e.g. business) and all images from keyword2 (e.g. card). This is bad, the search form is not relevant at all. Can you please look at the code down bellow and help me fix it so the search form will show only images with both keywords together (keyword1 + AND + keyword2).
public function search($search, $category = null, $timeframe = null)
{
$extends = explode(' ', $search);
if ($category) {
$categoryId = $this->category->whereSlug($category)->first();
}
$images = $this->posts($category, $timeframe)->where('title', 'LIKE', '%' . $search . '%')
->orWhere('tags', 'LIKE', '%' . $search . '%')
->whereNull('deleted_at')->whereNotNull('approved_at')->orderBy('approved_at', 'desc');
foreach ($extends as $extend) {
if (isset($categoryId)) {
$images->whereCategoryId($categoryId)->Where('tags', 'LIKE', '%' . $extend . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
->whereCategoryId($categoryId)->orWhere('title', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
->whereCategoryId($categoryId)->orWhere('image_description', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at');
} else {
$images->orWhere('tags', 'LIKE', '%' . $extend . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
->orWhere('title', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
->orWhere('image_description', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at');
}
}
return $images = $images->with('user', 'comments', 'favorites')->whereNotNull('approved_at')->whereNull('deleted_at')->paginate(perPage());
}
When you use the % sign before and after the search query, you say "give me anything with that items", if you want your system to search and give results exactly as per what the user has entered, you should use only the key phrase. try the code below.
public function search($search, $category = null, $timeframe = null)
{
$extends = explode(' ', $search);
if ($category) {
$categoryId = $this->category->whereSlug($category)->first();
}
$images = $this->posts($category, $timeframe)
->where('title', 'LIKE', "$search")
->orWhere('tags', 'LIKE', "$search")
->whereNull('deleted_at')
->whereNotNull('approved_at')
->orderBy('approved_at', 'desc');
foreach ($extends as $extend) {
if (isset($categoryId)) {
$images->whereCategoryId($categoryId)->Where('tags', 'LIKE', "$search")
->whereNotNull('approved_at')->whereNull('deleted_at')
->whereCategoryId($categoryId)->orWhere('title', 'LIKE', "$search")
->whereNotNull('approved_at')->whereNull('deleted_at')
->whereCategoryId($categoryId)
->orWhere('image_description', 'LIKE', "$search")
->whereNotNull('approved_at')->whereNull('deleted_at');
}
else {
$images->orWhere('tags', 'LIKE', "$extend")
->whereNotNull('approved_at')->whereNull('deleted_at')
->orWhere('title', 'LIKE', "$search")
->whereNotNull('approved_at')->whereNull('deleted_at')
->orWhere('image_description', 'LIKE', "$search")
->whereNotNull('approved_at')->whereNull('deleted_at');
}
}
return $images = $images->with('user', 'comments', 'favorites')
->whereNotNull('approved_at')
->whereNull('deleted_at')
->paginate(perPage());
}
I have two models Location and Danger
Danger has two fields location_id and user_id it simply save users report about locations
btw Danger has a one to many relation with Location
the question is
how can I sort locations with count of it's danger in a search form
here is my form:
$locations = Location::latest();
if ($request->get('q')) {
$q = $request->get('q');
$locations->where('desc', 'like', '%' . $q . '%')
->orWhere('name', 'like', '%' . $q . '%');
}
$locations=$locations->paginate(12);
return view('list')->with(compact('locations'));
If your location has many danger and your relation is named dangers then you can use withCount() for sorting as:
$locations = Location::withCount('dangers');
if ($request->get('q')) {
$q = $request->get('q');
$locations->where('desc', 'like', '%' . $q . '%')
->orWhere('name', 'like', '%' . $q . '%');
}
$locations->orderBy('dangers_count', 'desc')
$locations=$locations->paginate(12);
this might work.
$locations = Location::latest();
if ($request->get('q')) {
$q = $request->get('q');
$locations->where('desc', 'like', '%' . $q . '%')
->orWhere('name', 'like', '%' . $q . '%');
$locations->sortBy(function($item, $key){
return $location->danger()->count();
})
}
$locations=$locations->paginate(12);
return view('list')->with(compact('locations'));
Laravel 4 - advanced Where
I'm trying to retrieve Posts that have a $keyword like a certain Companion and besides that I want to retrieve the Posts the have a linking title or content as the $keyword.
But when I try to use a where or whereIn inside a whereHas the query doesn't take these into account. When the state is 0 (not visible) or not inside the category 1 the Post item should not get selected.
$companion_id = Companion::where('name', 'LIKE', '%' . $keyword . '%' )->lists('id');
The code block below has to do two things:
Search for Post items with a title or content like the $keyword
and search for Post items that have Companions like the $keyword
code:
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', 1)
->whereIn('category_id', array(1));
})
->whereIn('category_id', array(1))
->orwhere('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' )
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
The code above retrieves data succesfully except for the where and whereIn parts inside the whereHas.
Who can help me out?
wrap your orWhere clauses in (..)
you don't need where and whereIn in the whereHas closure, since it queries companions table
you don't need whereIn for category_id, unless you want to pass multiple ids there
.
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id);
})
->whereIn('category_id', array(1)) // why not where(..) ?
->where(function ($q) use ($keyword) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
Thanks to Jarek Tkaczyk.
His answer was almost correct. All I had to do was wrap the where inside a orWhere. Now I get the Posts that has a Companion like the $keyword and I get the Posts that has the $keyword inside the content or title.
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', '1');
})
->orWhere( function($q) use ( $keyword ) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->whereIn('category_id', array(1, 3))
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();