How to use Anxious Loading nested in Laravel 5.4 correctly? My related tables return "null"

Asked

Viewed 100 times

0

I have the following search in my application:

$results = Profile::with('profileCategories.categories', 'informations')->where('ativo', '=', "1")->where('nome', 'like', '%' . $request->search . '%')->orWhere('sobre', 'like', '%' . $request->search . '%')->orderBy('profile_type_id', 'ASC')->get();

Where I have a relationship between the table profiles, which has several profile_categories that also belongs to Categories (Many-to-Many relationship). When running I get Collection:

Collection {#333 ▼
  #items: array:1 [▼
    0 => Profile {#336 ▼
      +table: "profiles"
      +fillable: array:14 [▶]
      #casts: array:15 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:18 [▶]
      #original: array:18 [▶]
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: array:2 [▼
        "profileCategories" => Collection {#337 ▼
          #items: array:3 [▼
            0 => ProfileCategory {#344 ▼
              +table: "profile_categories"
              +fillable: array:1 [▶]
              #casts: array:2 [▶]
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:5 [▶]
              #original: array:5 [▶]
              #dates: []
              #dateFormat: null
              #appends: []
              #events: []
              #observables: []
              #relations: array:1 [▼
                "categories" => null
              ]
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
            1 => ProfileCategory {#345 ▶}
            2 => ProfileCategory {#346 ▶}
          ]
        }
        "informations" => Collection {#339 ▶}
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
  ]
}

My models are like this:

class ProfileCategory extends Model
{

    public $table = 'profile_categories';

    ...

    public function profiles()
    {
        return $this->belongsTo(\App\Models\Profile::class);
    }

    public function categories()
    {
        return $this->belongsTo(\App\Models\Category::class);
    }


class Category extends Model
{

    public $table = 'categories';

    ...

    public function profileCategories()
    {
        return $this->hasMany(\App\Models\ProfileCategory::class);
    }
}

class Profile extends Model
{
    public $table = 'profiles';

 ...

    public function profileCategories()
    {
        return $this->hasMany(\App\Models\ProfileCategory::class);
    }
}

With this I get "Categories" => null, how do I get the data from the associated category?

  • Can it be the keys that are not in the standard that Eloquent requires and if it is not in the standard (convention) has to be configured, if you could put the layout of these tables and their relations!? I say this because this is how it loads the relations ! if it does not load can be configuration.

  • Algumas link Relationship 1:n: https://answall.com/questions/152089/problemas-com-related-um-para-muitos-laravel/152108#152108

  • Relationship - N:M https://answall.com/questions/244308/laravel-eloquent-consulta-em-mais-de-umarts/244376#244376

  • @Virgilionovic took a look at the links and the only thing that is out of the patterns is the name of the table, but even using something apra rename did not work very well. Thank you for your attention.

1 answer

0

I managed to solve my problem as follows:

$results = Profile::rightJoin('informations', 'informations.profile_id', '=', 'profiles.id')
            ->join('profile_categories', 'profile_categories.profile_id', '=', 'profiles.id')
            ->rightJoin('categories', 'profile_categories.category_id', '=', 'categories.id')
            ->where('profiles.ativo', '=', "1")
            ->where(function ($query) use ($request) {
                $query->where('profiles.nome', 'like', '%' . $request->search . '%')
                    ->orWhere('profiles.sobre', 'like', '%' . $request->search . '%')
                    ->orWhere('categories.descricao', '=', $request->search)
                    ->orWhere('informations.rua', 'like', '%' . $request->search . '%');
            })
            ->orderBy('profiles.profile_type_id', 'ASC')
            ->select('profiles.id', 'profiles.nome', 'profiles.sobre', 'profiles.profile_type_id', 'informations.rua')->distinct()->get();

Now I can perform my search by evaluating the name, about, categories and also the street in the information. Otherwise I couldn’t do it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.