title: Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::type()
body: <p>I'm trying to make a api that will return the type of a word (noun, pronoun, verb, etc.) after creating that word in the database. But for some reason I am getting a "Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::type()" error when the type method is clearly defined in my vocabulary model. I am not using a many to many relationship but a one to many (that's why I am using hasMany() and belongsTo). Type has many Vocabulary but Vocabulary has only one Type and many VocabularyContents and VocabularyContent has only one vocabulary it is related to. So clearly no many to many relationship. So clearly my question is not a duplicate of <a href="https://stackoverflow.com/questions/38868551/call-to-undefined-method-laravel-5-2">Call to undefined method (laravel 5.2)
  </a>. Here are parts of the code for the application.</p>
  
  <p>The first model is the type model it allows me to get the "contents" of a type (model not listed here) and the vocabularies that belongs to a specific type.</p>
  
  <p><strong>model-code-listing 1: VocType.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class VocType extends Model
  {
      public function contents()
      {
          return $this-&gt;hasMany('App\VocTypeContent');
      }
  
      public function vocabularies()
      {
          return $this-&gt;hasMany('App\VocVocabulary');
      }
  }
  </code></pre>
  
  <p>this second model allows me to create a word in the vocabulary table access its "contents", type and category. This is where the issue lies.</p>
  
  <p><strong>model-code-listing 2: VocVocabulary.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class VocVocabulary extends Model
  {
      protected $fillable = ['voc_category_id','type_id', 'name', 'context', 'picture'];
      public $timestamps = false;
  
      public function contents()
      {
          return $this-&gt;hasMany('App\VocVocabularyContent');
      }
  
      public function type()
      {
          return $this-&gt;belongsTo('App\VocType');
      }
  
      public function category()
      {
          return $this-&gt;belongsTo('App\VocCategory');
      }
  }
  </code></pre>
  
  <p>The third model allows me to create the content of a vocabulary and access it parent vocabulary.</p>
  
  <p><strong>model-code-listing 3: VocVocabularyContent.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class VocVocabularyContent extends Model
  {
      protected $fillable = ['voc_vocabulary_id','lang_id', 'content', 'context', 'romanization', 'pronunciation', 'audio'];
      public $timestamps = false;
  
      public function vocabulary()
      {
          return $this-&gt;belongsTo('App\VocVocabulary');
      }
  }
  </code></pre>
  
  <p>below are the three migrations used for the models listed above.</p>
  
  <p><strong>migration-code-listing 1: create_voc_types_table.php</strong></p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateVocTypesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('voc_types', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('name');
              $table-&gt;string('abbreviation');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('voc_types');
      }
  }
  </code></pre>
  
  <p><strong>migration-code-listing 2: create_voc_vocabularies_table.php</strong></p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateVocVocabulariesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('voc_vocabularies', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;unsignedInteger('cat_id');
              $table-&gt;unsignedInteger('type_id');
              $table-&gt;foreign('cat_id')-&gt;references('id')-&gt;on('voc_categories')-&gt;onDelete('cascade');
              $table-&gt;foreign('type_id')-&gt;references('id')-&gt;on('voc_types')-&gt;onDelete('cascade');
              $table-&gt;string('name');
              $table-&gt;string('context');
              $table-&gt;string('picture');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('voc_vocabularies');
      }
  }
  </code></pre>
  
  <p><strong>migration-code-listing 3: create_voc_vocabulary_contents_table.php</strong></p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateVocVocabularyContentsTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('voc_vocabulary_contents', function (Blueprint $table) {
              $table-&gt;primary(['voc_id', 'lang_id']);
              $table-&gt;unsignedInteger('voc_id');
              $table-&gt;unsignedInteger('lang_id');
              $table-&gt;foreign('voc_id')-&gt;references('id')-&gt;on('voc_vocabularies')-&gt;onDelete('cascade');
              $table-&gt;foreign('lang_id')-&gt;references('id')-&gt;on('languages')-&gt;onDelete('cascade');
              $table-&gt;string('content');
              $table-&gt;string('context');
              $table-&gt;string('romanization');
              $table-&gt;string('pronunciation');
              $table-&gt;string('audio');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('voc_vocabulary_contents');
      }
  }
  </code></pre>
  
  <p>This is the controller where I am calling the type() method of vocabulary. basically I have an html form that sends a post request to this controller's method (postVocabularyAPI) if no id is provided in the request a vocabulary will be created (if the language is english). Then whether or not an id is provided with the request the method will create a vocabulary "content" for the given id (if no id is provided the given id will be the id of the previously created vocabulary). Then the postVocabularyAPI method will return a json response containing the id of the type of the vocabulary.</p>
  
  <p><strong>controller-code-listing 1: Vocabulearn.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\DB;
  use App\Language;
  use App\VocTheme;
  use App\VocCategory;
  use App\VocCategoryContent;
  use App\VocVocabulary;
  use App\VocVocabularyContent;
  use App\VocType;
  
  class Vocabulearn extends Controller
  {
  
      //other methods above
  
      public function postVocabularyAPI(Request $request, $language, $theme, $category){
  
          $vocabulary_id = $request-&gt;vocabulary_id;
          if($vocabulary_id === NULL){
              if($language == "english"){
                  $vocabulary = VocVocabulary::create([
                      'voc_category_id'   =&gt; VocCategory::where("slug", $category)-&gt;get()-&gt;first()-&gt;id,
                      'type_id'           =&gt; VocType::where("abbreviation", $request-&gt;type)-&gt;get()-&gt;first()-&gt;id,
                      'name'              =&gt; ucfirst(addslashes($request-&gt;translation)),
                      'context'           =&gt; $request-&gt;context,
                      'picture'           =&gt; ''
                  ]);
                  $vocabulary_id = $vocabulary-&gt;id;
              } else {
                  echo '{"success":false, "message":"Create first the English Vocabulary"}';
              }
          }
  
          $vocabularyContent = VocVocabularyContent::where('lang_id', '=', Language::where("slug", $language)-&gt;get()-&gt;first()-&gt;id)
          -&gt;where('voc_vocabulary_id', '=', $vocabulary_id)
          -&gt;first();
  
          if($vocabularyContent !== NULL){
              $vocabularies = DB::table('voc_vocabulary_contents')
              -&gt;where('lang_id', '=', Language::where("slug", $language)-&gt;get()-&gt;first()-&gt;id)
              -&gt;where('voc_vocabulary_id', '=', $vocabulary_id)
              -&gt;delete();
          }
  
          $vocabularyContent = VocVocabularyContent::create([
              'voc_vocabulary_id' =&gt; $vocabulary_id,
              'lang_id' =&gt; Language::where("slug", $language)-&gt;get()-&gt;first()-&gt;id,
              'content' =&gt; ucfirst(addslashes($translation)),
              'context' =&gt; addslashes($context),
              'romanization' =&gt; strtolower(addslashes($romanization)),
              'pronunciation' =&gt; $pronunciation,
              'audio' =&gt; $request-&gt;audio
          ]);
  
          echo '{"success":true, "type":"'.stripslashes(html_entity_decode($vocabularyContent-&gt;vocabulary()-&gt;type()-&gt;id)).'"}';
      }
  }
  </code></pre>
  
  <p>doing this gives me a </p>
  
  <blockquote>
    <p>"Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::type()"</p>
  </blockquote>
  
  <p>even when I change</p>
  
  <pre><code>echo '{"success":true, "type":"'.stripslashes(html_entity_decode($vocabularyContent-&gt;vocabulary()-&gt;type()-&gt;id)).'"}';
  </code></pre>
  
  <p>by</p>
  
  <pre><code>echo '{"success":true, "type":"'.stripslashes(html_entity_decode($vocabularyContent-&gt;vocabulary()-&gt;get()-&gt;first()-&gt;type()-&gt;id)).'"}';
  </code></pre>
  
  <p>I get an error stating </p>
  
  <blockquote>
    <p>"Call to a member function type() on null"</p>
  </blockquote>
  
  <p>which isn't right because the database was properly populated so I shouldn't be getting a null vocabulary.</p>