Laravel Relation Many to Many - laravel

I'm using laravel and sentinel (Sentry) that default using users, groups, and users_groups database table
users
-----------------
id name
1 user
2 user
3 admin
groups
-------------------
id name
1 Users
2 Admins
users_groups
--------------------
user_id group_id
1 1
2 1
3 1
3 2
i define the relation on laravel model and it's work fine when i try to print
$users = Group::find($id)->user;
but how to print just user only?
$users = Group::where(array("name"=>"Users"))->get()->user;
it's still show all data include the admins, i just want to show user only, how to do this?
i know the problem because at users_groups table admin have Users id too..
Please someone help me. It's verry confusing

You can use with to get the data of specified relation as below :
$users = Group::with('user')->find($id);
This will return data of group with user.
If you just want a data of only user table you have to change the query and rather than using model Group you have need to query on User model something like below:
$users = User::where('group_id', $groupId);
http://laravel.com/docs/5.1/eloquent-relationships
http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Model.html#method_with

Related

mysql ORDER BY depending on column

I have the table client_contacts like:
id
client_id
contact_type_id
data
1
1
3
johndoe#gmail.com
2
2
4
+999 999 999
3
..
..
....
In my contact_types table I have the following types:
id
description
1
address
2
fixed phone
3
email
4
mobile
I want the ability to sort all the contacts that are in the data column and by that, the possibility to sort with the contact_type_id. How to do it in Laravel 8 with Eloquent\Builder? I am using paginate as well.
Some uses:
Sort ASC all the emails and furthermore the possibility to sort two contact_types like fixed phone ASC and mobile DESC.
In my client model, I have hasMany() to the client_contacts model.
According to documentation:
MYSQL:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_field
LARAVEL:
https://laravel.com/docs/9.x/queries#orderbyraw
You can Add This Quote Of Code:
->orderByRaw("FIELD(contact_types.description, 'addresses', 'fixed phones', 'email', 'mobile') ASC")

How to get count from pivot table laravel?

I have two tables,
User cities user_cities
id name id name user_id city_id
1 aaa 1 cityA 2 1
2 bbb 2 cityB 2 3
3 ccc 3 cityC 1 2
3 3
3 2
Now i am filtering the user by cities. I am having the city_id = 2. So that i have to get the count of users belongs to the city_id 2.
How can i get that using laravel eloquent
As you describe in your answer you have the third table user_cities.
If you have table user_cities then count user like
DB::table('user_cities')->where('city_id', '2')->count();
There are different ways to do this, check https://laravel.com/docs/5.8/queries
But a simple way of doing it could be:
UserCity::whereCityId(2)->count();
Or:
UserCity::where('city_id',2)->count();
If you have set correctly the relationship between them, you can do something like that:
City::where('id', '=', 2)->withCount('users')->get();
It will place a {relation}_count column on your resulting model, and thus you can call it like a property: $city->users_count
Edit: For further information, you can check the official documentation.
Try this query :
$count = City::where('id', 2)->whereHas('users')->count();

get Average of column in one to many Relation in laravel

I Have 2 table:
stores
store_feedbacks | store_id , user_id ,rating
rating is number between 1-5 , Now I want to get average of all rating for one store,How I can do it?
Assuming your relationships are correctly set up:
$store->ratings()->avg();
Docs: https://laravel.com/docs/5.7/queries#aggregates - they're from the queries page, but it applies to Eloquent as well.

Laravel Polymorphic relations

Im having a bit of trouble to figure out how to make the polymorphic relations work, to create my dynamic user content as shown below.
my plan is to display content depending on the content_type and the link between is the id from the Users table and the user_id form UserData table. Those are both models too Users and UserData
Users table
*id* *username* *password*
1 johndoe 123
2 janedoe 321
UserData table
*user_id *content* *content_type*
1 John Doe full_name
1 john-doe.jpg avatar
1 laravel twitter
1 blue favorite_color
2 Jane Doe full_name
2 jane-doe.jpg avatar
2 Eloquent twitter
2 green favorite_color
I hope someone can see what my purpose here is and know a solution. AND maybe show how

Select data from Multiple table and show merged data into view mvc3

I have two tables like
USER_LOGIN
USER_ID USER_NAME PASSWORD
--------------------------------------------
1 User 1 some data
2 User2 somedata
3 User3 Some Data
And
USER_DETAIL
ID USER_ID NAME ADDRESS
-------------------------------
1 2 Name 2 Address
2 3 Name Three
Now how can select data from this two table by user id and show merged data into view??
Do your join operation in SQL Server as a function
Bring the result of the function from database to application using LINQ
Apply your result to the grid datasource
Yes, You can make it through join concept in SQL server
select * from USER_LOGIN A join USER_DETAIL B on A.USER_ID=B.USER_ID
Now you'll get the all data from joining two tables and can pass to a datatable or dataset anything, then you can pass it to controller and to view. Still you can choose you join style it may be "leftjoin","rightjoin" like this.
Please refer these links for clear explanation
http://www.w3schools.com/sql/sql_join.asp
http://beginner-sql-tutorial.com/sql-joins.htm
Hope it helps!!!

Resources