繁体   English   中英

Laravel 8 - 未找到基表或视图:1146 表“laravel8.brand”不存在

[英]Laravel 8 - Base table or view not found: 1146 Table 'laravel8.brand' doesn't exist

我已经搜索 web 很长时间了,我尝试添加protected $table = "brands"; 我的品牌 Model 如下所示:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Brand extends Model
{
    protected $table = "brands";
    use HasFactory;
    protected $fillable = [
        'brand_name',
        'brand_image'
    ];
}

然后在我的 controller 我有以下代码:

<?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!');
    }
}

通过我的路线传递:

// For Brand Route
Route::get('/brand/all', [BrandController::class, 'AllBrand'])->name('all.brand');

Route::post('/brand/add', [BrandController::class, 'StoreBrand'])->name('store.brand');

我还在views/admin/brand folder中的表单上使用了它:

<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

我的数据库表名为brands ,但添加了protected $table = "brands"; 没有解决这个问题。 我什至尝试重新迁移并重新执行此操作,但似乎根本没有任何效果。

有没有办法解决这个问题? 我不断收到SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.brand' doesn't exist (SQL: select count(*) as aggregate from brand where brand_name = Bx LLC)错误添加一个新品牌。

请帮忙!

问题不在于存储品牌,而在于验证:

在您的验证中:

'brand_name' => 'required|unique:brand|min:4',

这告诉 Laravel 确保“品牌”表中的品牌名称唯一性,因此当查询提交时,您会看到该错误。

此验证应为:

'brand_name' => 'required|unique:brands|min:4',

您可以指定 Eloquent model 来确定表名,而不是直接指定表名:

  'brand_name' => 'required|unique:App\Models\Brand|min:4',

参考: https://laravel.com/docs/8.x/validation#rule-unique

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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