Laravel - joining 2 tables with OR operator
I have a table called messages, and it's columns are
id, sender_id, message, receiver_id, created_at, updated_at
And the other table is called users, and it's columns are
id, firstname, lastname, email, password, created_at,updated_at
I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id
However I don't know how to use "OR" operator while joining in laravel
here's my raw query that gives me the desired output.
SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id
Here's the laravel query that I tried
$get_messages = DB::table('messages')
->where('messages.sender_id',$user_id)
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->on('users.id','=','messages.receiver_id');
})
->orwhere('messages.sender_id',$partner_id)
->where('messages.receiver_id',$user_id)
->orwhere('messages.receiver_id',$partner_id)
->groupby('messages.id')
->get();
print_r($get_messages);
php mysql laravel laravel-5
add a comment |
I have a table called messages, and it's columns are
id, sender_id, message, receiver_id, created_at, updated_at
And the other table is called users, and it's columns are
id, firstname, lastname, email, password, created_at,updated_at
I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id
However I don't know how to use "OR" operator while joining in laravel
here's my raw query that gives me the desired output.
SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id
Here's the laravel query that I tried
$get_messages = DB::table('messages')
->where('messages.sender_id',$user_id)
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->on('users.id','=','messages.receiver_id');
})
->orwhere('messages.sender_id',$partner_id)
->where('messages.receiver_id',$user_id)
->orwhere('messages.receiver_id',$partner_id)
->groupby('messages.id')
->get();
print_r($get_messages);
php mysql laravel laravel-5
add a comment |
I have a table called messages, and it's columns are
id, sender_id, message, receiver_id, created_at, updated_at
And the other table is called users, and it's columns are
id, firstname, lastname, email, password, created_at,updated_at
I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id
However I don't know how to use "OR" operator while joining in laravel
here's my raw query that gives me the desired output.
SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id
Here's the laravel query that I tried
$get_messages = DB::table('messages')
->where('messages.sender_id',$user_id)
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->on('users.id','=','messages.receiver_id');
})
->orwhere('messages.sender_id',$partner_id)
->where('messages.receiver_id',$user_id)
->orwhere('messages.receiver_id',$partner_id)
->groupby('messages.id')
->get();
print_r($get_messages);
php mysql laravel laravel-5
I have a table called messages, and it's columns are
id, sender_id, message, receiver_id, created_at, updated_at
And the other table is called users, and it's columns are
id, firstname, lastname, email, password, created_at,updated_at
I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id
However I don't know how to use "OR" operator while joining in laravel
here's my raw query that gives me the desired output.
SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id
Here's the laravel query that I tried
$get_messages = DB::table('messages')
->where('messages.sender_id',$user_id)
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->on('users.id','=','messages.receiver_id');
})
->orwhere('messages.sender_id',$partner_id)
->where('messages.receiver_id',$user_id)
->orwhere('messages.receiver_id',$partner_id)
->groupby('messages.id')
->get();
print_r($get_messages);
php mysql laravel laravel-5
php mysql laravel laravel-5
asked Mar 18 '16 at 2:03
KennethCKennethC
271215
271215
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Try this.
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->orOn('users.id','=','messages.receiver_id');
})
Thanks brother. It works fine.
– KennethC
Mar 18 '16 at 2:09
@KennethC, please accept the answer if it solved your case. Thanks :)
– aadarshsg
Mar 18 '16 at 6:34
done bro. thanks
– KennethC
Mar 19 '16 at 3:51
thanks Bro. its works really well
– thumber nirmal
Oct 1 '18 at 10:34
add a comment |
YOu can also use the direct query
$results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id") );
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36074866%2flaravel-joining-2-tables-with-or-operator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this.
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->orOn('users.id','=','messages.receiver_id');
})
Thanks brother. It works fine.
– KennethC
Mar 18 '16 at 2:09
@KennethC, please accept the answer if it solved your case. Thanks :)
– aadarshsg
Mar 18 '16 at 6:34
done bro. thanks
– KennethC
Mar 19 '16 at 3:51
thanks Bro. its works really well
– thumber nirmal
Oct 1 '18 at 10:34
add a comment |
Try this.
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->orOn('users.id','=','messages.receiver_id');
})
Thanks brother. It works fine.
– KennethC
Mar 18 '16 at 2:09
@KennethC, please accept the answer if it solved your case. Thanks :)
– aadarshsg
Mar 18 '16 at 6:34
done bro. thanks
– KennethC
Mar 19 '16 at 3:51
thanks Bro. its works really well
– thumber nirmal
Oct 1 '18 at 10:34
add a comment |
Try this.
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->orOn('users.id','=','messages.receiver_id');
})
Try this.
->leftjoin('users', function($join){
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->orOn('users.id','=','messages.receiver_id');
})
answered Mar 18 '16 at 2:07
aadarshsgaadarshsg
1,612923
1,612923
Thanks brother. It works fine.
– KennethC
Mar 18 '16 at 2:09
@KennethC, please accept the answer if it solved your case. Thanks :)
– aadarshsg
Mar 18 '16 at 6:34
done bro. thanks
– KennethC
Mar 19 '16 at 3:51
thanks Bro. its works really well
– thumber nirmal
Oct 1 '18 at 10:34
add a comment |
Thanks brother. It works fine.
– KennethC
Mar 18 '16 at 2:09
@KennethC, please accept the answer if it solved your case. Thanks :)
– aadarshsg
Mar 18 '16 at 6:34
done bro. thanks
– KennethC
Mar 19 '16 at 3:51
thanks Bro. its works really well
– thumber nirmal
Oct 1 '18 at 10:34
Thanks brother. It works fine.
– KennethC
Mar 18 '16 at 2:09
Thanks brother. It works fine.
– KennethC
Mar 18 '16 at 2:09
@KennethC, please accept the answer if it solved your case. Thanks :)
– aadarshsg
Mar 18 '16 at 6:34
@KennethC, please accept the answer if it solved your case. Thanks :)
– aadarshsg
Mar 18 '16 at 6:34
done bro. thanks
– KennethC
Mar 19 '16 at 3:51
done bro. thanks
– KennethC
Mar 19 '16 at 3:51
thanks Bro. its works really well
– thumber nirmal
Oct 1 '18 at 10:34
thanks Bro. its works really well
– thumber nirmal
Oct 1 '18 at 10:34
add a comment |
YOu can also use the direct query
$results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id") );
add a comment |
YOu can also use the direct query
$results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id") );
add a comment |
YOu can also use the direct query
$results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id") );
YOu can also use the direct query
$results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id
OR users.id = messages.receiver_id GROUP BY messages.id") );
answered Mar 18 '16 at 4:25
Haseena P AHaseena P A
4,45731025
4,45731025
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36074866%2flaravel-joining-2-tables-with-or-operator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown