tengo un problema con las relaciones ManyToMany en Laravel .
Tengo dos modelos que se relacionan con esta opción.
Modelo Patron - Tabla Patrons Modelo Atla - Tabla Atlas
En donde estas se relacionan con la tabla Pivot atla_patron.
EL problema es que cuando realizo la consulta desde un lado de la relación, en este caso desde Atla:
$atla = Atla::find(1)
$atla->patrons
Esta relacion si me da resultados
La inversa:
$patron = Patron::find(1)
$patron->atlas
Esta opcion me devuelve null.
si me da la opción de ver la tabla pivot y me da resultados con los atributos que se relacionan. Pero desde el lado del Patron me devuelve null. Adjunto codigo para verificacion. Agradezco las ayudas.
Modelo Patron
namespace App;
use Illuminate\Database\Eloquent\Model;
class Patron extends Model
{
protected $guarded = [];
public function atlas()
{
return $this->belongsToMany('App\Atla', 'atla_patron', 'patron_id', 'atla_id');
}
public function paciente()
{
return $this->belongsTo('App\Paciente');
}
}
Modelo Atla
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Atla extends Model
{
public function patrons()
{
return $this->belongsToMany('App\Patron', 'atla_patron', 'atla_id', 'patron_id');
}
}
Tabla Atla
public function up()
{
Schema::create('atlas', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
});
}
Tabla Patron
public function up()
{
Schema::create('patrons', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('persona_id');
$table->foreign('persona_id')->references('persona_id')->on('pacientes');
//Realizar tabla hija
$table->string('x_supino_izq_1')->nullable(); // V o F si esta selelecionado o no
$table->string('x_supino_izq_2')->nullable();
$table->string('x_supino_izq_3')->nullable();
$table->string('x_supino_izq_4')->nullable();
$table->string('x_supino_der_1')->nullable();
$table->string('x_supino_der_2')->nullable();
$table->string('x_supino_der_3')->nullable();
$table->string('x_supino_der_4')->nullable();
$table->string('x_prono_izq_1')->nullable();
$table->string('x_prono_izq_2')->nullable();
$table->string('x_prono_izq_3')->nullable();
$table->string('x_prono_izq_4')->nullable();
$table->string('x_prono_der_1')->nullable();
$table->string('x_prono_der_2')->nullable();
$table->string('x_prono_der_3')->nullable();
$table->string('x_prono_der_4')->nullable();
$table->string('giro_cab')->nullable();
$table->string('pelvis')->nullable();
$table->string('rest_sacra')->nullable();
$table->string('fosa_izq')->nullable(); //Valor numerico de 1 a 10 con decimales
$table->string('fosa_der')->nullable(); //Valor numerico de 1 a 10 con decimales
$table->string('angular_omop')->nullable();
$table->string('oblicuo_men')->nullable();
$table->string('oblicuo_may')->nullable();
$table->string('ecom')->nullable();
$table->string('com')->nullable();
$table->string('recto_post_izq')->nullable();
$table->string('recto_post_der')->nullable();
$table->string('inter_trans')->nullable();
$table->string('espelino_cab')->nullable();
$table->string('espelino_cue')->nullable();
$table->string('esc_medio')->nullable();
$table->string('C3')->nullable();
$table->string('C4')->nullable();
$table->string('C5')->nullable();
$table->string('C6')->nullable();
$table->string('C7')->nullable();
$table->string('D1')->nullable();
$table->string('D2')->nullable();
$table->string('D3')->nullable();
$table->string('D4')->nullable();
$table->string('D5')->nullable();
$table->string('D6')->nullable();
$table->string('D7')->nullable();
$table->string('D8')->nullable();
$table->string('D9')->nullable();
$table->string('D10')->nullable();
$table->string('D11')->nullable();
$table->string('D12')->nullable();
$table->string('L1')->nullable();
$table->string('L2')->nullable();
$table->string('L3')->nullable();
$table->string('L4')->nullable();
$table->string('L5')->nullable();
$table->string('il_cost')->nullable();
$table->string('dor_l')->nullable();
$table->string('ser_p_sup')->nullable();
$table->string('ser_p_inf')->nullable();
$table->string('SCMa')->nullable();
$table->string('SCMe')->nullable();
$table->string('Lig_si')->nullable();
$table->mediumText('atlas')->nullable();
$table->mediumText('axis')->nullable();
$table->mediumText('inferiores')->nullable();
$table->timestamps();
});
}
Tabla atla_patron (pivot)
*/
public function up()
{
Schema::create('atla_patron', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreign('atla_id')->references('id')->on('atlas');
$table->foreign('patron_id')->references('id')->on('patrons');
$table->unsignedBigInteger('atla_id');
$table->unsignedBigInteger('patron_id');
// $table->timestamps();
});
}
$patron = Patron::with('atlas')->find(1); $patron->atlas;