I have 2 relationships that point to the same User model: operador() and profesional().
class Cita extends Model
{
public function paciente(){
return $this->belongsTo('\App\Models\Paciente');
}
public function profesional(){
return $this->belongsTo('\App\Models\User');
}
public function operador(){
return $this->belongsTo('\App\Models\User');
}
}
In the view I call them like this:
@foreach ($comisiones as $comision)
<tr>
<td>{{ $comision->paciente->name }}</td>
<td>{{ $comision->profesional->name }}</td>
<td>{{ $comision->operador->name }}</td>
<td>{{ number_format($comision->total, 0, '.', '.') }}</td>
<td>{{ $comision->estado }}</td>
</tr>
@endforeach
The program crashes on me when it tries to call $commision->operador->name. If I leave it as a comment it works without problems. But it gives me an error when I have the 2 relations at the same time.
Can I have 2 relationships pointing to the same model? And if not, what alternative do I have? Thanks
CodePudding user response:
When you write :
public function operador(){
return $this->belongsTo('\App\Models\User');
}
Laravel expects that The Migration (The table citas) has a column named operador_id
. So Yes you can have multiple relationships to the same model.
CodePudding user response:
I modified comisiona_id and it keeps giving me an error. Mi migration:
$table->unsignedBiginteger('comisiona_id')->nullable(); // usuario que gana comision
$table->foreign('comisiona_id')->references('id')->on('users')->onDelete('cascade');
Mi modelo:
public function comisiona(){
return $this->belongsTo('\App\Models\User');
}
And my view
<td>{{ $comision->profesional->name }}</td>
<td>{{ $comision->comisiona->name }}</td>
gives me the same error