I write code like this
$category = Input::get('category'); // ?category=1
if(!empty($category)){ // ?category=1, category=2
$lists = \App\Test::where('created_at', '<=', 'now()')
->where('category', $category) // append this.
->orderBy('id','desc')
->get();
}
else { // ?category=, category=0
$lists = \App\Test::where('created_at', '<=', 'now()')
->orderBy('id','desc')
->get();
}
That is so work but I think dirty code.
I dont wanna write same code again if I can.
So I wish to do like this ( Not working )
$category = Input::get('category'); // ?category=1
$lists = \App\Test::where('created_at', '<=', 'now()');
if(!empty($category)){ // ?category=1, category=2
$lists .= $lists::where('category', $category);
}
$lists .= $lists::orderBy('id','desc')->get();
Anyone know kind solutions?
Use this code
$lists = \App\Test::where('created_at', '<=', 'now()');
if(!empty($category)){ // ?category=1, category=2
$lists = $lists->where('category', $category);
}
$lists->orderBy('id','desc')->get();
You can do it like:
$lists = \App\Test::where('created_at', '<=', 'now()');
and then when you want to append anything, add it like this:
if(!empty($category)){ // ?category=1, category=2
$lists = $lists::where('category','=', $category);
}
you don't need to use .
Related
I want to filter data from multiple tables so I write query like that and one thing I let you know that both data1 and data2 return same column with different data.
$data1 = collect(DB::table('table_1')->get()->toArray());
$data2 = collect(DB::table('table_2')->get()->toArray());
$results = $data1->merge($data2);
if ($request->name!="") {
$results->when(request('name'), function($q){
$q->Where('name', request('name'));
});
}
.
.
.
return $results;
But $results returns all data and not filter data
Try this :
$data1 = collect(DB::table('table_1')->get()->toArray());
$data2 = collect(DB::table('table_2')->get()->toArray());
$results = $data1->merge($data2);
if ($request->name!="") {
$filter_results = $results->when($request->name, function($q){
$q->where('name', $request->name);
});
}
.
.
.
return $filter_results;
Try this
$data1 = collect(DB::table('table_1')->get()->toArray());
$data2 = collect(DB::table('table_2')->get()->toArray());
$results = $data1->merge($data2);
$results->when($request->name, function($q) use($request){
$q->where('name', $request->name);
});
return $results;
I have tried to trim "appointment" to check if it is equal to string that I have sent by trim function from ajax. But trim('appointment') doesn't give me what I need. How can delete whitespaces from object in sql to check some condition using where
public function category_filter(Request $request)
{
$appointment = json_decode($request->appointment);
$categories = [];
foreach($appointment as $a){
$cat_c_id = Products::where(trim('appointment'), '=', $a)->select('id')->get();
foreach($cat_c_id as $cat_c){
array_push($categories, $cat_c);
}
}
var_dump($categories);
}
The correct answer here is to fix your data and not store it with whitespace in the first place. Failing that, you need to use a raw statement:
public function category_filter(Request $request)
{
$appointment = json_decode($request->appointment);
$categories = [];
foreach($appointment as $a) {
$cat_c_id = Products::whereRaw('TRIM(appointment) = ?', [$a])
->select('id')
->get();
foreach($cat_c_id as $cat_c){
array_push($categories, $cat_c);
}
}
var_dump($categories);
}
Taking it a step further, you are performing pointless foreach loops that can be replaced with something like this:
public function category_filter(Request $request)
{
$appointment = json_decode($request->appointment);
// build a string like TRIM(appointment) IN (?,?,?,?)
$raw = 'TRIM(appointment) IN (' . implode(',', array_fill(0, count($appointment), '?')) . ')';
$categories = Products::whereRaw($raw, $appointment)
->select('id')
->get()
->toArray();
}
var_dump($categories);
}
trim('appointment') //this will trim the string 'appointment', not its value on table
try this (considering that Products::where allow this type of implementation):
...
$cat_c_id = Products::where('trim(appointment)', '=', trim($a))->select('id')->get();
...
Good day guys, I'm working on plotting data on charts using ChartJs and Laravel, I need to plot data based on months and I want the whole 12 months showing on the chart even without data,
I have months in an array ['January', 'February'....
Then I have my data here:
$stats = DB::table('wallet_payouts')
->groupBy('date')
->orderBy('date', 'ASC')
->get([
DB::raw('DATE_FORMAT(created_at, "%M") as date'),
DB::raw('COUNT(*) as value')
]);
$labels = [];
$data = [];
foreach ($months as $month){
foreach ($stats as $stat){
if ($month == $stat->date){
array_push($labels, $stat->date);
array_push($data, $stat->value);
}else{
array_push($labels, $month);
array_push($data, 0);
}
}
}
But the issue is instead of 12 months after the loop I'm getting 24... Duplicates and the data also in 24... I want the months to just match with the data without the duplicates
Try the below code:
$stats = DB::table('wallet_payouts')
->groupBy('date')
->orderBy('date', 'ASC')
->get([
DB::raw('DATE_FORMAT(created_at, "%M") as date'),
DB::raw('COUNT(*) as value')
]);
$labels = $months;
$data = [];
$temp = [];
foreach ($stats as $stat){
$temp[$stat->date] = $stat->value;
}
foreach($months as $month){
if(array_key_exists($month,$temp)){
array_push($data, $temp[$month]);
} else {
array_push($data, 0);
}
}
It is the result function inside the HotelController.
public function result()
{
$data=Input::except(array('_token'));
$city= $data['city'];
$cities_id = DB::table('cities')
->select('id')
->where('cities.city_name', 'LIKE', "%$city%")
->get();
$hotel = array();
foreach ($cities_id as $value) {
$i=$value->id;
$hotel[] = DB::table('add_hotels')
->select('id')
->where('city_id', '=', $i)
->get();
}
var_dump($hotel);
exit();
return view('hotel.result',compact('hotel','city'));
}
This is the result which i'm getting, but I required only data marked by red color box
Try this query:
$cities_id = DB::table('cities')
->where('cities.city_name', 'LIKE', "%$city%")
->Join('add_hotels','add_hotels.city_id','=','cities.id')
->select('add_hotels.id')
->get();
var_dump($cities_id);
There are many matching credentials inside the table which are inserted to an foreach loop however I am getting the results only from the first foreach element. How could I fix that?
foreach($matches as $match)
{
$object = $match->object;
$sales->whereRaw("match (`object`) against (?)", array($object));
if($match->colourBase == '1')
{
$sales->where('colour', '=', 'CC');
$sales->orWhere('colour', '=', 'CC+DD');
if($match->maxPrice)
{
$sales->where('price', '<=', $match->maxPrice);
}
if($match->minPrice)
{
$sales->where('price', '>=', $match->minPrice);
}
}
}
$results = $sales->orderBy('sales.updated_at', 'asc')->get();
#update
This is how I get $matches
$matches = Match::where('PeopleID', '=', $id)->get();
If you want only one record
use first() function.
No need to use foreach. :)
$matches = Match::where('PeopleID', '=', $id)->first();