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;
}







3















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.










share|improve this question




















  • 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 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


















3















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.










share|improve this question




















  • 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 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














3












3








3








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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 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








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












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer


























  • 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












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
});


}
});














draft saved

draft discarded


















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









2














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.






share|improve this answer


























  • 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
















2














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.






share|improve this answer


























  • 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














2












2








2







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)