未找到基表或视图:1146 表 'laravel8.brand' 不存在(SQL:选择计数(*)

时间:2021-05-27 07:26:52

标签: laravel

我已经迁移了我的数据库:

enter image description here

如您所见,添加了 brands

在我的品牌控制器上,我得到了这个:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Brand;
use Illuminate\Support\Carbon;

class BrandController extends Controller
{
    public function AllBrand(){
        $brands = Brand::latest()->paginate(2);
        return view('admin.brand.index', compact('brands'));
    }

    public function StoreBrand(Request $request){
        $validatedData = $request->validate([
            'brand_name' => 'required|unique:brand|min:4',
            'brand_image' => 'required|mimes:jpg,jpeg,png',
        ],
        [
            'brand_name.required' => 'Please input brand name',
            'brand_image.min' => 'Brand longer than 4 Characters'
        ]);

        $brand_image = $request->file('brand_image');
        $name_gen = hexdec(uniqid());
        $img_ext = strtolower($brand_image->getClientOriginalExtension());
        $img_name = $name_gen.'.'.$img_ext;
        $upload_location = 'images/brand/';
        $publish_image =  $upload_location.$img_name;
        $brand_image->move($upload_location,$img_name);

        Brand::insert([
            'brand_name' => $request->brand_name,
            'brand_image' => $publish_image,
            'created_at' => Carbon::now()
        ]);

        return Redirect()->back()->with('success', 'Brand added successfully!');
    }
}

然后在我的 views/admin/brand/index.blade.php 中:

  <form action="{{ route('store.brand') }}" method="POST" enctype="multipart/form-data">
    @csrf
  <input type="text" name="brand_name" placeholder="Brand name"/>
  @error('brand_name')
  <span style="color: red; font-weight: bold;"> {{ $message }}</span>
  @enderror

  <h3>Brand Image</h3>

  <input type="file" name="brand_image"/>
  @error('brand_image')
  <span style="color: red; font-weight: bold;"> {{ $message }}</span>
  @enderror
  <br/>
  <br/>
  
  <button type="submit">Add Brand</button>
  </form>

但是当我尝试添加品牌时,它向我显示了以下错误:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.brand' doesn't exist (SQL: select count(*) as aggregate from brandwherebrand_name = Practice_Bom)

不确定是什么原因造成的,但我很确定我已经迁移了它,如下图所示。

知道我在这里遗漏了什么吗?

1 个答案:

答案 0 :(得分:1)

也许您在模型中设置了表名,将其设置为复数:

class Brand extends Model
{
    protected $table = 'brands';
    ...
}