How to use query builder to retrieve array values from different tables?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Following is the code snippet that I have used in my controller:
public function index(Request $request)
{
$query = DB::table('project_files')->select('project_id')->get()->toArray();
dd($query);
return view ( 'filesync/listfiles' )->with ( $query);
}
So when I try passing the values into the view, I get an array which consists of the project_id from the project_files table. What I would want to do now is to retrieve the project_names of these project_ids, stored in the projects table.I need to use the project_id for the same , but am unable to understand how to relate these using query builder.Any help would be appreciated, meaning how do I use the array values in the query retrieving project names from projects table.
php laravel-5
add a comment |
Following is the code snippet that I have used in my controller:
public function index(Request $request)
{
$query = DB::table('project_files')->select('project_id')->get()->toArray();
dd($query);
return view ( 'filesync/listfiles' )->with ( $query);
}
So when I try passing the values into the view, I get an array which consists of the project_id from the project_files table. What I would want to do now is to retrieve the project_names of these project_ids, stored in the projects table.I need to use the project_id for the same , but am unable to understand how to relate these using query builder.Any help would be appreciated, meaning how do I use the array values in the query retrieving project names from projects table.
php laravel-5
add a comment |
Following is the code snippet that I have used in my controller:
public function index(Request $request)
{
$query = DB::table('project_files')->select('project_id')->get()->toArray();
dd($query);
return view ( 'filesync/listfiles' )->with ( $query);
}
So when I try passing the values into the view, I get an array which consists of the project_id from the project_files table. What I would want to do now is to retrieve the project_names of these project_ids, stored in the projects table.I need to use the project_id for the same , but am unable to understand how to relate these using query builder.Any help would be appreciated, meaning how do I use the array values in the query retrieving project names from projects table.
php laravel-5
Following is the code snippet that I have used in my controller:
public function index(Request $request)
{
$query = DB::table('project_files')->select('project_id')->get()->toArray();
dd($query);
return view ( 'filesync/listfiles' )->with ( $query);
}
So when I try passing the values into the view, I get an array which consists of the project_id from the project_files table. What I would want to do now is to retrieve the project_names of these project_ids, stored in the projects table.I need to use the project_id for the same , but am unable to understand how to relate these using query builder.Any help would be appreciated, meaning how do I use the array values in the query retrieving project names from projects table.
php laravel-5
php laravel-5
asked Nov 26 '18 at 16:55
MVSMVS
6528
6528
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can setup relationships which is the ideal way or you can use a leftJoin
with your query:
DB::table('project_files')->select('project_files.project_id')->leftJoin('projects', 'project_files.project_id', '=', 'projects.project_id')->get()->toArray();
and replace the column name respectively to the correct values.
Another suggestion I would make is to actually make use of the M in MVC by defining models for your table instead of using the db facade.
Note: Don't forget to include the columns you wish to select within your select
function.
Hey, thanks for the reply.Do you think that setting up relationships could be a better approach to this, I find query builder messy, but since the number of tables to be dealt with increases, it somewhat ,makes the job easier, I don't happen to know much about eloquent though.Should I be considering that as well?
– MVS
Nov 26 '18 at 17:03
Eloquent, IMO, pretty much makes Laravel. So if you aren't using it when it is at your disposal it is quite a shame, as for the relationships, if you define them then it would save you from having to useleftJoin
and you can just use->with('relationship_name')
.
– Script47
Nov 26 '18 at 17:04
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%2f53485735%2fhow-to-use-query-builder-to-retrieve-array-values-from-different-tables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can setup relationships which is the ideal way or you can use a leftJoin
with your query:
DB::table('project_files')->select('project_files.project_id')->leftJoin('projects', 'project_files.project_id', '=', 'projects.project_id')->get()->toArray();
and replace the column name respectively to the correct values.
Another suggestion I would make is to actually make use of the M in MVC by defining models for your table instead of using the db facade.
Note: Don't forget to include the columns you wish to select within your select
function.
Hey, thanks for the reply.Do you think that setting up relationships could be a better approach to this, I find query builder messy, but since the number of tables to be dealt with increases, it somewhat ,makes the job easier, I don't happen to know much about eloquent though.Should I be considering that as well?
– MVS
Nov 26 '18 at 17:03
Eloquent, IMO, pretty much makes Laravel. So if you aren't using it when it is at your disposal it is quite a shame, as for the relationships, if you define them then it would save you from having to useleftJoin
and you can just use->with('relationship_name')
.
– Script47
Nov 26 '18 at 17:04
add a comment |
You can setup relationships which is the ideal way or you can use a leftJoin
with your query:
DB::table('project_files')->select('project_files.project_id')->leftJoin('projects', 'project_files.project_id', '=', 'projects.project_id')->get()->toArray();
and replace the column name respectively to the correct values.
Another suggestion I would make is to actually make use of the M in MVC by defining models for your table instead of using the db facade.
Note: Don't forget to include the columns you wish to select within your select
function.
Hey, thanks for the reply.Do you think that setting up relationships could be a better approach to this, I find query builder messy, but since the number of tables to be dealt with increases, it somewhat ,makes the job easier, I don't happen to know much about eloquent though.Should I be considering that as well?
– MVS
Nov 26 '18 at 17:03
Eloquent, IMO, pretty much makes Laravel. So if you aren't using it when it is at your disposal it is quite a shame, as for the relationships, if you define them then it would save you from having to useleftJoin
and you can just use->with('relationship_name')
.
– Script47
Nov 26 '18 at 17:04
add a comment |
You can setup relationships which is the ideal way or you can use a leftJoin
with your query:
DB::table('project_files')->select('project_files.project_id')->leftJoin('projects', 'project_files.project_id', '=', 'projects.project_id')->get()->toArray();
and replace the column name respectively to the correct values.
Another suggestion I would make is to actually make use of the M in MVC by defining models for your table instead of using the db facade.
Note: Don't forget to include the columns you wish to select within your select
function.
You can setup relationships which is the ideal way or you can use a leftJoin
with your query:
DB::table('project_files')->select('project_files.project_id')->leftJoin('projects', 'project_files.project_id', '=', 'projects.project_id')->get()->toArray();
and replace the column name respectively to the correct values.
Another suggestion I would make is to actually make use of the M in MVC by defining models for your table instead of using the db facade.
Note: Don't forget to include the columns you wish to select within your select
function.
answered Nov 26 '18 at 16:58
Script47Script47
10k42248
10k42248
Hey, thanks for the reply.Do you think that setting up relationships could be a better approach to this, I find query builder messy, but since the number of tables to be dealt with increases, it somewhat ,makes the job easier, I don't happen to know much about eloquent though.Should I be considering that as well?
– MVS
Nov 26 '18 at 17:03
Eloquent, IMO, pretty much makes Laravel. So if you aren't using it when it is at your disposal it is quite a shame, as for the relationships, if you define them then it would save you from having to useleftJoin
and you can just use->with('relationship_name')
.
– Script47
Nov 26 '18 at 17:04
add a comment |
Hey, thanks for the reply.Do you think that setting up relationships could be a better approach to this, I find query builder messy, but since the number of tables to be dealt with increases, it somewhat ,makes the job easier, I don't happen to know much about eloquent though.Should I be considering that as well?
– MVS
Nov 26 '18 at 17:03
Eloquent, IMO, pretty much makes Laravel. So if you aren't using it when it is at your disposal it is quite a shame, as for the relationships, if you define them then it would save you from having to useleftJoin
and you can just use->with('relationship_name')
.
– Script47
Nov 26 '18 at 17:04
Hey, thanks for the reply.Do you think that setting up relationships could be a better approach to this, I find query builder messy, but since the number of tables to be dealt with increases, it somewhat ,makes the job easier, I don't happen to know much about eloquent though.Should I be considering that as well?
– MVS
Nov 26 '18 at 17:03
Hey, thanks for the reply.Do you think that setting up relationships could be a better approach to this, I find query builder messy, but since the number of tables to be dealt with increases, it somewhat ,makes the job easier, I don't happen to know much about eloquent though.Should I be considering that as well?
– MVS
Nov 26 '18 at 17:03
Eloquent, IMO, pretty much makes Laravel. So if you aren't using it when it is at your disposal it is quite a shame, as for the relationships, if you define them then it would save you from having to use
leftJoin
and you can just use ->with('relationship_name')
.– Script47
Nov 26 '18 at 17:04
Eloquent, IMO, pretty much makes Laravel. So if you aren't using it when it is at your disposal it is quite a shame, as for the relationships, if you define them then it would save you from having to use
leftJoin
and you can just use ->with('relationship_name')
.– Script47
Nov 26 '18 at 17:04
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%2f53485735%2fhow-to-use-query-builder-to-retrieve-array-values-from-different-tables%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