I know the question has been responded many times, but for some reason I couldn’t make it work for my tables, no matter what, and I don’t understand why.
I’ve been trying this for like 4 hours and I couldn’t get it done right.
So here are my functions from two models:
ConducatorDoctorat
public function materii() { return $this->belongsToMany('AppDomeniuDoctorat', 'profesor_domeniu', 'domeniu_id', 'profesor_id'); }
DomeniuDoctorat
public function materii() { return $this->belongsToMany('AppConducatorDoctorat', 'profesor_domeniu', 'profesor_id', 'domeniu_id'); }
and profesor_domeniu schema:
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class ProfesorDomeniu extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('profesor_domeniu', function (Blueprint $table) { $table->unsignedBigInteger('profesor_id'); $table->unsignedBigInteger('domeniu_id'); $table->foreign('profesor_id') ->references('id')->on('conducatori_doctorat') ->onDelete('cascade'); $table->foreign('domeniu_id') ->references('id')->on('domenii_doctorat') ->onDelete('cascade'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('profesor_domeniu'); } }
In my controller, I tried so many ways to do it, for example, like this:
public function edit($id) { $materii = ConducatorDoctorat::findOrFail($id)->materii()->pluck('domeniu_id'); return view('admin.conducatori_doctorat.edit')->with([ 'materii' => $materii ]); }
but it still doesn’t work.
With the given $id, I want to retrieve all the data from profesor_domeniu where profesor_id == $id.
That’s all, but I can’t get it.
How can this be done and why doesn’t my approach work?
//edit for clarity:
conducator_doctorat is where the professors are stored and domenii_doctorat is where their fields are stored.
In profesor_domeniu, I store what each professor teaches, by linking an id from conducatori_doctorat to an id of a field from domenii_doctorat.
//edit2:
materii() means the fields they teach.
//edit3:
My many-to-many relationship with some data added into the pivot table profesor_domeniu.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
eager load your relationship on your model :
public function edit($id) { $conducatorDoctorat = ConducatorDoctorat::with('materii')->findOrFail($id); $materii = $conducatorDoctorat->materii; return view('admin.conducatori_doctorat.edit')->with([ 'materii' => $materii ]); }
you can also request only your materii related to your conducatorDoctorat :
public function edit($id) { $materii = DomeniuDoctorat::whereHas('materii', function($query) use ($id){ $query->where('id', $id); })->get(); return view('admin.conducatori_doctorat.edit')->with([ 'materii' => $materii ]); }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0