How to use nested loading nested in Laravel 5.4 correctly? my related tables return "null"

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 profiles table, which has several profile_categories that also belongs to categories (Many-to-Many relationship). When I run I receive 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 look 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 search to receive data from the associated category?

    
asked by anonymous 21.04.2018 / 03:25

1 answer

0

I was able 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 carry out my search by evaluating the name, about, categories and also the street in the information. Otherwise I could not.

    
21.04.2018 / 16:14