Laravel Sort Results by Query and URL - laravel

I want sort my Search Results with an URL. I cant use Controller in my way because i use Route for /search.
My Search Route:
Route::get ( '/', function () {
$mydb= Product::paginate(200);
return view ( 'search' )->withProduct($mydb);
} );
Route::any ( '/search', function () {
$q = Input::get ( 'q' );
$sort = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orderBy('created_at','desc')->paginate(20);
if($q != ""){
$products = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->paginate (200)->setPath ( '' );
$pagination = $products->appends ( array (
'q' => Input::get ( 'q' )
) );
if (count ( $products ) > 0)
return view ( 'search' )->withDetails ( $products )->withQuery ( $q );
}
Now i want create an Button where i can Sort the results from query search.
Button Example:
Sort by Price
I want add like this to my Route:
$sort = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orderBy('created_at','desc')->paginate(20);
This dont work..
And then add the {{ $sort }}behind my Button URL like:
Sort by Price
Any Idea how i can do it correct? Sorry i am an Beginner with Laravel and i know its not correct.
Thanks!

There appears to be a lot of errors in your code.
But this line
Sort by Price
could be
Sort by Price
Then in your closure
$q = Input::get ( 'q' );
$orderby = Input::has('orderby') ? Input::get('orderby') : 'created_at';
$sort = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orderBy($orderby,'desc')->paginate(20);

Related

Laravel Relationship Search

I'm building a project with Laravel 8. I need to handle request parameter which contains page, limit, searchColumn, searchText, orderColumn and orderDirection. Sometimes I need to search in relations. I've built this system below but it doesn't work on relationships.
$query = (new Log())->newQuery();
$query->with('customer', 'domain', 'type');
if ($request->searchColumn != "" && $request->searchColumn != NULL) {
$query->where($request->searchColumn, 'LIKE', '%' . $request->searchText . '%');
}
if ($request->orderColumn != "" && $request->orderColumn != NULL) {
$query->orderBy($request->orderColumn, $request->orderDirection);;
}
$logs = $query->paginate($request->limit, ['*'], '', $request->page);
For example if $request->searchColumn is 'customer.name', how should I search? I've tried few ways but they don't work.
You may use whereHas method which allow to define additional query constraints on your relation. E.g:
$searchText = $request->searchText;
if ( explode( '.', $request->searchColumn )[0] == 'customer' ) {
$query->whereHas( 'customer', function( $query ) use ( $searchText ) {
$query->where( 'name', 'LIKE', '%' . $searchText . '%' )
});
}

How to convert this query in Laravel?

How do I convert this query to laravel form?
Somthing along the lines of:
Stocks::where('shop_id', $client_id)
->where(function ($query) use ($client_id, $ware_cd) {
$query->where('ware_id',
DB::raw(
'select ware_id from t_ware where shop_id=' . $client_id . ' and ware_cd=' . $ware_cd
)
);
})
->distinct()
->get();

Laravel Sortby Filter Search Products by Select

Whats the best way to build an Product Search Filter with an <select>?
My Route:
Route::any ( '/search', function () {
$q = Input::get ( 'q' );
if($q != ""){
$products = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->paginate (200)->setPath ( '' );
$pagination = $products->appends ( array (
'q' => Input::get ( 'q' )
) );
if (count ( $products ) > 0)
return view ( 'search' )->withDetails ( $products )->withQuery ( $q );
}
return view ( 'search' )->withMessage ( 'No Products!' );
} );
Now i want add an Drop down Filter with <select> to Search Page like:
<select name="sortby">
<option value="newproducts">New Products</option>
<option value="cheapprice">Cheapest Price</option>
</select>
Can i do it in my Route like:
if (select( ..newproducts... ) > 0)
return view ( 'search' )->withDetails ( $products )->withQuery ( $q );
}
Thanks for some tips:)
Sure you can add it there, but not with a conditional. You already pass a query parameter to search, just give the select a name and send that along too. You would use ->orderBy(...) on you collection you get when searching for $q.
// from your html
<select name="sortby">
// in your controller/route
$products->orderBy(request->get('sortBy'))
// new products
If (request->get('sortBy') === 'newproducts')
$products->orderBy('created_at', 'desc');
// cheap price
If (request->get('sortBy') === 'cheapprice')
$products->orderBy('price', 'asc');
https://laravel.com/docs/5.5/eloquent-collections#available-methods

write orwhere for multi tables in eloquent

I am using eloquent as ORM and I want to use where in multi-table like this:
$raw_query = EntityCity::with(['province']);
$raw_query = $raw_query->where(function ( $q ) use ( $search_data ) {
$q->where('city.title' , 'like' , "%$search_data%")
->orwhere('province.title' , 'like' , "%$search_data%");
});
}
$this->data[ 'result_list' ] = $raw_query->limit($this->per_page)
->orderByDesc("time_insert")
->offset(( $page_num - 1 ) * $this->per_page)
->get();
However, I encounter the following error:
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'province.title' in 'where clause' (SQL: select count(*) as aggregate from city where (city.title like %fars% or province.title like %fars%))
If I comment the orwhere it works.
So how can you write this with orwhere?
Instead of:
$raw_query = $raw_query->where(function ( $q ) use ( $search_data ) {
$q->where('city.title' , 'like' , "%$search_data%")
->orwhere('province.title' , 'like' , "%$search_data%");
});
}
you should use:
$raw_query = $raw_query->where(function($q) {
$q->where('city.title', 'like', "%$search_data%")
->orWhereHas('province', function ( $q ) use ( $search_data ) {
$q->where('province.title' , 'like' , "%$search_data%");
});
});
Notice that the where .. orWhereHas was wrapped here in additional where and this gives you confidence you can add any other conditions as for example selecting only active cities:
$raw_query = $raw_query->where(function($q) {
$q->where('city.title', 'like', "%$search_data%")
->orWhereHas('province', function ( $q ) use ( $search_data ) {
$q->where('province.title' , 'like' , "%$search_data%");
});
})->where('active', 1);
Try to use orWhereHas:
$raw_query = $raw_query->where('city.title', 'like', "%$search_data%")
->orWhereHas('province', function ( $q ) use ( $search_data ) {
$q->where('province.title' , 'like' , "%$search_data%");
});

How to output data using query builder while eliminating duplicates

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);
}

Resources