Pass chain function as paramter as parameter in php











up vote
3
down vote

favorite












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?
    – emaillenin
    Nov 22 at 4:25










  • Ii updated the question
    – ashok poudel
    Nov 22 at 4:31










  • you have already answered your question
    – emaillenin
    Nov 22 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 at 5:02










  • Or you can explain the use case and someone can provide a better solution.
    – Ijas Ameenudeen
    Nov 22 at 5:05















up vote
3
down vote

favorite












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?
    – emaillenin
    Nov 22 at 4:25










  • Ii updated the question
    – ashok poudel
    Nov 22 at 4:31










  • you have already answered your question
    – emaillenin
    Nov 22 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 at 5:02










  • Or you can explain the use case and someone can provide a better solution.
    – Ijas Ameenudeen
    Nov 22 at 5:05













up vote
3
down vote

favorite









up vote
3
down vote

favorite











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 at 10:45

























asked Nov 22 at 4:24









ashok poudel

147112




147112








  • 1




    what is the value of $join_as_parameter? is it a table name?
    – emaillenin
    Nov 22 at 4:25










  • Ii updated the question
    – ashok poudel
    Nov 22 at 4:31










  • you have already answered your question
    – emaillenin
    Nov 22 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 at 5:02










  • Or you can explain the use case and someone can provide a better solution.
    – Ijas Ameenudeen
    Nov 22 at 5:05














  • 1




    what is the value of $join_as_parameter? is it a table name?
    – emaillenin
    Nov 22 at 4:25










  • Ii updated the question
    – ashok poudel
    Nov 22 at 4:31










  • you have already answered your question
    – emaillenin
    Nov 22 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 at 5:02










  • Or you can explain the use case and someone can provide a better solution.
    – Ijas Ameenudeen
    Nov 22 at 5:05








1




1




what is the value of $join_as_parameter? is it a table name?
– emaillenin
Nov 22 at 4:25




what is the value of $join_as_parameter? is it a table name?
– emaillenin
Nov 22 at 4:25












Ii updated the question
– ashok poudel
Nov 22 at 4:31




Ii updated the question
– ashok poudel
Nov 22 at 4:31












you have already answered your question
– emaillenin
Nov 22 at 4:34




you have already answered your question
– emaillenin
Nov 22 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 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 at 5:02












Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 at 5:05




Or you can explain the use case and someone can provide a better solution.
– Ijas Ameenudeen
Nov 22 at 5:05












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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 at 10:10










  • Yes. Can pass eloquent builder as well
    – Ijas Ameenudeen
    Nov 22 at 10:11










  • Then I get this error
    – ashok poudel
    Nov 22 at 10:13










  • Indirect modification of overloaded property AppModelProduct::$joins has no effect
    – ashok poudel
    Nov 22 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 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',
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








up vote
2
down vote



accepted










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 at 10:10










  • Yes. Can pass eloquent builder as well
    – Ijas Ameenudeen
    Nov 22 at 10:11










  • Then I get this error
    – ashok poudel
    Nov 22 at 10:13










  • Indirect modification of overloaded property AppModelProduct::$joins has no effect
    – ashok poudel
    Nov 22 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 at 10:31















up vote
2
down vote



accepted










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 at 10:10










  • Yes. Can pass eloquent builder as well
    – Ijas Ameenudeen
    Nov 22 at 10:11










  • Then I get this error
    – ashok poudel
    Nov 22 at 10:13










  • Indirect modification of overloaded property AppModelProduct::$joins has no effect
    – ashok poudel
    Nov 22 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 at 10:31













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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 at 16:36

























answered Nov 22 at 5:59









Ijas Ameenudeen

6,33212442




6,33212442












  • Can't this be achieved using Model instead of DB::query()
    – ashok poudel
    Nov 22 at 10:10










  • Yes. Can pass eloquent builder as well
    – Ijas Ameenudeen
    Nov 22 at 10:11










  • Then I get this error
    – ashok poudel
    Nov 22 at 10:13










  • Indirect modification of overloaded property AppModelProduct::$joins has no effect
    – ashok poudel
    Nov 22 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 at 10:31


















  • Can't this be achieved using Model instead of DB::query()
    – ashok poudel
    Nov 22 at 10:10










  • Yes. Can pass eloquent builder as well
    – Ijas Ameenudeen
    Nov 22 at 10:11










  • Then I get this error
    – ashok poudel
    Nov 22 at 10:13










  • Indirect modification of overloaded property AppModelProduct::$joins has no effect
    – ashok poudel
    Nov 22 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 at 10:31
















Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 at 10:10




Can't this be achieved using Model instead of DB::query()
– ashok poudel
Nov 22 at 10:10












Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 at 10:11




Yes. Can pass eloquent builder as well
– Ijas Ameenudeen
Nov 22 at 10:11












Then I get this error
– ashok poudel
Nov 22 at 10:13




Then I get this error
– ashok poudel
Nov 22 at 10:13












Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 at 10:13




Indirect modification of overloaded property AppModelProduct::$joins has no effect
– ashok poudel
Nov 22 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 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 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)