Tengo tres tablas:
- Users
- Profiles
- Ads
Estoy listando todos los anuncios que se encuentren publicados, me los muestra... Pero requiero mostrar la foto del perfil junto al anuncio, pero la collección no me trae la información del perfil y el problema debe estar en la relación.
Mi tabla users en la migración:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('city_id')->nullable();
$table->foreign('city_id')->references('id')->on('cities');
$table->string('first_name');
$table->string('last_name');
$table->string('username')->unique();
$table->string('phone')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Mi tabla Profiles en la migración:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedBigInteger('profession_id')->nullable();
$table->foreign('profession_id')->references('id')->on('professions');
$table->text('description')->nullable();
$table->string('photo')->nullable();
$table->timestamps();
});
}
Mi tabla Ads en la migración:
public function up()
{
Schema::create('ads', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('code');
$table->string('title');
$table->text('description');
$table->string('status')->default('DRAFT');
$table->timestamps();
});
}
Las tengo relacionadas de la siguiente manera:
Model User:
public function profile()
{
return $this->belongsTo(Profile::class);
}
public function ads()
{
return $this->hasMany(Ad::class);
}
Model Profile:
public function user()
{
return $this->belongsTo(User::class);
}
public function profession()
{
return $this->belongsTo(Profession::class);
}
Model ads
public function user()
{
return $this->belongsTo(User::class);
}
public function profile()
{
return $this->belongsTo(Profile::class);
}
Mi consulta en el controlador:
public function index()
{
$ads = Ad::with('user.profile')
->whereStatus('PUBLISHED')
->orderBy('ads.id', 'DESC')
->paginate(12);
dd($ads);
return view('web/users/listing', compact('ads'));
}
Gracias de antemano