Home>

I am trying to download the API in the selected product category of certain attribute groups. I can not understand how to display the name of the attribute group of the selected category, all groups are displayed on the API. So the structure in the database looks like, so displays the API.

Class GroupScontroller EXTENDS CONTROLLER
   {
     Public Function Index (Request $ Request)
     {
       Return Groups :: All ();
      }
    } 

Add an example of data -what and in what kind want to get

InDevX2021-07-26 14:44:21

When choosing a category Card, I want to get a list of groups assigned to this category. There is a category: -1, -2 in category 1 Groups -Basic, Country of manufacture, Screen diagonal. And the category 2 groups are basic, screen format. In the future, when you click on the group to display the list of attributes that correspond to it. Now implemented by the API output of attributes when choosing a category, I want to break the attributes to the subgroups and then cause.

Denis Mamedom2021-07-26 14:56:55
  • Answer # 1

    Making groups for the category. API Controller groups for comparing category and group and output

    Public Function Index (Request $ Request)
     {
       $ Category_ID= Category_ID;
      $ Data= Array ("Data"= >
     []);
      $ success= array ('success'= >
     true);
      $ Status= Array ('Status'= >
     200);
      $ Output= [];
      $ result= [];
      if (IS_NUMERIC ($ Request->
    INPUT ('Category_id'))) {
        $ Category_ID= $ Request->
    INPUT ('Category_ID');
        $ Category= Category :: Find ($ Category_ID);
        If (! is_null ($ Category) &
    &
     Count ($ Category->
    toarray ())) {
        $ groups= $ Category->
    Groups;
        Foreach ($ Groups AS $ GROUP)
        {
          Unset ($ Group [Created_at ']);
          Unset ($ group ['updated_at']);
          Unset ($ group ['pivot']);
          Array_Push ($ Result, $ Group);
        }
       }
      }
      $ DATA ['Data']= $ Result;
      Array_Push ($ Output, $ DATA);
      Array_Push ($ Output, $ Success);
      Array_Push ($ Output, $ Status);
      RETURN RESPONSE () ->
    JSON ($ Output, 200);
      }
    

    prescribed in the model model model

    Public Function GROUPS ()
    {
        Return $ this->
    Belongstomany (GROUPS :: Class);
    }
    
  • Answer # 2

    Making groups for the category. API Controller groups for comparing category and group and output

    Public Function Index (Request $ Request)
     {
       $ Category_ID= Category_ID;
      $ Data= Array ("Data"= >
     []);
      $ success= array ('success'= >
     true);
      $ Status= Array ('Status'= >
     200);
      $ Output= [];
      $ result= [];
      if (IS_NUMERIC ($ Request->
    INPUT ('Category_id'))) {
        $ Category_ID= $ Request->
    INPUT ('Category_ID');
        $ Category= Category :: Find ($ Category_ID);
        If (! is_null ($ Category) &
    &
     Count ($ Category->
    toarray ())) {
        $ groups= $ Category->
    Groups;
        Foreach ($ Groups AS $ GROUP)
        {
          Unset ($ Group [Created_at ']);
          Unset ($ group ['updated_at']);
          Unset ($ group ['pivot']);
          Array_Push ($ Result, $ Group);
        }
       }
      }
      $ DATA ['Data']= $ Result;
      Array_Push ($ Output, $ DATA);
      Array_Push ($ Output, $ Success);
      Array_Push ($ Output, $ Status);
      RETURN RESPONSE () ->
    JSON ($ Output, 200);
      }
    

    prescribed in the model model model

    Public Function GROUPS ()
    {
        Return $ this->
    Belongstomany (GROUPS :: Class);
    }