Tengo un error con una relación muchos a muchos en Larvel 5.5
Relaciono una tabla interventions
con otra patients
con tabla pivote user_interventions
. A continuación adjunto las migraciones y los modelos.
intenventions
public function up()
{
Schema::create('interventions', function (Blueprint $table) {
$table->increments('id');
$table->date('date');
$table->integer('professional_id')->unsigned();
$table->foreign('professional_id')->references('id')->on('professionals');
$table->string('patient_record')->nullable;
$table->foreign('patient_record')->references('record')->on('patients');
$table->integer('type_id')->unsigned();
$table->foreign('type_id')->references('id')->on('types');
$table->integer('place_id')->unsigned();
$table->foreign('place_id')->references('id')->on('places');
$table->text('description')->nullable();
$table->timestamps();
});
}
El modelo interventions
namespace App;
use Illuminate\Database\Eloquent\Model;
class Intervention extends Model
{
protected $fillable = ['date', 'professional_id', 'patient_record', 'type_id', 'place_id', 'description'];
public function patients()
{
return $this->belongsToMany('App\Patient', 'user_interventions', 'interventions_id', 'patient_record');
}
public function professionals()
{
return $this->belongsTo(Professional::class, 'professional_id');
}
public function types()
{
return $this->belongsTo(Type::class, 'type_id');
}
public function places()
{
return $this->belongsTo(Place::class, 'place_id');
}
}
patients
public function up()
{
Schema::create('patients', function (Blueprint $table) {
$table->string('record', 7)->index();
$table->string('name', 75);
$table->timestamps();
});
}
El modelo patients
class Patient extends Model
{
public function interventions()
{
return $this->belongsToMany('App\Intervention', 'user_interventions', 'patient_record', 'interventions_id');
}
}
El código que utilizo para pasar datos a la vista
public function createUsers(Request $request)
{
$intervention_id = $request->intervention_id;
if ($intervention_id) {
$patients = Patient::all();
$interventiongrup = Intervention::where('id', $intervention_id)->with('professionals')->with('types')->with('places')->get();
$intervention_users = Intervention::where('id', $intervention_id)->with('patients')->get();
return view('interventions.grupal')->with(compact('patients', 'interventiongrup', 'intervention_users',$intervention_id));
}
}
El código que utilizo para mostrar los datos en la vista
<table>
<thead>
<tr>
<th>Usuarios</th>
</tr>
</thead>
<tbody>
@foreach ($intervention_users->patients as $intervention_patient)
<tr>
<td width="450px">{{ $intervention_patient->record }} {{ $intervention_patient->name }}</td>
</tr>
@endforeach
</tbody>
</table>
Espero su ayuda, gracias.
En primer lugar lo hago como me dices pero me sigue dando error:
Aqui pongo el modelo Patient
class Patient extends Model
{
protected $fillable = ['record', 'name'];
protected $primaryKey = 'record';
protected $keyType = 'string';
public $incrementing = 'false';
public function interventions()
{
return $this->belongsToMany('App\Intervention', 'user_interventions', 'patient_record', 'interventions_id')->withTimestamps();
}
// public function user_interventions()
// {
// return $this->hasMany(User_intervention::class);
// }
}
Y el código del controlador que devuelve el valor:
public function createUsers(Request $request)
{
$intervention_id = $request->intervention_id;
if ($intervention_id) {
$patients = Patient::all();
$interventiongrup = Intervention::where('id', $intervention_id)->with('professionals')->with('types')->with('places')->get();
$intervention_users = Intervention::where('id', $intervention_id)->with('patients')->get();
return view('interventions.grupal')->with(compact('patients', 'interventiongrup', 'intervention_users', $intervention_id));
}
}
Y el código de la vista:
<table>
<thead>
<tr>
<th>Usuarios</th>
</tr>
</thead>
<tbody>
@if ($intervention_users)
@foreach ($intervention_users as $intervention_patient)
<tr>
@if ($intervention_patient)
<td width="450px">{{ $intervention_patient->patients->record }} {{ $intervention_patient->patients->name }}</td>
@endif
</tr>
@endforeach
@endif
</tbody>
</table>
Y como he puesto en la respuesta, al mostrar con dd
si que aparece la relación. No se si puede tener algo que ver el pasarlo con compact
.
id
depatients
id
depatients
es el camporecord
.