Php 为什么在插入数据库时,Eloquent没有看到模型中的所有字段
我是Laravel开发的新手,正在尝试创建一个由SQLite数据库支持的雄辩模型。我的本地开发服务器上有以下代码,它正确地接受请求,构建模型,并将该模型保存为callback_requests表中的一行 下面是模型定义文件和数据库迁移文件。我已经使用Php 为什么在插入数据库时,Eloquent没有看到模型中的所有字段,php,laravel,activerecord,eloquent,Php,Laravel,Activerecord,Eloquent,我是Laravel开发的新手,正在尝试创建一个由SQLite数据库支持的雄辩模型。我的本地开发服务器上有以下代码,它正确地接受请求,构建模型,并将该模型保存为callback_requests表中的一行 下面是模型定义文件和数据库迁移文件。我已经使用 php artisan migrate:reset --force php artisan migrate CallbackRequest.php namespace App\Models; use Illuminate\Database\E
php artisan migrate:reset --force
php artisan migrate
CallbackRequest.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CallbackRequest extends Model
{
public $name;
public $phone;
public $email;
public $preferredTime;
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCallbackRequestsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('callback_requests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('phone');
$table->string('email');
$table->string('preferredTime');
$table->string('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('callback_requests');
}
}
namespace App\Http\Controllers;
use App\Models\CallbackRequest;
use App\Mail\CallbackRequestMailable;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function send(Request $request)
{
$cbReq = new CallbackRequest();
$cbReq->name = $request->get('name');
$cbReq->email = $request->get('email');
$cbReq->phone = $request->get('phone');
$cbReq->preferredTime = $request->get('preferredTime');
var_dump($cbReq);
// save the entity to the database in case mail gets lost in transit
$cbReq->save();
// send an email to the address configured in the .env file
$result = Mail::to(env("MAIL_CALLBACK_DELIVERY_ADDRESS"))->send(new CallbackRequestMailable($cbReq));
}
}
创建回调请求表(迁移)。php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CallbackRequest extends Model
{
public $name;
public $phone;
public $email;
public $preferredTime;
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCallbackRequestsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('callback_requests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('phone');
$table->string('email');
$table->string('preferredTime');
$table->string('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('callback_requests');
}
}
namespace App\Http\Controllers;
use App\Models\CallbackRequest;
use App\Mail\CallbackRequestMailable;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function send(Request $request)
{
$cbReq = new CallbackRequest();
$cbReq->name = $request->get('name');
$cbReq->email = $request->get('email');
$cbReq->phone = $request->get('phone');
$cbReq->preferredTime = $request->get('preferredTime');
var_dump($cbReq);
// save the entity to the database in case mail gets lost in transit
$cbReq->save();
// send an email to the address configured in the .env file
$result = Mail::to(env("MAIL_CALLBACK_DELIVERY_ADDRESS"))->send(new CallbackRequestMailable($cbReq));
}
}
这是控制器文件,我在其中创建模型的新实例,用传入请求中的值填充它,并将其保存到DB
MailController.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CallbackRequest extends Model
{
public $name;
public $phone;
public $email;
public $preferredTime;
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCallbackRequestsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('callback_requests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('phone');
$table->string('email');
$table->string('preferredTime');
$table->string('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('callback_requests');
}
}
namespace App\Http\Controllers;
use App\Models\CallbackRequest;
use App\Mail\CallbackRequestMailable;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function send(Request $request)
{
$cbReq = new CallbackRequest();
$cbReq->name = $request->get('name');
$cbReq->email = $request->get('email');
$cbReq->phone = $request->get('phone');
$cbReq->preferredTime = $request->get('preferredTime');
var_dump($cbReq);
// save the entity to the database in case mail gets lost in transit
$cbReq->save();
// send an email to the address configured in the .env file
$result = Mail::to(env("MAIL_CALLBACK_DELIVERY_ADDRESS"))->send(new CallbackRequestMailable($cbReq));
}
}
我已经转储了$cbReq变量的内容,以确保正确添加了值,我得到的输出(如下所示)表明它们已经添加了
object(App\Models\CallbackRequest)#222 (30) {
["name"]=>
string(13) "Customer Name"
["phone"]=>
string(9) "012345678"
["email"]=>
string(14) "cust@email.com"
["preferredTime"]=>
string(7) "morning"
["connection":protected]=>
NULL
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["withCount":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(false)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(0) {
}
["original":protected]=>
array(0) {
}
["changes":protected]=>
array(0) {
}
["casts":protected]=>
array(0) {
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["appends":protected]=>
array(0) {
}
["dispatchesEvents":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["relations":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["timestamps"]=>
bool(true)
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
}
但是,当应用程序在保存时遇到错误时,会显示以下消息
{
"message": "SQLSTATE[HY000]: General error: 1364 Field 'phone' doesn't have a default value (SQL: insert into `callback_requests` (`updated_at`, `created_at`) values (2019-03-11 10:19:24, 2019-03-11 10:19:24))",
"exception": "Illuminate\\Database\\QueryException",
...
}
它从错误消息中看起来好像Eloquent不知道自定义列,并且试图只填充自动递增ID和时间戳。奇怪的是,当我在本地服务器上运行相同的代码时,一切正常,只有当代码部署到测试服务器时,我才会遇到错误
对于这个冗长的问题我深表歉意,我已经尽量少提了,但没有遗漏任何相关的内容。如果您有任何建议,我们将不胜感激。您需要从模型中删除您的属性。因此,删除所有这些:
public $name;
public $phone;
public $email;
public $preferredTime;
否则,当您设置$model->phone=$request->phone
时,它会在对象上设置此属性,而不是在模型的$attributes
数组中设置键,该数组将保存到数据库中
这是由于Laravel钩住PHP magic方法来设置模型的属性。这是基本模型类中的内容:
/**
* Dynamically set attributes on the model.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$this->setAttribute($key, $value);
}
如果IDE的IntelliSense(自动完成)具有这些属性,则仍然可以使用docblock提示动态属性:
/**
* Class CallbackRequest
*
* @property-read string name
* @property-read string phone
* @property-read string email
* @property-read string preferredTime
*/
class CallbackRequest extends Model {}
或者使用类似于自动化此步骤的软件包。感谢您的快速响应,特别是解释了问题的根本原因!那很有魅力。