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.
– novic
Algumas link Relationship 1:n: https://answall.com/questions/152089/problemas-com-related-um-para-muitos-laravel/152108#152108
– novic
Relationship - N:M https://answall.com/questions/244308/laravel-eloquent-consulta-em-mais-de-umarts/244376#244376
– novic
@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.
– Walter Lucas