![](/img/trans.png)
[英]SQLSTATE[42S02]: Base table or view not found: 1146 Table X doesn't exist
[英]SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sathish_company.clients' doesn't exist
这发生在 laravel FWK-9 中。 我在我的 Mysql 服务器中创建了一个名为“client”的数据库,并在 .env conf 文件中正确配置了数据库,并且它已连接。
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={sathish_company}
DB_USERNAME={username}
DB_PASSWORD={pass}
表名是client
但显示的错误是'sathish_company.clients' doesn't exist
,数据库表名应该是'sathish_company.client'
,我不知道's'
在最后自动添加的位置。 我认为's'
是问题所在。 我将表名从client
更改为clients
以测试s
问题,它运行良好,但是s
是如何添加的,从哪里添加? 谁能告诉我这有什么问题以及如何解决这个问题?
laravel-9 中的错误如下。
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sathish_company.clients' doesn't exist
select * from `clients`
下面的 controller Class 调用 model。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Client;
class CompanyController extends Controller
{
public function show($client) {
$client = Client::all();
return view('client',['client'=>$client]);
}
}
下面的 model class 由 controller 调用。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
use HasFactory;
}
迁移表如下。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('client', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('project_name');
$table->integer('amount');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('client');
}
};
我已经尝试了基于此 [42S02] SQL 错误在论坛中的所有解决方案,没有什么能像PHP artisan migrate:refresh-seed and PHP artisan migrate
将此添加到您的 model protected $table = 'your_table_name';
然后再试一次
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.