Home > Net >  Bad Method Call exception title does not exist
Bad Method Call exception title does not exist

Time:02-27

I am trying to run my create_library_data migration and I got this error.

   BadMethodCallException 

  Method Illuminate\Database\Schema\Blueprint::title does not exist.

  at vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php:113
    109▕      */
    110▕     public function __call($method, $parameters)
    111▕     {
    112▕         if (! static::hasMacro($method)) {
  ➜ 113▕             throw new BadMethodCallException(sprintf(
    114▕                 'Method %s::%s does not exist.', static::class, $method
    115▕             ));
    116▕         }
    117▕ 

  1   database/migrations/2022_02_26_143113_create_library_data.php:19
      Illuminate\Database\Schema\Blueprint::__call()

       4 vendor frames 
  6   database/migrations/2022_02_26_143113_create_library_data.php:25
      Illuminate\Support\Facades\Facade::__callStatic()

I am confused since I did create the migration file and thought I was following the correct structure. Here is the migration file. (all my other migrations passed btw).

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('library_data', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->title();
            $table->author();
            $table->description();
            $table->year_published();
            $table->num_of_pages();
            $table->genre();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('library_data');
    }
};

CodePudding user response:

No. It's wrong. Use this :

Schema::create('library_data', function (Blueprint $table) {
    $table->id(); // primary
    $table->string('title');
    $table->string('author');
    $table->text('description');
    $table->string('year_published');
    $table->string('num_of_pages');
    $table->string('genre');
    $table->timestamps(); // created_at and updated_at
});

Check Available Column Types

CodePudding user response:

Read the documentation. That is a wrong way to define the columns.

    public function up()
    {
        Schema::create('library_data', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('title', 50);
            $table->string('author', 50);
            $table->text('description');
            $table->year('year_published');
            $table->integer('num_of_pages');
            $table->string('genre', 50);
        });
    }

CodePudding user response:

Schema::create('books', function (Blueprint $table) {//library_data
        $table->id();
        $table->string('title');//title
        $table->string('genre');//genre
        $table->text('description')->nullable();//description
        $table->foreignId('author_id')->constrained('users');//author
        $table->date('year_published')->nullable();//year_published
        $table->unsignedBigInteger('num_of_pages')->nullable();//num_of_pages
        $table->timestamps();
      });

You can take the genre to a separate table and do many to many with it

  Schema::create('books', function (Blueprint $table) {//library_data
      $table->id();
      $table->string('title')->unique();//title
      $table->text('description')->nullable();//description
      $table->foreignId('author_id')->constrained('users');//author
      $table->date('year_published')->nullable();//year_published
      $table->unsignedBigInteger('num_of_pages')->nullable();//num_of_pages
      $table->timestamps();
  });

  Schema::create('genres', function (Blueprint $table) {
      $table->id();
      $table->string('name')->unique();
      $table->foreignId('author_id')->constrained('users');//author
      $table->timestamps();
  });

  Schema::create('book_genre', function (Blueprint $table) {
      $table->id();
      $table->foreignId('book_id')->constrained();
      $table->foreignId('genre_id')->constrained();
      $table->timestamps();
  });
  • Related