php - Laravel method chaining a query through a function -
Is it possible to chain methods as conditional in PHP / Laravel, or passed to the method chain through function for?
I want to control a query in Laravel's query builder by setting a base query first, then sending it to a function to keep the other methods chained. As a quick example:
$ query = model: whereFoo ('bar'); If ($ needFoo) {$ query = $ query- & gt; Is ('foo'); } $ Query = queryMethod ($ query); Function query method ($ query) {return $ query- & gt; Where ('some', '<', 10); } $ Items = $ query- & gt; get receive (); $ query = Model: whereFoo ('bar'); and $ items = $ query- & gt; Seems to ignore everything between -
What a difference does not make that $ query . Is it possible to get advice / advice?
Edit: I have accepted the answer given below because it answers the questions I have asked my problem was actually unrelated if it helps someone : I was called in a rogue or where () base query, which, along with mandatory results, regardless of other chain methods. It is necessary that nesting is needed in a modern way.
What you should do, but you can simplify it:
< Code> $ query = Model :: whereFoo ('bar'); If ($ surefu) $ query- & gt; Is ('foo'); QueryMethod ($ query); Function Query Method ($ query) {$ query- & gt; Where ('some', '<', 10); } $ Items = $ query- & gt; get receive (); Since the object in PHP is passed from context, you will always behave with that same example.
Comments
Post a Comment