I have a query like this:
$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->get();
and a table that has a column id (users.id) and another table that has columns id and user_id (user_roles.id & user_roles.user_id),
but the problem is.. what is being returned on $user->id is the user_roles.id instead of the users.id column.. how do i fix this so that what i get is not the role id but the user id instead..
thanks!
Found it!
using ->select('users.*', 'user_roles.role_name') i was able to remove user_roles.id from the returned values and thus eliminating the conflict.
Here is the final query:
$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->select('users.*', 'user_roles.role_name')->get();
The better way to do this is using 'as':
$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->get(array('users.*', '**user_roles.id as user_roles_id**', 'user_roles.*'));
Related
I have Users table which has under_reference column that represents by whom this user was referred.
I want to order user by having highest under_reference count. How to order by counting under reference
$data['user'] = User::orderBy('id','DESC')->get();
$data['user'] = User::select(DB::raw('count(*) as total'))
->groupBy('under_reference')
->orderBy('under_reference','DESC')
->get();
Here is the query that can help you. You can get the total users referred by a person and orderBy reference.
Use Group By for this situation, and sort from it. Here's an example.
$query = [
'name',
'email',
// store count of under_reference as total
DB::raw('count(under_reference) as total')
];
$sorted = User::groupBy('name', 'email')
->select($query)
->orderBy('total', 'desc') // remember total before, sort it by desc or asc
->get();
You can refer from laravel documentation here on raw expressions
The name and email is just an examaple, as you didn't show us what your table's column looks like. But you should get a pretty rough idea
i created another column totalref with Integer . i coded logic to update this column also with referral bonus and then orderby totalref. and it worked like magic
Try this:
$user_info = DB::table('users')
->select('under_reference',DB::raw('count(under_reference) as ur'))
->groupBy('under_reference')
->orderBy('ur','DESC')
->get();
You can try in this way also worked.
$data['user'] = DB::select('SELECT *` FROM `user` ORDER BY
CAST(`under_reference` AS decimal) DESC');
You can cast the DB column varchar to decimal in query.
I have three tables:tbl_borrower, tbl_clientandtbl_guarantor
tbl_Client:
id|name|address|email|
tbl_Guarantor:
id|clientid(fk)|Guaranteed_date|borrower_id(fk from borrower table)
I want to retrieve all the values of client table except the values which are present in guarantor table in the controller of Laravel 5.5.
Once you have the models and relationships set up, you should just do:
Client::doesntHave('guarantor')->get()
https://laravel.com/docs/5.6/eloquent-relationships#querying-relationship-absence
If you're using the query builder, it would be:
$clients = DB::table('tbl_clients')
->join('tbl_guarantor', 'tbl_clients.id', '=', 'tbl_guarantor.clientid')
->select('tbl_clients.*')
->whereNull('tbl_guarantor.clientid')
->get();
https://laravel.com/docs/5.5/queries
With the premise of using a left join and testing for NULL on the 2nd table id based on this answer. https://stackoverflow.com/a/4076157/3585500
Try this :
DB::table('tbl_Client')
->groupBy('tbl_Client.id')
->leftjoin('tbl_Guarantor', 'tbl_Client.id', '=', 'tbl_Guarantor.clientid')
->get([
'tbl_Client.*'
]);
SELECT myTable.*, otherTable.foo, otherTable.bar...
how can we write above query in cakephp ? I tried this but didn't work.
$data = $this->Articles->find()->select(['Articles.*','Categories.name'])->innerJoineWith('Categories');
It giving me error near SELECT Fees.* ASFees__*.
So instead of that, I have to write all columns of the Article Table.
$data = $this->Articles->find()->select(['Articles.id','Articles.name','Articles.title','Articles.description','Categories.name'])->innerJoineWith('Categories');
is there any solution in cakephp? please tell me. Thank You.
You can do like this:
$this->Articles->find('all')->contain(['Categories' => function($q) {
return $q->select('Categories.name');
}])->select($this->Articles);
$this->Articles in select statement will fetch all the records from
the Articles table and $q->select('Categories.name') will fetch only
Category name from the associated Categories table.
Ref: https://github.com/cakephp/cakephp/issues/7913
$data = $this->Articles->find()
->select($this->Articles)
->select(['Categories.name'])
->innerJoineWith('Categories');
I have table contain id, customer_id,..etc so findOrFail will work only for id so want to check customer_id record not id.
How can I check other record using findOrFail something like this
$post = Customer::findOrFail('customer_id',$request->customer_id)
Maybe you are looking for firstOrFail(), so you can do:
Customer::whereCustomerId($request->customer_id)->firstOrFail();
findOrFail() only works for the primary key id so you have to use firstOrFail.
Note that you could also write it as:
Customer::where('customer_id', $request->customer_id)->firstOrFail();
You can use any column of a table
Customer::where('<any column>', <column value>)->firstOrFail();
Laravel example
$model = App\Flight::findOrFail(1);
$model = App\Flight::where('legs', '>', 100)->firstOrFail();
I am using the following to sum a total column:
$hive_count = Hive::where('active','true')
->groupBy('hive_type_id')
->selectRaw('sum(total) as sum, hive_type_id')
->pluck('sum','hive_type_id');
But rather than using the hive_type_id for the array key, I would like to access the hive_type name from the hive_types table (column 'name'). I have tried hive_type_id.name but this did not work.
Models: Hive & HiveType
Thanks for your help.
I would like to access the hive_type name from the hive_types table (column 'name').
You've to join the table hive_types in your query so you can access the name :
$hive_count = DB::table('hives')
->where('active','true')
->join('hive_types', 'hives.hive_type_id', '=', 'hive_types.id')
->groupBy('hive_type_id','hive_types.name')
->selectRaw('sum(total) as sum, hive_types.name as name')
->pluck('sum','name');