1
I have this structure
Migration from the Exercise table
public function up()
{
Schema::create('exercicio', function(Blueprint $table)
{
$table->increments('id');
$table->string('nome')->unique();
$table->timestamps();
$table->time('deleted_at')->nullable();
});
}
Table Migration Musculature
public function up()
{
Schema::create('musculatura', function(Blueprint $table)
{
$table->increments('id');
$table->string('nome')->unique();
$table->timestamps();
$table->time('deleted_at')->nullable();
});
}
Migration of the associative table between Exercicio_musculatura
public function up()
{
Schema::create('exercicio_musculatura', function (Blueprint $table) {
$table->integer('exercicio_id')->unsigned();
$table->foreign('exercicio_id')->references('id')->on('exercicio')->onUpdate('cascade')->onDelete('cascade');
$table->integer('musculatura_id')->unsigned();
$table->foreign('musculatura_id')->references('id')->on('musculatura')->onUpdate('cascade')->onDelete('cascade');
});
}
Model of the Table Exercise
use SoftDeletes;
public $timestamps = true;
protected $table = 'exercicio';
protected $fillable = ['nome'];
protected $dates = ['deleted_at'];
//Aqui é o método que retorna todos os exercicios com suas respectivas musculaturas pré cadastradas no banco
//o método vai buscar no segundo parametro deste return "na tabela associativa exercicio_musculatura"
public function musculaturas(){
return $this->belongsToMany("App\Http\Models\Musculatura", "Exercicio_Musculatura");
}
public function aparelhos(){
return $this->belongsToMany("App\Http\Models\Aparelho", "Exercicio_Aparelho");
}
Model of the Musculature Table
use SoftDeletes;
public $timestamps = true;
protected $table = 'exercicio';
protected $fillable = ['nome'];
protected $dates = ['deleted_at'];
//Aqui é o método que retorna todos os exercicios com suas respectivas musculaturas pré cadastradas no banco
//o método vai buscar no segundo parametro deste return "na tabela associativa exercicio_musculatura"
public function musculaturas(){
return $this->belongsToMany("App\Http\Models\Musculatura", "Exercicio_Musculatura");
}
public function aparelhos(){
return $this->belongsToMany("App\Http\Models\Aparelho", "Exercicio_Aparelho");
}
Model of the Associative Table of Exercicio_musculatura
class Exercicio_Musculatura extends Model
{
}
My question is the following: I am in the controller, and receiving from an AJAX the ID of the musculature from another place, and I want to consult all the exercises related to that musculature, my bank already has in the associative table the id’s very straight. I’m unable to return anything in my query, how can I do this?
How is the consultation that is not working? please post
– Ryan Lemos
DB::('exercise')->Where('id', "=", $musculatura_id)->with('musculaturas')->get();
– ArieLopes
I’m trying to do this query in a controller
– ArieLopes
It is more interesting to use the models class instead of the 'DB' class. But there are some mistakes, I will try to formulate an answer here and you test if it worked.
– Ryan Lemos