Can someone help me with this query.
table users
+------+---------------------+
| id | name |
+------+---------------------+
| 1 | John |
| 2 | Jade |
| 3 | Robbert |
| 4 | Steve |
+------+---------------------+
table friendship
+------+---------------------+
| uid | friend_id |
+------+---------------------+
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
+------+---------------------+
Assume current user id is 1.
Want to get the name of current user friends. ( all of them )
But this code returns only current user name for each friend it finds.
which for above example data, output is : John , John each one for each row.
$friends = DB::table('users')
->join('friendship', function($join)
{
$join->on('users.id', '=', 'friendship.uid');
})
->where('friendship.blocked', '=', '0' )
->where('users.id', '=', '1' )
->get();
Above SQL Code:
select * from `users`
inner join `friendship`
on `users`.`id` = `friendship`.`uid`
where `users`.`id` = 1
You should change your join condition. You're joining on the user id and you want to join on the friend side:
select name from users
join friendship on users.id = friendship.friend_id
where friendship.uid = 1
In short, you're getting 2 jhon because you have 2 friends of jhon but you're getting the userid info of those pieces of data and you want the friend side.
Fiddle here.
Might not be an exact answer to your question, but you should use Eloquent ORM to do things that simple, and it can be something liket this:
class User extends Eloquent {
public function friends()
{
return $this->hasMany('friendship', 'uid');
}
}
class Friendship extends Eloquent {
public function user($query)
{
return $this->belongsTo('User', 'friend_id');
}
public function scopeBlocked($query)
{
return $query->where('blocked', '=', '0');
}
public function scopeNotBlocked($query)
{
return $query->where('blocked', '=', '1');
}
}
Then you just have to use it:
$user = User::find(1);
$friends = $user->friends()->notBlocked()->get();
foreach($friends as $friend)
{
echo $friend->user->name;
}
Related
My requirement?
If i am the logged in user with Id = 1, then through the Messages Table i want to select users from Users Table to whom i sent the message or from whome i received the message.
Table 1: Users
+----+------+-------+
| id | name | email |
+----+------+-------+
| 1 | a | ??? |
| 2 | b | ??? |
| 3 | c | ??? |
| 4 | d | ??? |
| 5 | e | ??? |
| 6 | f | ??? |
| 7 | g | ??? |
| 8 | h | ??? |
| 9 | i | ??? |
| 10 | j | ??? |
+----+------+-------+
Table 2: Messages
+----+---------+-------------+
| id | user_id | receiver_id |
+----+---------+-------------+
| 1 | 1 | 2 |
| 2 | 3 | 1 |
| 3 | 4 | 1 |
| 4 | 1 | 5 |
| 5 | 1 | 3 |
+----+---------+-------------+
User Model
public function messages()
{
return $this->belongsToMany(User::class, 'messages', 'user_id', 'receiver_id');
}
Message Model
public function user()
{
return $this->belongsTo(User::class);
}
So what i have tried so far?
$id = Auth::id();
$users = User::with(['messages' => function($query) use($id){
$query->where('user_id', $id)
->orWhere('received_id', $id)
->orderBy('created_at', 'DESC');
}])->get();
dd($users);
What is the expected result?
Using this query, i am getting all of my 10 users. Although i should only get 4 users(those with id's 2,3,4,5).
If the above query is wrong, or i should follow another method or i should created some sort of relationships Please help.
Hopefully you have understood the question, i am new to Laravel but i am learning.
Probably what you need is three relations(one to many) in the User model. One for sent messages, one for received messages and one for both, like this:
public function messagesSent()
{
return $this->hasMany(Message::class, 'user_id');
}
public function messagesReceived()
{
return $this->hasMany(Message::class, 'receiver_id');
}
public function messages()
{
return $this->messagesSent()->union($this->messagesReceived()->toBase());
}
Then you should be able to get user messages like this: User::with('messages')->get();
I think you should use a join statement or "whereHas" to select users who have any messages.
$id = Auth::id();
$users = User::whereHas('messages', function ($query) use($id){
$query->where('user_id', $id)
->orWhere('received_id', $id);
})
->get();
To have access to "messages" you should add "with" statement too.
Adding my own solution(i.e working) to this question.
User Model
public function sent()
{
return $this->hasMany(Message::class, 'user_id');
}
public function received()
{
return $this->hasMany(Message::class, 'receiver_id');
}
Query
$users = User::whereHas('sent', function ($query) use($id) {
$query->where('receiver_id', $id);
})->orWhereHas('received', function ($query) use($id) {
$query->where('user_id', $id);
})->get();
dd($users);
I am creating a system using Laravel and AngularJS where I assign tasks to users. Multiple tasks has multiple users and vice versa. In the Database, I have this tables:
task:
id | name
task_users:
id | task_id | user_id
users:
id | name
In my view, I display a particular task, using id of task table. I display a list of users (called unassigned users) who are not assigned to that particular task. When that user is assigned, it's name gets removed from the list.
To achieve this, I used this query:
public static function remainingUser($task_id)
{
return \DB::table('users')
->leftjoin('task_users', 'task_users.user_id', '=', 'users.id')
->select('users.id',
'users.name as name'
)
->where('task_id', '!=', $task_id)
->orWhere('task_id', null)
->get();
}
Suppose I have this data
task:
id | name
1 | Task1
2 | Task2
3 | Task3
users:
id | name
1 | User1
2 | User2
3 | User3
4 | User4
5 | User5
6 | User6
7 | User7
8 | User8
9 | User9
10 | User10
task_users:
id | task_id | user_id
1 | 1 | 1
1 | 1 | 2
1 | 1 | 3
1 | 1 | 5
1 | 1 | 6
1 | 1 | 7
1 | 2 | 2
1 | 2 | 4
Now suppose I am displaying task details of task_id = 1 and I want to assign user 4 to this task. So my list of unassigned users should contain all the users who are not assigned this task. My query does not return the required data. I have also tried different conditions in where clause, but I do not get the correct required data. What am I doing wrong?
The issue exists because when you select from Users, you get all users left Joined with a single instance of tasks. So if User1 has Task1 and Task2, only Task1 will be matched here, because it will match User1 to the 1st row found for him within task_users. In order to list the unasigned users, your query would look similar to this:
public static function remainingUser($task_id)
{
return \DB::table('task_users')
->rightJoin('users', 'task_users.user_id', '=', 'users.id')
->select(
\DB::raw("DISTINCT(`users`.`id`)"),
'users.name as name'
)
->where('task_id', '!=', $task_id)
->orWhere('task_id', null)
->get();
}
please help..
I have 2 tables
Current user_id = 3
Users Table:
| user_id | email | name |
-------------------------------------------
| 1 | one#gmail.com | ridwan |
| 2 | two#gmail.com | budi |
| 3 | six#gmail.com | stevan |
| 4 | ten#gmail.com | agung |
Relations Table [ user_id and follower_id are related to Users Table ]
| relation_id | user_id | follower_id |
-----------------------------------------
| 1 | 1 | 3 |
| 2 | 2 | 3 |
i want to get the list of the user, but if i already have relation with a user, it will give me a status 'following', just like instagram, maybe look like this
{
user_id : 1,
name : ridwan,
status : following
},
{
user_id : 2,
name : budi,
status : following
},
{
user_id : 4,
name : agung,
status : not following
}
how can i do that in laravel?
thank you..
In your Relations model
public function user()
{
return $this->belongsTo(User::class);
}
Then, in your controller (where you want to get this list), you just need to use Eloquent or Query Builder to get what you want (with eager loading, it's always better) :
//get all users related to your_current_user
$relations = Relation::where('follower_id', your_current_user_id)->with('user')->get();
Finally, you just have to make manipulate your data like you want, for example :
foreach($relations as $relation){
$relation->user->name; //get the name of the related user
}
Take a look at Relationships !
Thank you guys.. finally I use sql case, i was only confusing about how to get the data with 'following' status
here is the code:
DB::table('users')->leftjoin('relationships', 'users.user_id', '=', 'relationships.user_id')->select('users.user_id','users.status as user_type','users.email','users.display_name','users.profile_image',DB::raw('(CASE WHEN relationships.follower_id = ' . $user_id . ' THEN "Following" ELSE "Not Following" END) AS status'))->orderBy('user_id','asc')->where('users.user_id','!=',$user_id)->get();
I try to eager load a polymorphic model with some conditions that's connected to two other models and having some problems.
My models:
class One extends Model {
public function twos() {
return $this->hasMany(Two::class);
}
public function threes() {
return $this->morphMany(Three::class, 'threeable');
}
}
class Two extends Model {
public function one() {
return $this->belongsTo(One::class);
}
public function threes() {
return $this->morphMany(Three::class, 'threeable');
}
}
class Three extends Model {
public function threeable() {
return $this->morphTo();
}
}
So far everythings is everything great: I've a lot of One items that have a lot of Two relations and both have even more Three relations (on its own).
Now I try to get the latest Three from Two, where it also can come from a One relation (highest threes.updated_at from either A or B).
The threes table looks something like this
| id | threeable_id | threeable_type | updated_at |
|---|---|---|---|
| 1 | 1 | A | 1 |
| 2 | 1 | A | 2 |
| 3 | 1 | B | 3 |
| 4 | 1 | B | 4 |
| 5 | 2 | A | 2 |
| 6 | 2 | A | 4 |
| 7 | 2 | B | 1 |
| 8 | 2 | B | 3 |
I want threes.id = 4 for B::find(1)->withC() and threes.id = 6 for B::find(2)->withC().
For performance reasons I want to load it in one query (I'm loading multiple Two and want the related Three and don't want to fire an own query for that). So I tried to put it in a scope (in class Two). Joining on the Three table and doing some max on the updated_at in a sub query ... It was a total mess and didn't worked that well
It didn't really feel the "Laravel-way" neither :(
Thanks for any help
EDIT
Maybe as an addition here's the pure SQL
SELECT twos.id, threes.id
FROM
(SELECT
twos.id twoId, MAX(th.updated_at) threeMaxUA
FROM
twos
LEFT JOIN (SELECT
th.*
FROM
(SELECT
threeable_id, threeable_type, MAX(updated_at) AS updated_at
FROM
threes
WHERE
deleted_at IS NULL
GROUP BY threeable_id , threeable_type) thmax
LEFT JOIN
threes pr ON (th.threeable_id = thmax.threeable_id
AND th.updated_at = thmax.updated_at
AND th.threeable_type = thmax.threeable_type)
GROUP BY th.threeable_id , th.threeable_type) th
ON
(
(th.threeable_id = twos.id AND th.threeable_type = 'App\\Two')
OR
(th.threeable_id = twos.product_id AND th.threeable_type = 'App\\One')
)
GROUP BY twos.id
) twthmax
LEFT JOIN twos ON twos.id = twthmax.twoId
LEFT JOIN threes ON (
twthmax.threeMaxUA = threes.updated_at
AND
(
(threes.threeable_id = twos.id AND threes.threeable_type = 'App\\Two')
OR
(threes.threeable_id = twos.product_id AND threes.threeable_type = 'App\\One')
)
)
I have two tables.
Table 1: table_company
+---------------------------+
| company_id | company_name |
+---------------------------+
| 1 | Apple |
| 2 | Samsung |
+---------------------------+
Table 2: table_products
+------------+--------------+-------------+-----------+
| product_id | product_name | category_id |company_id |
+-----------------------------------------------------+
| 1 | iPhone | 3 | 1 |
| 2 | galaxy | 3 | 2 |
| 1 | iPad | 4 | 1 |
| 2 | tab | 4 | 2 |
+-----------------------------------------------------+
I want to join this 2 tables to get the company name according to category_id.
I wrote the following code in my model. but did not get anything. Please help.
public function select_company_by_category_id($category_id) {
$this->db->select('*');
$this->db->from('tbl_products');
$this->db->join('tbl_company', 'company_id = company_id');
$this->db->where('category_id', $category_id);
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
try to replace your join with this:
$this->db->join('tbl_company', 'tbl_company.company_id = tbl_products.company_id');
you can find more examples in codeigniter active record page
Use Left Join for this
public function select_company_by_category_id($category_id) {
$this->db->select('*');
$this->db->from('table_products');
$this->db->join('table_company', 'table_company.company_id = table_products.company_id', 'left'); # Changed
$this->db->where('table_products.category_id', $category_id); # Changed
$query = $this->db->get(); # Improved
$result = $query->result_array(); # Improved
return $result;
}
First of all open your database error from database.php file only on development line not on production.
Than issue is that company_id is available in both tables with same name than you must need to add table alias as:
public function select_company_by_category_id($category_id)
{
$this->db->select();
$this->db->from('table_products');
$this->db->join('table_company', 'table_company.company_id = table_products.company_id');
$this->db->where('table_products.category_id', $category_id);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}