Laravel Nova 没有坚持新模式
Laravel Nova not persisting new model
我 运行 遇到了一个奇怪的问题,即 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 / 任何其他 Web 路由中时,会成功创建一个新模型并将其保存在数据库中。
我的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',
];
}
我的用户table迁移
<?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 版本 15.1 分发 10.4.6-MariaDB
使用批量赋值时不初始化新模型class,调用静态方法::create()
\App\User::create(['name' => '', 'email' => '', etc]);
还要确保用户模型中的所有属性都是可填充的
protected $fillable = [
'name', 'email', 'password', // Add the rest
];
或者你也可以做我做的事
protected $guarded = []; // yolo
希望对您有所帮助
原来问题出在我的 MariaDB 安装上。最近升级了它,我想可能有一些配置导致了混乱。从 MariaDB 转移到 MySQL 8 解决了这个问题。
我 运行 遇到了一个奇怪的问题,即 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 / 任何其他 Web 路由中时,会成功创建一个新模型并将其保存在数据库中。
我的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',
];
}
我的用户table迁移
<?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 版本 15.1 分发 10.4.6-MariaDB
使用批量赋值时不初始化新模型class,调用静态方法::create()
\App\User::create(['name' => '', 'email' => '', etc]);
还要确保用户模型中的所有属性都是可填充的
protected $fillable = [
'name', 'email', 'password', // Add the rest
];
或者你也可以做我做的事
protected $guarded = []; // yolo
希望对您有所帮助
原来问题出在我的 MariaDB 安装上。最近升级了它,我想可能有一些配置导致了混乱。从 MariaDB 转移到 MySQL 8 解决了这个问题。