I want the distinct names from my database table.The table contains column id and name.
I have tried
$diff=Crud::distinct('name')->get();
$diff=Crud::distinct()->get();
Both of them not worked.They are returning the result with duplicates.Here Crud is my eloquent model.
But when i use
$cnt=Crud::distinct()->count('name'); //returns the correct count
and
$u=Crud::all();
$diff=$u->unique('name'); //returns the distinct names
Why duplicate values are returned while i'm using the distinct? I want distinct names by using distinct.
Thanks.
distinct() doesn't accept arguments. Use one of these options:
$diff = Crud::distinct()->pluck('name');
$diff = Crud::distinct()->get(['name']);
$diff = Crud::distinct()->select('name')->get();
Related
So i am trying to get distinct collections like this
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->get();
But when i do a count($data) I get 1600 but when i run
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->count();
I get 1440. This is weird as i only want collection with distinct field 'Farmer_BankVerificationNumber_Farmer'. How do i write the query correctly?
It's because you're asking your query to count the distinct values in the wrong place.
You need to tell your count parameter what field you would like to count on. So you'll basically ask the query to get the database information, separate the distinct values and then count how many of a specific field are distinct.
Your finished query should look like this:
$data = DB::table('project_1_data')
->distinct()
->count('Farmer_BankVerificationNumber_Farmer');
In a table I want to get the patientNames grouping them based on appointmentDate. But is only returning me array of one value.
I tried:
$app = DB::table('appointment')->select('appointmentDate','patientName')
->where('doctorId',$id)
->groupBy('appointmentDate')->get();
I want all the patients groupedby the appointment date, but it gives me only one patient detail, though multiple patients have same appointmentDate.
That's how sql group by works.
Try with orderBy instead, if you just need them sorted all by appointmentDate.
$app = DB::table('appointment')->select('appointmentDate','patientName')
->where('doctorId',$id)
->orderBy('appointmentDate')->get();
If you want all patients in separate arrays for each appointmentDate it's better to use Eloquents instead of DB select():
$app = Appointment::where('doctorId',$id)->get()->groupBy('appointmentDate');
I run in a strange issue, I need to sum a column using eloquent but every function I use eloquent do not consider the decimal.
First of all I have the column set ad DECIMAL(8,2) and in my DB the data is stored in the correct way.
If I just use a simple query:
select sum(total_after_tax) from invoice_details where invoice_id = 1
I get the result: 312.93
Now if I use eloquent:
$invoice = Invoice::with(['details'])->get();
dd($invoice->first()->details->sum('total_after_tax'));
I get: 312.
How is this possibile? How can I get the correct value form DB?
I have a model Form for table forms. There is a column called guid which can be null, or contain some sort of grouping random hash.
I need to select all forms that have column guid either null or unique in current search. In other words, for repeating guid values in current search I select only first occurence of every guid hash.
I tried:
$results = App\Form::where(... some where clauses .. ).groupBy('guid')
and it's almost ok, but for all rows, where guid == NULL it groups them and selects only one (and I need all of them).
How can I get the unique or null rows either by building proper SQL query or filtering the results in PHP?
Note: I need my $results to be an Illuminate\Database\Eloquent\Builder instance
EDIT:
I fount out that SQL version of query I need is:
SELECT * FROM `forms` WHERE .... GROUP BY IFNULL(guid, id)
What would be equivallent query for Laravel's database query builder?
UPDATE: Using DB::raw
App\Form::where(... conditions ...)
->groupBy(DB::raw("IFNULL('guid', 'id')"));
Or the another way could be:
You can also use whereNotNull, whereNull & at last merge both the collections using merge() like this:
First get the results where guid is grouped by (excluding null guid's here):
$unique_guid_without_null = App\Form::whereNotNull('guid')->groupBy('guid')->get();
Now, get the results where guid is null:
$all_guid_with_null = App\Form::whereNull('guid')->get();
and at last merge both the collections using merge() method:
$filtered_collection = $unique_guid_without_null->merge($all_guid_with_null);
Hope this helps!
For your edited question, you can use raw() as;
->groupBy(DB::raw("IFNULL('guid', 'id')"))
So your final query will be as:
$results = App\Form::where(... some where clauses .. )
->groupBy(DB::raw("IFNULL('guid', 'id')"));
By above query, your $results will be an instance of Illuminate\Database\Eloquent\Builder.
Im using Eloquent. But I'm having trouble understanding Eloquent syntax. I have been searching, and trying this cheat sheet: http://cheats.jesse-obrien.ca, but no luck.
How do i perform this SQL query?
SELECT user_id FROM notes WHERE note_id = 1
Thanks!
If you want a single record then use
Note::where('note_id','1')->first(['user_id']);
and for more than one record use
Note::where('note_id','1')->get(['user_id']);
If 'note_id' is the primary key on your model, you can simply use:
Note::find(1)->user_id
Otherwise, you can use any number of syntaxes:
Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first();
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
Also note, in any of these examples, you can also just assign the entire object to a variable and just grab the user_id attribute when needed later.
$note = Note::find(1);
// $user_id = $note->user_id;