HOW TO: Laravel 5 - mySQL query - laravel

the situation:
I have one mySQL database and I want to get a dinstinct count of the column C_ID where the set timestamp is like the current day (e.g. 2015-08-14)
in phpMyAdmin it works fine:
SELECT COUNT(DISTINCT C_ID) FROM table WHERE touched_at LIKE '2015-08-14%'
the result is 32; but in Laravel 5 I get 320 because the distinct function does not work for me
$date = date('Y-m-d');
$one = DB::table('talbe')
->select()
->where('touched_at', 'like', "$date%")
->distinct('C_ID')
->count();
Can you please assist me with this problem?
Thanks in advance
Timo

This may work. Tried it in one of my projects and that produces the right answer for me.
$one = DB::table('talbe')
->where('touched_at', 'like', "$date%")
->count(DB::raw('distinct C_ID'));

Try this if it works:
$date = date('Y-m-d');
$one = DB::table('talbe')
->select('touched_at')
->where('touched_at', 'like', "$date%")
->distinct('C_ID')
->count();

You may also try this code
$date = date('Y-m-d');
$one = DB::table('table')
->select(DB::raw('count(Distinct C_ID) as C_ID'))
->where('touched_at', 'like', "$date%")
->get();

Related

Laravel date time condition not working properly

I have two columns in my offer table as follow, I want to fetch those offer's record which is currently active:
offer_start_date
offer_end_date
24-10-2022 10:57:00
24-10-2023 10:57:00
Below is my query date condition working fine but time is giving problems:
$now = Carbon::now();
$today = $now->toDateString();
$currentTime = $now->toTimeString();
$offers_data = Offer::whereRaw("offer_status=2 and offer_type=1 and is_special_offer=3")
->whereDate('offer_start_date','<=',$today)
->whereTime('offer_start_date','<=',$currentTime)
->whereDate('offer_end_date','>=',$today)
->whereTime('offer_end_date','>=',$currentTime)
->limit(10)
->get();
What would be the reason?
Using whereDate and whereTime together is likely not working the way you think it is.
Instead, try just using where() and Laravel will work it out for you.
$now = Carbon::now();
$offers_data = Offer::whereRaw("offer_status=2 and offer_type=1 and is_special_offer=3")
->where('offer_start_date', '<=', $now)
->where('offer_end_date', '>=', $now)
->limit(10)
->get();

Retrieve data between dates in laravel

I am using an eloquent query to retrieve data from a table. The table columns look like this:
id started_at finished_at
1 06/02/2019 06/15/2019
2 06/05/2019 06/11/2019
What I want to do is, given a $date (ex: 06/08/2019 ) and get the data of the row, that the $date between started at and finished_at columns.
DB::table('table_name')->whereBetween('started_at', [$date1, $date2])
->orWhereBetween('finished_at', [$date1, $date2])->get();
$date = date('Y-m-d',strtotime($started_at));
$data = Model::where('started_at', '>', $date)->where('finished_at', '<', $date)->first();
Try this
$data = Model::where('started_at', '<', $date)->where('finished_at', '>', $date)->first();

Using day, Month or Year as MYSQL query filter in Laravel

In my code, i am querying a database by accepting month and year input from user.
I have tried writing it the normal PHP way and other ways i can find online but none seems to be working. here is the code i am using currently
$salesmonth = $request->input('mn');
$salesyear = $request->input('yr');
$id = Auth::user()->id;
$comm = \DB::table('bakerysales')
->where([
['customer_id', '=', $id], [MONTH('sales_date'), '=', $salesmonth], [YEAR('sales_date
'), '=', $salesyear]
])
->get();
return view::make('showCommission')->with('comm', $comm);
I expect the query to return data from rows that match user selected month and year
Laravel comes with a few different where clauses for dealing with dates e.g.
whereDate / whereMonth / whereDay / whereYear.
This means that your controller method can look something like:
$comm = \DB::table('bakerysales')
->where('customer_id', auth()->id())
->whereMonth('sales_date', $request->input('mn'))
->whereYear('sales_date', $request->input('yr'))
->get();
return view('showCommission', compact('comm'));

Counting columns with if conidition in query

Counting only columns in which, Buy_Rate is more than Sell_Rate.
My query is not resulting as per expected, it is resulting me wrong.
$user_id = Auth::user()->id;
$losing_trades_count = FinalTrade::where('user_id', '=', $user_id)->where('buy_rate', '<', 'sell_rate')->get()->count();
Inverse: If sell_rate is more than buy_rate then only, count the columns.
You can use whereColumn
$losing_trades_count = FinalTrade::where('user_id', '=', $user_id)
->whereColumn('buy_rate', '<', 'sell_rate')
->count();
Also there is no need to call get() when you need count() from query builder
laravel eloquent where don't support comparing columns. so you need use raw SQL in order to compair two columns. you can do something like,
$user_id = Auth::user()->id;
$losing_trades_count = FinalTrade::where('user_id', '=', $user_id)->whereRaw('buy_rate < sell_rate')->get()->count();
hope this helps!

Select Join Where and orWhere in Laravel 5

I have two tables- jobseekers and resumes. I try to achieve three search options- by entering 1. "first name", 2. "last name", and 3. "first name + last name". My current code is as below:
$q = \Request::get('keyword');
$data['resume'] = Resume::join('jobseekers', 'jobseekers.user_id', '=', 'resumes.user_id')
->where('jobseekers.first_name','like','%'.$q.'%')
->orWhere('jobseekers.last_name','like','%'.$q.'%')
->orderBy('resumes.updated_at','desc')->paginate(50);
Using my texbox (keywords), When I search only last / first name, it works fine. However, when I type both first + last name in the textbox, it shows no result.
Please share me how to achieve this.
Using CONCAT():
->orWhere(DB::raw("CONCAT(jobseekers.first_name, ' ', jobseekers.last_name)"), 'LIKE','%'.$q.'%');
Use where and or where like this
$results = App\Table::select('*')
->where(function ($query) use ($search_term) {
$query->where('column3', 'like', $search_term.'%')
->orWhere('column4', 'like', $search_term.'%')
})
->orderBy('column1', 'asc')
->get();
Hope this help

Resources