I have a db table dateprices
id|tour_id|start|end|price
I want to get all the price of the tour starting and ending between the dates i.e. 2017-06-20 and 2017-07-11
I have written following query but it is returning an empty collection:
$dates = DatePrice::where('tour_id','=',$request->product_id)
->where('start', 'like', '%'.$request->start.'%')
->where('end', 'like', '%'.$request->end.'%')
->get();
Is there something wrong with my query ?
You should use the whereBetween() function like so:
$dates = DatePrice::where('tour_id','=',$request->product_id)
->whereBetween('start', array('2017-06-20', '2017-07-11'))
->whereBetween('end', array('2017-06-20', '2017-07-11'))
->get();
Try with below query,
If there is a single condition then you can user whereBetween().
If you would like to add more condition, you can use orWhereBetween().
$dates = DatePrice::where('tour_id','=',$request->product_id)
->whereBetween('start', 'like', '%'.$request->start.'%')
->orWhereBetween('end', 'like', '%'.$request->end.'%')
->get();
You can search by specifying the name of the column (dates) like so;
$dates = DatePrice::where('tour_id','=',$request->product_id)
->whereBetween('date', array($request->from_date, $request->to_date))
->get();
Related
I am looking for a way to take one query in Eloquent to get all search results, and then take those results, group them by type, and output the different types.
I currently have this setup to get all my search results:
$listings = Listing::where('listings.expiration_date', $date_operator, date('Y-m-d H:i:s'));
$listing->where(function($query) use ($keyword) {
$query->where('listings.lot_number', 'LIKE', '%'.$keyword.'%')
->orWhere('listings.title', 'LIKE', '%'.$keyword.'%')
->orWhere('listings.brand', 'LIKE', '%'.$keyword.'%')
->orWhere('listings.grade', 'LIKE', '%'.$keyword.'%')
->orWhere('listings.tags', 'LIKE', '%'.$keyword.'%')
->orWhere('listings.player', 'LIKE', '%'.$keyword.'%');
})->leftJoin('bids', function($join){
$join->on('bids.listing_id', '=', 'listings.id')
->on('bids.id', '=', DB::raw("(select max(id) from bids WHERE bids.listing_id = listings.id)"));
})->leftJoin('media', function($join) {
$join->on('media.listing_id', '=', 'listings.lot_number')
->where('media.group_order', '=', '1')->groupBy('media.group');
});
$listings = $listings->get();
The resulting $listings shows all the search results that I want to appear. Each listing has a specific type (item_type) assigned to them (i.e. Card, Ball, Bat, etc). I'd like to take all those results and group the types so that I can get a unique list of types to display in the menu. I've tried GroupBy on the Collection but that doesn't seem to be working.
Thank You
Use the power of collections
$itemTypes = $listings->pluck('item_type')->unique()
// if item type is a sub array / relationship then you will need to use dot notation
So we are plucking only the 'item_type' field, removing all duplicates by using the unique method and you should then have a collection of unique item_type's
my code in LaporanController.php
class LaporanController extends Controller
{
public function index(Request $request)
{
if (isset($request->start_date) && isset($request->end_date)) {
$start_date = Carbon::parse($request->start_date)->format('Y-m-d');
$end_date = Carbon::parse($request->end_date)->format('Y-m-d');
$attendance_absent = DB::table('attendance_absent as absent')
->whereBetween('absent.do_date_start', 'absent.do_date_end', [$start_date, $end_date])
->get();
dd($attendance_absent);
}
}
}
how to get request data from start_date and end_date according to attendance_absent table from database fields do_date_start and do_date_end? i try to use whereBetween but i get error : Illuminate\Database\Query\Builder::whereBetween(): Argument #2 ($values) must be of type array, string given. how to solve my problem ?
normally whereBetween using for single column date check. but in this case youu need get from the different columns. so try to check those date like this. i think it will we help full for you.
do try it like this
->whereDate('do_date_start', '>=', $from_date)
->whereDate('do_date_start', '<=', $to_date)
->whereDate('do_date_end', '>=', $from_date)
->whereDate('do_date_end', '<=', $to_date)
other thin if you used whereBetween you will not get equal date of today.
whereBetween function is used to query one field with 2 or more values, what you really want is just where function twice, try this:
...
->where([
['absent.do_date_start', '>=', $start_date],
['absent.do_date_end', '<=', $end_date],
])
->orWhere(function ($query) use ($start_date, $end_date) {
$query->where([
['absent.do_date_start', '>', $start_date],
['absent.do_date_start', '>', $end_date],
['absent.do_date_end', '<', $start_date],
['absent.do_date_end', '<', $end_date],
])
})
->get();
...
Hi I have this eloquent function in my controller to return rows to my view , I want to limit to filter by today's date
here is my function:
$rows = \App\Models\WeightsJournalView::where(function($query) use($request){
$query->orwhere('id','like',$request['search'])
->orwhere('currentAvgWeightMale','like',$request['search'])
->orwhere('currentAvgWeightFemale','like',$request['search'])
->orwhere('pastAvgWeightMale','like',$request['search'])
->orwhere('pastAvgWeightFemale','like',$request['search'])
->orwhere('batch_name','like',$request['search'])
->orwhere('barn_name','like',$request['search'])
->orwhere('farm_name','like',$request['search'])
->orwhere('created_at','like',date('Y-m-d'));
})->orderBy($request['sort'],$request['order'])
->skip($request['offset'])
->take($request['limit'])
->get();```
you can use:
orWhereBetween('created_at',[Carbon::now()->startOfDay(),\Carbon::now()->endOfDay()])
\Carbon::now()->startOfDay() will give you the very beginning of today
\Carbon::now()->endOfDay() will give you the very ending of the day
or you can use:
orWhereDate('created_at',Carbon::now()->toDateString())
orWhereDate will take the only date part of dateTime column, and it value will be compared to date string of now.
That should do it if I understood your problem correctly
$rows = \App\Models\WeightsJournalView::whereDate('created_at', '=' , date('Y-m-d'))
->andWhere(function ($query) {
$query->where('id','like',$request['search'])
->orWhere('currentAvgWeightMale','like',$request['search'])
->orWhere('currentAvgWeightFemale','like',$request['search'])
->orWhere('pastAvgWeightMale','like',$request['search'])
->orWhere('pastAvgWeightFemale','like',$request['search'])
->orWhere('batch_name','like',$request['search'])
->orWhere('barn_name','like',$request['search'])
->orWhere('farm_name','like',$request['search'])
->orWhere('created_at','like',date('Y-m-d'));
})->orderBy($request['sort'],$request['order'])
->skip($request['offset'])
->take($request['limit'])
->get();```
The first solution worked after I tweaked it like the following thanks Guys :
$rows = \App\Models\WeightsJournalView::whereDate('created_at', '=' , date('Y-m-d'))
->Where(function ($query) use($request){
$query->where('id','like',$request['search'])
->orWhere('currentAvgWeightMale','like',$request['search'])
->orWhere('currentAvgWeightFemale','like',$request['search'])
->orWhere('pastAvgWeightMale','like',$request['search'])
->orWhere('pastAvgWeightFemale','like',$request['search'])
->orWhere('batch_name','like',$request['search'])
->orWhere('barn_name','like',$request['search'])
->orWhere('farm_name','like',$request['search']);
})->orderBy($request['sort'],$request['order'])
->skip($request['offset'])
->take($request['limit'])
->get();
I wanted to manipulate how data will be shown with cost layering methods such as LIFO and FIFO.
To get things started, here's my query:
$items = Inventory::whereActive('1')
->where('quantity', '>', 0)
->where(function($query) use($request) {
$query->where('name', 'LIKE', '%'.$request->search.'%')
->orwhere('ref_code', 'LIKE', '%'.$request->search.'%');
})->orderBy('created_at', 'dsc')
->groupBy('ref_code')
->get();
I wanted to use groupBy with ref_code so it will only show one data with the same ref_code, and I used orderBy with created_at and dsc for descending so that one data that will be shown will be the last one that was added. (Well, that's what I think that would happen).
But unfortunately, when I used groupBy, it only shows the first added data. How can I select the last data? I thought orderBy was gonna do the job but it didn't.
You can use an alternative way for this. If you have auto incrementing primary key as id then you can do it like,
$ids = Inventory::whereActive('1')
->where('quantity', '>', 0)
->where(function($query) use($request) {
$query->where('name', 'LIKE', '%'.$request->search.'%')
->orwhere('ref_code', 'LIKE', '%'.$request->search.'%');
})
->select('ref_code',\DB::raw('MAX(id) as ID'))
->groupBy('ref_code')
->pluck('ID');
Now, Get Items as
$items = Inventory::whereIn('id',$ids)->get();
Hence you will get your desired result.
I have a variable where I get the records from GameMute modal and I need to know how I can only get the ones where expires_at is not yet expired. Its formatted just like created_at
$mutes = GameMute::orderBy('created_at', 'asc')->get();
Looked all over google but its with formatted time or something else.
Thanks.
Use Carbon like this,
$mutes = GameMute::where('expires_at','>', \Carbon\Carbon::now())
->orderBy('created_at', 'asc')->get();
Try this one :
$date = date("Y-m-d h:i:s");
$users = DB::table('users')->whereDate('expires_at',">",$date))
->orderBy('created_at', 'asc')->get();
Try where condition
$mutes = GameMute::whereRaw('expires_at > now()')->orderBy('created_at', 'asc')->get();
Try these
$mutes = GameMute::where('expires_at ', '>', new DateTime('today'))->orderBy('created_at', 'asc')->get();
Hope your expire_at column is a date type column
$mutes = GameMute::where('expires_at','>',date('Y-m-d'))
->orderBy('created_at', 'asc')
->get();
Or if it contained date time value like "2017-07-31 16:50" then you can query like this :
$mutes = GameMute::whereDate('expires_at','>',date('Y-m-d'))
->orderBy('created_at', 'asc')
->get();