@0314masaa

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

$ php artisan migrate が出来ない

解決したいこと

laravel開発者入門を参考に
laravelでWebアプリをつくっています。
$ php artisan migrate
をしてDBにテーブルを作成したいです。

発生している問題・エラー

BadMethodCallException
未定義のメソッドをコールバックが参照したり、引数を指定しなかったりした場合にスローされる例外です。
とありますが、参考書にも載っておらず、問題とされているMacroable.php:103も弄っていません。

terminal.
$ php artisan migrate

   BadMethodCallException  : Method Illuminate\Routing\UrlGenerator::forceSchema does not exist.

  at /home/ec2-user/environment/cms/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:103
     99|      */
    100|     public function __call($method, $parameters)
    101|     {
    102|         if (! static::hasMacro($method)) {
  > 103|             throw new BadMethodCallException(sprintf(
    104|                 'Method %s::%s does not exist.', static::class, $method
    105|             ));
    106|         }
    107| 

  Exception trace:

  1   Illuminate\Routing\UrlGenerator::__call("forceSchema")
      /home/ec2-user/environment/cms/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261

  2   Illuminate\Support\Facades\Facade::__callStatic("forceSchema")
      /home/ec2-user/environment/cms/app/Providers/AppServiceProvider.php:31

  Please use the argument -v to see more details.

指摘されているMacroable.php

Macroable.php
<?php

namespace Illuminate\Support\Traits;

use BadMethodCallException;
use Closure;
use ReflectionClass;
use ReflectionMethod;

trait Macroable
{
    /**
     * The registered string macros.
     *
     * @var array
     */
    protected static $macros = [];

    /**
     * Register a custom macro.
     *
     * @param  string  $name
     * @param  object|callable  $macro
     * @return void
     */
    public static function macro($name, $macro)
    {
        static::$macros[$name] = $macro;
    }

    /**
     * Mix another object into the class.
     *
     * @param  object  $mixin
     * @param  bool  $replace
     * @return void
     *
     * @throws \ReflectionException
     */
    public static function mixin($mixin, $replace = true)
    {
        $methods = (new ReflectionClass($mixin))->getMethods(
            ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
        );

        foreach ($methods as $method) {
            if ($replace || ! static::hasMacro($method->name)) {
                $method->setAccessible(true);
                static::macro($method->name, $method->invoke($mixin));
            }
        }
    }

    /**
     * Checks if macro is registered.
     *
     * @param  string  $name
     * @return bool
     */
    public static function hasMacro($name)
    {
        return isset(static::$macros[$name]);
    }

    /**
     * Dynamically handle calls to the class.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     *
     * @throws \BadMethodCallException
     */
    public static function __callStatic($method, $parameters)
    {
        if (! static::hasMacro($method)) {
            throw new BadMethodCallException(sprintf(
                'Method %s::%s does not exist.', static::class, $method
            ));
        }

        $macro = static::$macros[$method];

        if ($macro instanceof Closure) {
            $macro = $macro->bindTo(null, static::class);
        }

        return $macro(...$parameters);
    }

    /**
     * Dynamically handle calls to the class.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     *
     * @throws \BadMethodCallException
     */
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
下記の関数が問題かと思われます
    public function __call($method, $parameters)
    {
        if (! static::hasMacro($method)) {
下記がMacroable.php:103行目です
            throw new BadMethodCallException(sprintf(
                'Method %s::%s does not exist.', static::class, $method
            ));
        }

        $macro = static::$macros[$method];

        if ($macro instanceof Closure) {
            $macro = $macro->bindTo($this, static::class);
        }

        return $macro(...$parameters);
    }
}

作成中のcreate_books_table.php

2022_11_06_035022_create_books_table.php
<?php

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

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->strings('item_name');   //書籍名
            $table->integer('item_number'); //冊数
            $table->integer('item_amount'); //金額
            $table->datetime('published');  //本公開日
            $table->timestamps();
            
        });
    }

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

設定を変更したAppServiceProvider.php

AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
    //下記、2行追記しました
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //下記、2行追記しました
        Schema::defaultStringLength(191);
        URL::forceSchema('https');
    }
}


解りづらい書き方、質問で申し訳御座いません。
もし宜しければ、皆様アドバイスよろしくお願い致します。

0 likes

1Answer

    public function boot()
    {
        Schema::defaultStringLength(191);
        URL::forceSchema('https');
    }

forceSchemaではなくforceSchemeのようです。(末尾がaではなくe

どうやらバージョンアップで変更されたようです。
利用しているバージョンに合わせて対応してください。

0Like

Comments

  1. @0314masaa

    Questioner

    ありがとう御座います!!
    上記の内容を変更したところ問題なく進めました!!
    参考書の方も、もう一度確認したところ、
    URL::forceScheme('https');
    としっかり記載してありました。
    (他の箇所がずっとSchema表記だったので見落としておりました。)

    @blue32a様 本当にありがとう御座います!!

Your answer might help someone💌