How to write a custom function in a model?
There is a model data:
class Order extends Model
How to write a custom method inside the Order
class so that it can be called in constructor like this:
Order::myMethod()
Order->myMethod()
Where myMethod
is:
public function myMethod()
return DB::query(<SQL QUERY>);
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
laravel laravel-5
add a comment |
There is a model data:
class Order extends Model
How to write a custom method inside the Order
class so that it can be called in constructor like this:
Order::myMethod()
Order->myMethod()
Where myMethod
is:
public function myMethod()
return DB::query(<SQL QUERY>);
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
laravel laravel-5
2
Read the docs on query scopes.
– Devon
Nov 15 '18 at 1:21
This looks like an XY problem . Can you provide more infomation on exactly what you needmyMethod
to do?
– apokryfos
Nov 15 '18 at 1:26
What you need is Repository Design Pattern.
– Kenny
Nov 15 '18 at 2:09
Curious, why not use Eloquent?
– wheelmaker
Nov 15 '18 at 2:47
add a comment |
There is a model data:
class Order extends Model
How to write a custom method inside the Order
class so that it can be called in constructor like this:
Order::myMethod()
Order->myMethod()
Where myMethod
is:
public function myMethod()
return DB::query(<SQL QUERY>);
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
laravel laravel-5
There is a model data:
class Order extends Model
How to write a custom method inside the Order
class so that it can be called in constructor like this:
Order::myMethod()
Order->myMethod()
Where myMethod
is:
public function myMethod()
return DB::query(<SQL QUERY>);
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
laravel laravel-5
laravel laravel-5
asked Nov 15 '18 at 0:19
OPVOPV
1,68221338
1,68221338
2
Read the docs on query scopes.
– Devon
Nov 15 '18 at 1:21
This looks like an XY problem . Can you provide more infomation on exactly what you needmyMethod
to do?
– apokryfos
Nov 15 '18 at 1:26
What you need is Repository Design Pattern.
– Kenny
Nov 15 '18 at 2:09
Curious, why not use Eloquent?
– wheelmaker
Nov 15 '18 at 2:47
add a comment |
2
Read the docs on query scopes.
– Devon
Nov 15 '18 at 1:21
This looks like an XY problem . Can you provide more infomation on exactly what you needmyMethod
to do?
– apokryfos
Nov 15 '18 at 1:26
What you need is Repository Design Pattern.
– Kenny
Nov 15 '18 at 2:09
Curious, why not use Eloquent?
– wheelmaker
Nov 15 '18 at 2:47
2
2
Read the docs on query scopes.
– Devon
Nov 15 '18 at 1:21
Read the docs on query scopes.
– Devon
Nov 15 '18 at 1:21
This looks like an XY problem . Can you provide more infomation on exactly what you need
myMethod
to do?– apokryfos
Nov 15 '18 at 1:26
This looks like an XY problem . Can you provide more infomation on exactly what you need
myMethod
to do?– apokryfos
Nov 15 '18 at 1:26
What you need is Repository Design Pattern.
– Kenny
Nov 15 '18 at 2:09
What you need is Repository Design Pattern.
– Kenny
Nov 15 '18 at 2:09
Curious, why not use Eloquent?
– wheelmaker
Nov 15 '18 at 2:47
Curious, why not use Eloquent?
– wheelmaker
Nov 15 '18 at 2:47
add a comment |
5 Answers
5
active
oldest
votes
Guess you are asking about the static functions:
class Order extends Model
public static function myMethod()
and you can call it anywhere like
Order::myMethod();
add a comment |
Rather create a custom function in Model, You can use traits to achieve the desired output.
Please follow either steps:-
- https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3
- https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5
Welcome ........
– Mayank Majithya
Nov 16 '18 at 5:39
add a comment |
I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this
class ABC extends Model
//here is your fillable array;
public function abc()
//Here is your Eloquent statement or SQL query;
just call this abc() method inside the controller like this
use ABC;
class AbcController extends Controller
private $_abc; // it is private variable
// this is constructor
public function __construct(ABC $abc)
$this->_abc= $abc;
public function abcMethod()
$this->_abc->abc();
Thanks
add a comment |
You can achieve the desired behavior using magic methods __call and __callStatic
if your real method is static you can use __call()
to intercept all "non static" calls and use it to call the static and use __callStatic
to forward the calls to a new instance to that class .
Your methods should be always static because if a non static method exists and you are calling it statically php raises an error
Non-static method Foo::myMethod() should not be called statically
No problem if your method is static
class Order extends Model
public static function myMethod()
return static::query()->where(...)->get(); // example
public function __call($name, $arguments)
return forward_static_call_array([__CLASS__, $name], $arguments);
public static function __callStatic($name, $arguments)
return call_user_func_array([app(__CLASS__), $name], $arguments);
(new Order())->myMethod();
Order::myMethod();
add a comment |
I don't believe I'm understanding your intention. You've stated:
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
Why does the Order->myMethod()
need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.
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%2f53310720%2fhow-to-write-a-custom-function-in-a-model%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Guess you are asking about the static functions:
class Order extends Model
public static function myMethod()
and you can call it anywhere like
Order::myMethod();
add a comment |
Guess you are asking about the static functions:
class Order extends Model
public static function myMethod()
and you can call it anywhere like
Order::myMethod();
add a comment |
Guess you are asking about the static functions:
class Order extends Model
public static function myMethod()
and you can call it anywhere like
Order::myMethod();
Guess you are asking about the static functions:
class Order extends Model
public static function myMethod()
and you can call it anywhere like
Order::myMethod();
answered Nov 15 '18 at 3:54
Baraa Al-TabbaaBaraa Al-Tabbaa
66558
66558
add a comment |
add a comment |
Rather create a custom function in Model, You can use traits to achieve the desired output.
Please follow either steps:-
- https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3
- https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5
Welcome ........
– Mayank Majithya
Nov 16 '18 at 5:39
add a comment |
Rather create a custom function in Model, You can use traits to achieve the desired output.
Please follow either steps:-
- https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3
- https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5
Welcome ........
– Mayank Majithya
Nov 16 '18 at 5:39
add a comment |
Rather create a custom function in Model, You can use traits to achieve the desired output.
Please follow either steps:-
- https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3
- https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5
Rather create a custom function in Model, You can use traits to achieve the desired output.
Please follow either steps:-
- https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3
- https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5
answered Nov 15 '18 at 5:42
Mayank MajithyaMayank Majithya
1,064515
1,064515
Welcome ........
– Mayank Majithya
Nov 16 '18 at 5:39
add a comment |
Welcome ........
– Mayank Majithya
Nov 16 '18 at 5:39
Welcome ........
– Mayank Majithya
Nov 16 '18 at 5:39
Welcome ........
– Mayank Majithya
Nov 16 '18 at 5:39
add a comment |
I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this
class ABC extends Model
//here is your fillable array;
public function abc()
//Here is your Eloquent statement or SQL query;
just call this abc() method inside the controller like this
use ABC;
class AbcController extends Controller
private $_abc; // it is private variable
// this is constructor
public function __construct(ABC $abc)
$this->_abc= $abc;
public function abcMethod()
$this->_abc->abc();
Thanks
add a comment |
I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this
class ABC extends Model
//here is your fillable array;
public function abc()
//Here is your Eloquent statement or SQL query;
just call this abc() method inside the controller like this
use ABC;
class AbcController extends Controller
private $_abc; // it is private variable
// this is constructor
public function __construct(ABC $abc)
$this->_abc= $abc;
public function abcMethod()
$this->_abc->abc();
Thanks
add a comment |
I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this
class ABC extends Model
//here is your fillable array;
public function abc()
//Here is your Eloquent statement or SQL query;
just call this abc() method inside the controller like this
use ABC;
class AbcController extends Controller
private $_abc; // it is private variable
// this is constructor
public function __construct(ABC $abc)
$this->_abc= $abc;
public function abcMethod()
$this->_abc->abc();
Thanks
I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this
class ABC extends Model
//here is your fillable array;
public function abc()
//Here is your Eloquent statement or SQL query;
just call this abc() method inside the controller like this
use ABC;
class AbcController extends Controller
private $_abc; // it is private variable
// this is constructor
public function __construct(ABC $abc)
$this->_abc= $abc;
public function abcMethod()
$this->_abc->abc();
Thanks
answered Nov 15 '18 at 8:28
Zeeshan TanveerZeeshan Tanveer
313
313
add a comment |
add a comment |
You can achieve the desired behavior using magic methods __call and __callStatic
if your real method is static you can use __call()
to intercept all "non static" calls and use it to call the static and use __callStatic
to forward the calls to a new instance to that class .
Your methods should be always static because if a non static method exists and you are calling it statically php raises an error
Non-static method Foo::myMethod() should not be called statically
No problem if your method is static
class Order extends Model
public static function myMethod()
return static::query()->where(...)->get(); // example
public function __call($name, $arguments)
return forward_static_call_array([__CLASS__, $name], $arguments);
public static function __callStatic($name, $arguments)
return call_user_func_array([app(__CLASS__), $name], $arguments);
(new Order())->myMethod();
Order::myMethod();
add a comment |
You can achieve the desired behavior using magic methods __call and __callStatic
if your real method is static you can use __call()
to intercept all "non static" calls and use it to call the static and use __callStatic
to forward the calls to a new instance to that class .
Your methods should be always static because if a non static method exists and you are calling it statically php raises an error
Non-static method Foo::myMethod() should not be called statically
No problem if your method is static
class Order extends Model
public static function myMethod()
return static::query()->where(...)->get(); // example
public function __call($name, $arguments)
return forward_static_call_array([__CLASS__, $name], $arguments);
public static function __callStatic($name, $arguments)
return call_user_func_array([app(__CLASS__), $name], $arguments);
(new Order())->myMethod();
Order::myMethod();
add a comment |
You can achieve the desired behavior using magic methods __call and __callStatic
if your real method is static you can use __call()
to intercept all "non static" calls and use it to call the static and use __callStatic
to forward the calls to a new instance to that class .
Your methods should be always static because if a non static method exists and you are calling it statically php raises an error
Non-static method Foo::myMethod() should not be called statically
No problem if your method is static
class Order extends Model
public static function myMethod()
return static::query()->where(...)->get(); // example
public function __call($name, $arguments)
return forward_static_call_array([__CLASS__, $name], $arguments);
public static function __callStatic($name, $arguments)
return call_user_func_array([app(__CLASS__), $name], $arguments);
(new Order())->myMethod();
Order::myMethod();
You can achieve the desired behavior using magic methods __call and __callStatic
if your real method is static you can use __call()
to intercept all "non static" calls and use it to call the static and use __callStatic
to forward the calls to a new instance to that class .
Your methods should be always static because if a non static method exists and you are calling it statically php raises an error
Non-static method Foo::myMethod() should not be called statically
No problem if your method is static
class Order extends Model
public static function myMethod()
return static::query()->where(...)->get(); // example
public function __call($name, $arguments)
return forward_static_call_array([__CLASS__, $name], $arguments);
public static function __callStatic($name, $arguments)
return call_user_func_array([app(__CLASS__), $name], $arguments);
(new Order())->myMethod();
Order::myMethod();
answered Nov 15 '18 at 9:25
simonecoscisimonecosci
82658
82658
add a comment |
add a comment |
I don't believe I'm understanding your intention. You've stated:
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
Why does the Order->myMethod()
need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.
add a comment |
I don't believe I'm understanding your intention. You've stated:
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
Why does the Order->myMethod()
need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.
add a comment |
I don't believe I'm understanding your intention. You've stated:
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
Why does the Order->myMethod()
need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.
I don't believe I'm understanding your intention. You've stated:
Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.
Why does the Order->myMethod()
need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.
answered Nov 15 '18 at 1:13
parseMaestro63parseMaestro63
816
816
add a comment |
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%2f53310720%2fhow-to-write-a-custom-function-in-a-model%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
2
Read the docs on query scopes.
– Devon
Nov 15 '18 at 1:21
This looks like an XY problem . Can you provide more infomation on exactly what you need
myMethod
to do?– apokryfos
Nov 15 '18 at 1:26
What you need is Repository Design Pattern.
– Kenny
Nov 15 '18 at 2:09
Curious, why not use Eloquent?
– wheelmaker
Nov 15 '18 at 2:47