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.
php laravel method-chaining
|
show 3 more comments
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.
php laravel method-chaining
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 wholejoin('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
|
show 3 more comments
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.
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 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 wholejoin('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
|
show 3 more comments
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 wholejoin('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
|
show 3 more comments
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.
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
|
show 10 more comments
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.
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
|
show 10 more comments
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.
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
|
show 10 more comments
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.
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 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
|
show 10 more comments
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
|
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.
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.
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?– 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 wholejoin('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