Laravel: php artisan migrate:fresh failed ``database.themes`` 不存在

Posted

技术标签:

【中文标题】Laravel: php artisan migrate:fresh failed ``database.themes`` 不存在【英文标题】:Laravel: php artisan migrate:fresh failed ``database.themes`` does not exist 【发布时间】:2021-03-04 05:35:57 【问题描述】:

我正在尝试运行 php artisan migrate:fresh 来发布对我的播种机的修改。它部分工作,它删除了数据库中的所有表,但它不会重新创建它们,因为Table 'database.themes' does not exist。我在下面包含了我的Themes.php 模型、ThemesController.php 和任何其他需要Themes 的资源,希望能够理解这一点。

2020_11_14_125914_create_themes_table(迁移)

<?php

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

class CreateThemesTable extends Migration

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    
        Schema::create('themes', function (Blueprint $table) 
            $table->id('theme_id');

            $table->string('theme_title')->default('Main');
            $table->string('theme_slug')->default('main')->unique();
            $table->text('theme_description')->nullable();
            $table->string('theme_uri')->default('laravel.cmsbase.com')->nullable();
            $table->string('theme_version')->default('1.0.0')->nullable();
            $table->string('theme_license')->default('The MIT License (MIT)')->nullable();
            $table->string('theme_tags')->nullable();
            $table->string('theme_type')->default('web');
            $table->string('author_name')->default('Spencer K Media');
            $table->string('author_uri')->nullable();

            $table->boolean('is_theme')->default(1)->nullable();
            $table->timestamps();
        );

        DB::table('themes')->insert([
            'theme_id' => '1',
            'theme_title' => 'Core theme',
            'theme_slug' => 'main',
            'theme_description' => 'Default theme for the theme package',
            'theme_uri' => 'https://laravel.cmsbase.com',
            'theme_version' => '1.0.0',
            'theme_license' => 'The MIT License (MIT)',
            'theme_tags' => 'default, simple, base',
            'theme_type' => 'web',
            'author_name' => 'Spencer K Media',
            'author_uri' => 'https://spencerkmedia.com',
            'is_theme' => 1,
        ]);
    

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

Themes.php(模型)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Themes extends Model

    use HasFactory;
    
    protected $fillable = ['theme_title', 'theme_slug', 'theme_uri', 'theme_version', 'theme_license', 'theme_tags', 'theme_type', 'author_name', 'author_uri', 'is_theme'];

ThemesController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Themes;

use Carbon\Carbon;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ThemeSettingsController extends Controller

    public $themes;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    
        $themes = Themes::get();
        return view('admin.settings.theme', compact('themes'));
    

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function activate()
    
        
    

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, $theme, $json)
    
        // Get the path of theme.json
        $path = glob("/resources/views/themes/,*/theme.json", GLOB_BRACE);

        // Parse the json file into array with $json
        $json = json_decode(file_get_contents($path), true);

        var_dump($json);

        $this->validate($request, [
            'is_theme' => 'accepted'
        ]);

        $theme = new Themes();

        $theme->theme_title = $json->theme_title;
        $theme->theme_slug = strtolower($json->theme_title);
        $theme->theme_description = $json->theme_description;
        $theme->theme_uri = $json->theme_uri;
        $theme->theme_version = $json->theme_version;
        $theme->theme_license = $json->theme_license;
        $theme->theme_tags = $json->theme_tags;
        $theme->theme_type = $json->theme_type;
        $theme->author_name = $json->author_name;
        $theme->author_uri = $json->author_uri;
        $theme->is_theme = $request->is_theme;
        $theme->save();
        notify()->success('Theme successfully enabled :)', 'Success');
        return redirect()->back();
    

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    
        //
    

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    
        //
    

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    
        //
    

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    
        //
    

RouteServiceProvider

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use App\Models\Themes;

class RouteServiceProvider extends ServiceProvider

    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    
        $settings = Themes::get();
        $this->configureRateLimiting();

        $this->routes(function () use ($settings) 
            Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
                ->group(base_path('routes\api.php'));

            Route::middleware('web')
            ->namespace($this->namespace)
                ->group(base_path('routes\web.php'));


            foreach ($settings as $key => $setting) 
                Route::middleware('web')
                ->group(base_path('resources/views/themes/' . $setting->theme_slug . '/route.php'));
            
        );
    

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    
        RateLimiter::for('api', function (Request $request) 
            return Limit::perMinute(60);
        );
    

Web.php(路由文件)

Route::prefix('settings')->name('settings.')->group(function () 
    Route::get('/home', function () 
        return view('admin.settings.index');
    )->name('index');
    Route::get('/general', function () 
        return view('admin.settings.general');
    )->name('general');
    Route::get('/site', function () 
        return view('admin.settings.site');
    )->name('site');
    Route::get('/theme', function () 
        return view('admin.settings.theme');
    )->name('theme');
);

任何帮助表示赞赏并提前感谢您!

【问题讨论】:

通常您会分别创建(迁移)和填充(播种机)。我不记得这是否真的是一个例外,但无论哪种方式,最好将它们分开。 您是否尝试删除您的 DB::insert 语句并再次运行迁移? 如果我没记错的话,laravel 在 artisan 命令上的命令之前运行 Providers,所以它尝试在 RouteServiceProvider 上运行 $settings = Themes::get(); 并失败,因此出现错误。对于临时修复,请删除该代码,然后运行 ​​migrate 命令。 @MathiasHillmann 请把你的答案写在下面,我会标记它,另外,这个服务提供商是完整功能所必需的,我打算让这个 laravel 包可用,有没有另一种方法来制作其服务提供商之外的路由工作?例如,在控制器中? 【参考方案1】:

Laravel 在 artisan 命令的命令之前运行 Providers,所以它试图 $settings = Themes::get();在 RouteServiceProvider 上失败,因此出现错误。对于临时修复,请删除该代码,然后运行 ​​migrate 命令。

对于更永久的修复,您可以在运行查询之前检查模型/表是否存在:

    public function boot()
    
        if($settings = Themes::get())
            $this->configureRateLimiting();

            $this->routes(function () use ($settings) 
                Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                    ->group(base_path('routes\api.php'));

                Route::middleware('web')
                ->namespace($this->namespace)
                    ->group(base_path('routes\web.php'));


                foreach ($settings as $key => $setting) 
                    Route::middleware('web')
                    ->group(base_path('resources/views/themes/' . $setting->theme_slug . '/route.php'));
                
            );
        
    

【讨论】:

以上是关于Laravel: php artisan migrate:fresh failed ``database.themes`` 不存在的主要内容,如果未能解决你的问题,请参考以下文章

php Laravel Artisan迁移实例

php Laravel Artisan Migrate创建领域

无法 php artisan 迁移 - Laravel

laravel 自动删除 php artisan serve 上的 server.php

如何使用 Artisan 命令 (PHP) 在 Laravel 5.2 中创建 RESTful 资源控制器

Laravel php artisan 产生错误