【问题标题】:Laravel Nova not persisting new modelLaravel Nova 没有坚持新模式
【发布时间】:2020-02-16 18:39:36
【问题描述】:

我遇到了一个奇怪的问题,即 Laravel Nova 无法持久化我的任何模型。尝试在新的 Nova 安装中保存用户并使用创建的默认用户资源仍然失败。 (虽然更新用户条目是可行的)。

我在尝试创建新用户时遇到的错误是:

No query results for model [App\User].

查看 Laravel Nova Core,负责此的文件是:vendor/laravel/nova/src/Http/Controllers/ResourceStoreController.php 在这个函数里面,如果我尝试手动保存一个新模型

dd((new User())->create(['name' => '', 'email' => '', etc]));

它成功输出了模型,但仍然没有将它保存在数据库中。

将相同的代码放置在 Tinker / 任何其他网络路由中时,成功创建了一个新模型并将其保存在数据库中。

我的app\Nova\User.php

<?php

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\Password;

class User extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = 'App\\User';

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'name';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id', 'name', 'email',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Gravatar::make(),

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),

            Text::make('Email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            Password::make('Password')
                ->onlyOnForms()
                ->creationRules('required', 'string', 'min:8')
                ->updateRules('nullable', 'string', 'min:8'),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

用户模型

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

我的用户表迁移

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

知道什么可能会以这种方式触发保存操作吗?有关其他信息,我的设置如下:

Laravel (5.8) 新星 (2.5) PHP 7.2 MySQL Ver 15.1 Distrib 10.4.6-MariaDB

【问题讨论】:

    标签: php mysql laravel mariadb laravel-nova


    【解决方案1】:

    原来问题出在我的 MariaDB 安装上。最近升级了它,我想可能有一些配置引起了混乱。从 MariaDB 迁移到 MySQL 8 解决了这个问题。

    【讨论】:

    • 您确定两个数据库版本之间需要什么功能或不同的功能吗?这对于 (1) Laravel 开发人员和 (2) 其他用户来说很方便。
    • 感谢@RickJames,不,不幸的是我无法确定。最近从 mysql 5 更改为 mariadb 10,我认为在切换期间我没有正确解决一些冲突,因此出现了错误。这是我的强烈怀疑。
    【解决方案2】:

    使用批量赋值时不要初始化新的Model类,调用静态方法::create()

    \App\User::create(['name' => '', 'email' => '', etc]);
    

    还要确保用户模型中的所有属性都是可填充的

    protected $fillable = [
        'name', 'email', 'password', // Add the rest
    ];
    

    或者你可以做我做的事

    protected $guarded = []; // yolo
    

    希望对你有帮助

    【讨论】:

    • 谢谢,但两种方法都没有运气:(
    • 发布您的 app/Nova/User.php 课程,以便我重现此内容
    • 您的 nova 资源看起来不错,可能是用户模型或用户表迁移错误,请将这些添加到问题中
    • 完成。请记住,这基本上是 Laravel Nova 的全新安装 - 所以我没有定制任何东西。在我正在使用的更成熟的代码库上经历了同样的事情后,创建了这个新实例。
    • 一切都很好(你的User::create 代码在哪里?),我无法重现这个,所以这绝对是 Nova 中的一个错误,尝试下载一个新版本,因为你可能已经损坏了 / 中的文件新星目录
    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 2021-04-29
    • 2022-12-09
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    相关资源
    最近更新 更多