简体   繁体   中英

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

Make sure about testimonials table exists in your xyz DB. If you've created table with another name then you've to define it in the model.

Let say, You've taken a table name as testimonial. then in your model, the protected field will be,

class Testimonial extends Model
{
    protected $table = 'testimonial';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM