php - Base table or view not found: 1146 Table 'xyz.testimonials' doesn't exist (SQL: select * from `testimonials`)
问题描述
[SOLVED] I'm not a pro on Laravel. Just started with web development. I was asked to make changes on an existing project which I downloaded from c Panel. On the server, the project was working fine. But after downloading it I'm getting the following error and not quite sure what's going on.
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'xyz.testimonials' doesn't exist (SQL: select * from
testimonials
)
After downloading the project I can the following
php artisan cache:clear
composer update
php artisan migrate
php artisan db:seed
The following the TestimonialController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Testimonial;
class TestimonialController extends Controller
{
public function index()
{
$testimonials = Testimonial::all();
return view('dashboard.testimonials.index')->withTestimonials($testimonials);
}
public function create()
{
return view('dashboard.testimonials.create');
}
public function store(Request $request)
{
$request->validate(['testimonial_text'=>'required']);
$testimonial = Testimonial::create($request->all());
if($testimonial)
{
$this->success('Testimonial added successfully');
}
else
{
$this->error();
}
return redirect()->back();
}
public function edit(Testimonial $testimonial)
{
return view('dashboard.testimonials.edit')->withTestimonial($testimonial);
}
public function update(Testimonial $testimonial,Request $request)
{
if($testimonial->update($request->all()))
{
$this->success('Testimonial Updated Successfully');
}
else
{
$this->error();
}
return redirect()->route('dashboard.testimonials.index');
}
public function destroy(Testimonial $testimonial)
{
if($testimonial->delete())
{
$this->success('Testimonial Deleted Successfully');
}
else
{
$this->error();
}
return redirect()->back();
}
}
Testimonial.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Testimonial extends Model
{
public $guarded = [];
public function allTestimonials()
{
return self::all();
}
}
解决方案
There is two way of table define in Laravel.
- model class name (testimonial) = singular and table
name(testimonials) = plural , Kindly please check if
testimonials
is available or not in your database. whenever you don't define$table
into model, it means Laravel automatic search for table. you have to manually add
$table
into Model fileas below. whenever you are not
creating table name in plural form as following first rule.protected $table = 'testimonial';
推荐阅读
- c++ - 是否可以使用 gcc/g++ 从 Windows 编译到 Linux?
- three.js - 如何在 Autodesk Forge 中对缓冲区几何对象进行分组
- reactjs - 使用功能组件反应条件重新渲染不起作用
- python - 如何在我的 python beautifulsoup 代码片段中获取前 10 个表数据
- java - 从 Java 运行 .vbs 文件
- sorting - Dart中的非变异排序列表
- regex - 正则表达式匹配列分隔的字符串,除非它以连字符开头
- c# - 如何从 .net 运行长 SQL 脚本?
- angular - Safari(14)在单击任何超链接时触发“popstate”和“hashchange”,并在 Angular 11 中触发 2 个路由更改
- reactjs - 为什么我不能在 React 中读取 Formik 的下拉值?