我的代碼是用 PHP 撰寫的,使用 Laravel 和 Postgresql 作為資料庫。
我有一個模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
use HasFactory;
protected $fillable = [
'last_name',
'first_name',
'phone_number',
'address',
];
//protected $table = 'database.contact';
protected $hidden = [
'remember_token',
];
}
和遷移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Contact extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact ',function(Blueprint $table){
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->integer('phone_number');
$table->ipAddress('address')->nullable();
$table->rememberToken();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact');
}
}
但是當我在終端中運行時:
php artisan tinker
然后:
Contact::create(['last_name' => 'joe', 'first_name' => 'ajoe.com', 'phone_number' => 88552])
我收到錯誤:
Illuminate\Database\QueryException with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: 關系“contacts”不存在第 1 行:插入“contacts”(“last_name”、“first_name”、“phone_nu... ^( SQL:插入“聯系人”(“last_name”、“first_name”、“phone_number”、“updated_at”、“created_at”)值(joe, ajoe.com, 88552, 2021-11-06 21:00:43, 2021 -11-06 21:00:43) 回傳“id”)'
如果您知道為什么會發生這種情況,我很樂意得到它,謝謝。
uj5u.com熱心網友回復:
在模型中添加這一行
protected $table = 'contact';
uj5u.com熱心網友回復:
第一步:從遷移中的表名中洗掉多余的空格:
Schema::create('contact ',function(Blueprint $table){
到:
Schema::create('contact',function(Blueprint $table){
然后,在您的 Contact 模型中,設定 $table 屬性:
class Contact extends Model
{
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'contact';
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351393.html
標籤:php 拉拉维尔 PostgreSQL 模型 修补匠
