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?