Php Laravel查找单数表名
当我将数据从表单发送到数据库时Php Laravel查找单数表名,php,laravel,eloquent,Php,Laravel,Eloquent,当我将数据从表单发送到数据库时 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'fypp.joblisting' doesn't exist (SQL: select count(*) as aggregate from `joblisting` where `email` = ) 发生此错误。在我的数据库中,joblistings表存在,但laravel正在查找单数名,而不是复数名。就连我也用新的迁移和其他东西创建了新的模
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'fypp.joblisting' doesn't exist (SQL: select count(*) as aggregate from `joblisting` where `email` = )
发生此错误。在我的数据库中,joblistings表存在,但laravel正在查找单数名,而不是复数名。就连我也用新的迁移和其他东西创建了新的模型,但仍然面临这个问题
模型代码
class Joblisting extends Model
{
use HasFactory;
protected $fillable = [
'title',
'category',
'company',
'email',
'level',
'number',
'description',
];
}
控制器代码
class JoblistingController extends Controller
{
public function showForm()
{
return view('frontend.pages.addjob');
}
public function createjob(Request $request)
{
$this->validate($request,[
'title'=> 'required|string',
'category'=> 'required',
'company' =>'required',
'email' => 'required|unique:joblisting',
'level' => 'required',
'number' => 'required',
'description' => 'required',
]);
Joblisting::create([
'title' => $request->title,
'category' => $request->category,
'company'=> $request->company,
'email'=> $request->email,
'level'=> $request->level,
'number'=> $request->number,
'description'=> $request->description,
]);
return redirect('viewlist')->with('success', 'job posted successfully');
}
}
迁移代码
class CreateJoblistingsTable extends Migration
{
public function up()
{
Schema::create('joblistings', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('category');
$table->string('company');
$table->string('email');
$table->string('level');
$table->string('number');
$table->string('description');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('joblistings');
}
}
Laravel使用蛇壳,因此: 修正1: 将控制器名称用作
JobListingController
修正2:
在您的模型中,添加t以下行:
protected$table='joblistings';
您必须这样使用
'email' => 'required|unique:joblistings',
快速修复方法是在模型中定义
$table='joblistings'