Wednesday, 15 May 2013

php - Using eloquent select related entries after restricting parent entry in Laravel -


If I have a laurel class, then the category says, which has 1: relation with another class, Say suburban, how do I ask all subcategories with restrictions on the category category?

For example, query all subcategories where the title in the class squares is "test".

  class category model {increases public function subcategories ({$ return-> $-> Hammaman ('app \ subcategory'); }} Increases class subcategory model {public function category ()) {return $ this-> Affiliation ('app' category '); }}  

I have tried:

  $ subcategories = Subcategory :: with (['category' => function ($ query) {$ Query-> where ('title', '=', 'test');}]) - & gt; get (); // gives empty space to all subcategories, where there is no zero = test  

I am interested in all the ways of doing this!

edit - two solutions:

  $ data = category :: whereTitle ('test') - & gt; First () -> gt; Subcategories // or equivalently: $ data = category :: whereTitle ('test') - & gt; (Get) [0] - & gt; Subcategories;  

or create a dynamic scope by using an alpha solution within the subcategory

  public function scope range ($ query, $ type) {return $ Query-> Where (use 'range', function ($ q) ($ type) {$ q-> gtetitical ($ type);}); } // Then you can call segment from $ data = subcategory :: ('test') - & gt; get ();  

You can try to do something like this:

  $ Subcategories = Subcategory :: Where is ('range', function ($ query) {$ query-> where ('title', 'test');}) From ('Category') - & gt; );   

This code will return the subcategories with the related category but only the related category.title Test . See also documentation about it.


No comments:

Post a Comment