首页 > 解决方案 > 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();
    }
}

标签: phpmysqllaravel

解决方案


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';


推荐阅读