title: Retrieve data from many to many relationship in Laravel
body: <p>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.</p>
  <p>I've been trying this for like 4 hours and I couldn't get it done right.</p>
  <p>So here are my functions from two models:</p>
  <p>ConducatorDoctorat</p>
  <pre><code>public function materii()
      {
          return $this-&gt;belongsToMany('App\DomeniuDoctorat', 'profesor_domeniu', 'domeniu_id', 'profesor_id');
      }
  </code></pre>
  <p>DomeniuDoctorat</p>
  <pre><code>public function materii()
      {
          return $this-&gt;belongsToMany('App\ConducatorDoctorat', 'profesor_domeniu', 'profesor_id', 'domeniu_id');
      }
  </code></pre>
  <p>and profesor_domeniu schema:</p>
  <pre><code>&lt;?php
  
  use Illuminate\Database\Migrations\Migration;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Support\Facades\Schema;
  
  class ProfesorDomeniu extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('profesor_domeniu', function (Blueprint $table) {
              $table-&gt;unsignedBigInteger('profesor_id');
              $table-&gt;unsignedBigInteger('domeniu_id');
              
              $table-&gt;foreign('profesor_id')
                    -&gt;references('id')-&gt;on('conducatori_doctorat')
                    -&gt;onDelete('cascade');
              $table-&gt;foreign('domeniu_id')
                    -&gt;references('id')-&gt;on('domenii_doctorat')
                    -&gt;onDelete('cascade');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('profesor_domeniu');
      }
  }
  </code></pre>
  <p>In my controller, I tried so many ways to do it, for example, like this:</p>
  <pre><code>public function edit($id)
      {
          $materii = ConducatorDoctorat::findOrFail($id)-&gt;materii()-&gt;pluck('domeniu_id');
  
          return view('admin.conducatori_doctorat.edit')-&gt;with([
              'materii' =&gt; $materii
          ]);
      }
  </code></pre>
  <p>but it still doesn't work.</p>
  <p>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.</p>
  <p>How can this be done and why doesn't my approach work?</p>
  <p>//edit for clarity:</p>
  <p>conducator_doctorat is where the professors are stored and domenii_doctorat is where their fields are stored.</p>
  <p>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.</p>
  <p>//edit2:</p>
  <p>materii() means the fields they teach.</p>
  <p>//edit3:</p>
  <p>My many-to-many relationship with some data added into the pivot table profesor_domeniu.</p>
  <p><a href="https://i.stack.imgur.com/nIqOj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nIqOj.png" alt="click" /></a></p>