Pass chain function as paramter as parameter in php
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a function. It has method chaining that needs to be performed.
public function someFunction()
{
$query=$this->model;
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
It was working fine but I need a slight modification in that function what I want is I want to pass a join in that function for method chaining.
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
//Join should be executed here as a parameter in method chaning .
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
So that final function execution will be like this
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
->join('table','sometable.id', '=', 'other_table')
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
Is there any way to do this? Any help would be appreciated. Thanks.
php laravel method-chaining
|
show 3 more comments
I have a function. It has method chaining that needs to be performed.
public function someFunction()
{
$query=$this->model;
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
It was working fine but I need a slight modification in that function what I want is I want to pass a join in that function for method chaining.
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
//Join should be executed here as a parameter in method chaning .
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
So that final function execution will be like this
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
->join('table','sometable.id', '=', 'other_table')
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
Is there any way to do this? Any help would be appreciated. Thanks.
php laravel method-chaining
1
what is the value of$join_as_parameter
? is it a table name?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:25
Ii updated the question
– ashok poudel
Nov 22 '18 at 4:31
you have already answered your question
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:34
is$join_as_parameter
is set of prams as'table', 'sometable.id', '=', 'other_table'
or the wholejoin('table','sometable.id', '=', 'other_table')
functions ?
– Ijas Ameenudeen
Nov 22 '18 at 5:02
Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 '18 at 5:05
|
show 3 more comments
I have a function. It has method chaining that needs to be performed.
public function someFunction()
{
$query=$this->model;
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
It was working fine but I need a slight modification in that function what I want is I want to pass a join in that function for method chaining.
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
//Join should be executed here as a parameter in method chaning .
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
So that final function execution will be like this
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
->join('table','sometable.id', '=', 'other_table')
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
Is there any way to do this? Any help would be appreciated. Thanks.
php laravel method-chaining
I have a function. It has method chaining that needs to be performed.
public function someFunction()
{
$query=$this->model;
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
It was working fine but I need a slight modification in that function what I want is I want to pass a join in that function for method chaining.
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
//Join should be executed here as a parameter in method chaning .
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
So that final function execution will be like this
public function someFunction($join_as_parameter)
{
$query=$this->model;
$query->select($columns)
->join('table','sometable.id', '=', 'other_table')
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
Is there any way to do this? Any help would be appreciated. Thanks.
php laravel method-chaining
php laravel method-chaining
edited Nov 22 '18 at 10:45
ashok poudel
asked Nov 22 '18 at 4:24
ashok poudelashok poudel
178114
178114
1
what is the value of$join_as_parameter
? is it a table name?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:25
Ii updated the question
– ashok poudel
Nov 22 '18 at 4:31
you have already answered your question
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:34
is$join_as_parameter
is set of prams as'table', 'sometable.id', '=', 'other_table'
or the wholejoin('table','sometable.id', '=', 'other_table')
functions ?
– Ijas Ameenudeen
Nov 22 '18 at 5:02
Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 '18 at 5:05
|
show 3 more comments
1
what is the value of$join_as_parameter
? is it a table name?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:25
Ii updated the question
– ashok poudel
Nov 22 '18 at 4:31
you have already answered your question
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:34
is$join_as_parameter
is set of prams as'table', 'sometable.id', '=', 'other_table'
or the wholejoin('table','sometable.id', '=', 'other_table')
functions ?
– Ijas Ameenudeen
Nov 22 '18 at 5:02
Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 '18 at 5:05
1
1
what is the value of
$join_as_parameter
? is it a table name?– Lenin Raj Rajasekaran
Nov 22 '18 at 4:25
what is the value of
$join_as_parameter
? is it a table name?– Lenin Raj Rajasekaran
Nov 22 '18 at 4:25
Ii updated the question
– ashok poudel
Nov 22 '18 at 4:31
Ii updated the question
– ashok poudel
Nov 22 '18 at 4:31
you have already answered your question
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:34
you have already answered your question
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:34
is
$join_as_parameter
is set of prams as 'table', 'sometable.id', '=', 'other_table'
or the whole join('table','sometable.id', '=', 'other_table')
functions ?– Ijas Ameenudeen
Nov 22 '18 at 5:02
is
$join_as_parameter
is set of prams as 'table', 'sometable.id', '=', 'other_table'
or the whole join('table','sometable.id', '=', 'other_table')
functions ?– Ijas Ameenudeen
Nov 22 '18 at 5:02
Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 '18 at 5:05
Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 '18 at 5:05
|
show 3 more comments
1 Answer
1
active
oldest
votes
This way you can achieve what you need.
use DB;
use Closure;
use IlluminateDatabaseQueryJoinClause;
public function someFunction(Closure $join_clauser)
{
//create Query Builder object
$query = DB::query();
//Add the `$join` object to the table joins for this query
$join_as_parameter = call_user_func($join_closure, $query);
$query->joins = array_merge((array) $query->joins, [$join_as_parameter]);
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
//create Query Builder object
$query = DB::query();
And execute the functions as,
someFunction(function($query){
// return JoinClause object with joining conditions
return (new JoinClause($query, 'inner', 'table'))
->on('table.id', '=', 'othe_table.table_id');
});
Furthermore, you can modify this to pass array of joins
to add multiple joins
your the query.
To use this with eloquent models, replace
$query = DB::query();
with
$query = Model::query()->getQuery();
NOTE : ->getQuery()
is used to retrieve the QueryBuilder
object since JoinClause
expects it as the first param.
Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 '18 at 10:10
Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 '18 at 10:11
Then I get this error
– ashok poudel
Nov 22 '18 at 10:13
Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 '18 at 10:13
I updated the answer with the Eloquent way. But, I don't get the error. What's your Laravel & PHP version ?
– Ijas Ameenudeen
Nov 22 '18 at 10:31
|
show 10 more comments
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%2f53423873%2fpass-chain-function-as-paramter-as-parameter-in-php%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
This way you can achieve what you need.
use DB;
use Closure;
use IlluminateDatabaseQueryJoinClause;
public function someFunction(Closure $join_clauser)
{
//create Query Builder object
$query = DB::query();
//Add the `$join` object to the table joins for this query
$join_as_parameter = call_user_func($join_closure, $query);
$query->joins = array_merge((array) $query->joins, [$join_as_parameter]);
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
//create Query Builder object
$query = DB::query();
And execute the functions as,
someFunction(function($query){
// return JoinClause object with joining conditions
return (new JoinClause($query, 'inner', 'table'))
->on('table.id', '=', 'othe_table.table_id');
});
Furthermore, you can modify this to pass array of joins
to add multiple joins
your the query.
To use this with eloquent models, replace
$query = DB::query();
with
$query = Model::query()->getQuery();
NOTE : ->getQuery()
is used to retrieve the QueryBuilder
object since JoinClause
expects it as the first param.
Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 '18 at 10:10
Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 '18 at 10:11
Then I get this error
– ashok poudel
Nov 22 '18 at 10:13
Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 '18 at 10:13
I updated the answer with the Eloquent way. But, I don't get the error. What's your Laravel & PHP version ?
– Ijas Ameenudeen
Nov 22 '18 at 10:31
|
show 10 more comments
This way you can achieve what you need.
use DB;
use Closure;
use IlluminateDatabaseQueryJoinClause;
public function someFunction(Closure $join_clauser)
{
//create Query Builder object
$query = DB::query();
//Add the `$join` object to the table joins for this query
$join_as_parameter = call_user_func($join_closure, $query);
$query->joins = array_merge((array) $query->joins, [$join_as_parameter]);
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
//create Query Builder object
$query = DB::query();
And execute the functions as,
someFunction(function($query){
// return JoinClause object with joining conditions
return (new JoinClause($query, 'inner', 'table'))
->on('table.id', '=', 'othe_table.table_id');
});
Furthermore, you can modify this to pass array of joins
to add multiple joins
your the query.
To use this with eloquent models, replace
$query = DB::query();
with
$query = Model::query()->getQuery();
NOTE : ->getQuery()
is used to retrieve the QueryBuilder
object since JoinClause
expects it as the first param.
Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 '18 at 10:10
Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 '18 at 10:11
Then I get this error
– ashok poudel
Nov 22 '18 at 10:13
Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 '18 at 10:13
I updated the answer with the Eloquent way. But, I don't get the error. What's your Laravel & PHP version ?
– Ijas Ameenudeen
Nov 22 '18 at 10:31
|
show 10 more comments
This way you can achieve what you need.
use DB;
use Closure;
use IlluminateDatabaseQueryJoinClause;
public function someFunction(Closure $join_clauser)
{
//create Query Builder object
$query = DB::query();
//Add the `$join` object to the table joins for this query
$join_as_parameter = call_user_func($join_closure, $query);
$query->joins = array_merge((array) $query->joins, [$join_as_parameter]);
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
//create Query Builder object
$query = DB::query();
And execute the functions as,
someFunction(function($query){
// return JoinClause object with joining conditions
return (new JoinClause($query, 'inner', 'table'))
->on('table.id', '=', 'othe_table.table_id');
});
Furthermore, you can modify this to pass array of joins
to add multiple joins
your the query.
To use this with eloquent models, replace
$query = DB::query();
with
$query = Model::query()->getQuery();
NOTE : ->getQuery()
is used to retrieve the QueryBuilder
object since JoinClause
expects it as the first param.
This way you can achieve what you need.
use DB;
use Closure;
use IlluminateDatabaseQueryJoinClause;
public function someFunction(Closure $join_clauser)
{
//create Query Builder object
$query = DB::query();
//Add the `$join` object to the table joins for this query
$join_as_parameter = call_user_func($join_closure, $query);
$query->joins = array_merge((array) $query->joins, [$join_as_parameter]);
$query->select($columns)
->skip($request->get('start') * $request->get('length'))
->take($request->get('length'))
->orderBy(
$request->get('sort_column'),
$request->get('sort_direction')
)
->get();
//Some other task
}
//create Query Builder object
$query = DB::query();
And execute the functions as,
someFunction(function($query){
// return JoinClause object with joining conditions
return (new JoinClause($query, 'inner', 'table'))
->on('table.id', '=', 'othe_table.table_id');
});
Furthermore, you can modify this to pass array of joins
to add multiple joins
your the query.
To use this with eloquent models, replace
$query = DB::query();
with
$query = Model::query()->getQuery();
NOTE : ->getQuery()
is used to retrieve the QueryBuilder
object since JoinClause
expects it as the first param.
edited Nov 22 '18 at 16:36
answered Nov 22 '18 at 5:59
Ijas AmeenudeenIjas Ameenudeen
6,95733048
6,95733048
Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 '18 at 10:10
Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 '18 at 10:11
Then I get this error
– ashok poudel
Nov 22 '18 at 10:13
Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 '18 at 10:13
I updated the answer with the Eloquent way. But, I don't get the error. What's your Laravel & PHP version ?
– Ijas Ameenudeen
Nov 22 '18 at 10:31
|
show 10 more comments
Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 '18 at 10:10
Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 '18 at 10:11
Then I get this error
– ashok poudel
Nov 22 '18 at 10:13
Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 '18 at 10:13
I updated the answer with the Eloquent way. But, I don't get the error. What's your Laravel & PHP version ?
– Ijas Ameenudeen
Nov 22 '18 at 10:31
Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 '18 at 10:10
Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 '18 at 10:10
Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 '18 at 10:11
Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 '18 at 10:11
Then I get this error
– ashok poudel
Nov 22 '18 at 10:13
Then I get this error
– ashok poudel
Nov 22 '18 at 10:13
Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 '18 at 10:13
Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 '18 at 10:13
I updated the answer with the Eloquent way. But, I don't get the error. What's your Laravel & PHP version ?
– Ijas Ameenudeen
Nov 22 '18 at 10:31
I updated the answer with the Eloquent way. But, I don't get the error. What's your Laravel & PHP version ?
– Ijas Ameenudeen
Nov 22 '18 at 10:31
|
show 10 more comments
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%2f53423873%2fpass-chain-function-as-paramter-as-parameter-in-php%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
1
what is the value of
$join_as_parameter
? is it a table name?– Lenin Raj Rajasekaran
Nov 22 '18 at 4:25
Ii updated the question
– ashok poudel
Nov 22 '18 at 4:31
you have already answered your question
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:34
is
$join_as_parameter
is set of prams as'table', 'sometable.id', '=', 'other_table'
or the wholejoin('table','sometable.id', '=', 'other_table')
functions ?– Ijas Ameenudeen
Nov 22 '18 at 5:02
Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 '18 at 5:05