问题 'SQLSTATE[42S02]: Base table or view not found

Problem 'SQLSTATE[42S02]: Base table or view not found

我对 php artisan tinker 有意见,找不到他想要 'businesses' table 而不是 'business' 的原因table。 帮我修正错误,我觉得我犯了很多错误) 我的问题:

Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.businesses' doesn't exist (SQL: select * from `businesses`)'

我的数据库business_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class BusinessTable extends Migration
{
    /**
    * Выполнение миграций.
    *
    * @return void
    */
    public function up()
    {
    Schema::create('business', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('mail');
    $table->string('web-site');
    $table->timestamps();
    });
    }

    /**
    * Отмена миграций.
    *
    * @return void
    */
    public function down()
    {
    Schema::drop('business');
    }
}

我的控制器:BusinessController.php

<?php

    namespace App\Http\Controllers;
    use \App\Models\Business;
    use Illuminate\Http\Request;

    class BusinessController extends Controller
    {
    public function index()
    {
    $business = \App\Models\Business::all();
    return view('business.index', compact('business'));
    }
    public  function store()
    {
    $business = new Business();
    $business->title = request()->input('title');
    $business->description = request()->input('description');
    $business->save();
    return redirect('/business');

    }
    }

我的模特:Business.php

<?php

    namespace App\Http\Controllers;
    use \App\Models\Business;
    use Illuminate\Http\Request;

    class BusinessController extends Controller
    {
    public function index()
    {
    $business = \App\Models\Business::all();
    return view('business.index', compact('business'));
    }
    public  function store()
    {
    $business = new Business();
    $business->title = request()->input('title');
    $business->description = request()->input('description');
    $business->save();
    return redirect('/business');

    }
    }

我的查看文件:business.blade.php

@extends('layouts.layout')
@section('title')Бізнес@endsection
@section ('main_content')
    <h1>Бизнес</h1>
    <p>
        <li>{{ $business->title}}</li>>
    </p>
@endsection

想象一下我的错误: my error

Laravel 期望数据库 table 名称是模型的复数形式。

在这种情况下,您的 business 模型变为 businesses 的 table。

查看此处了解更多信息。

https://laravel.com/docs/8.x/eloquent#table-names


你有两种选择来解决这个问题。

指定您要在业务模型中使用的 table 名称。

class Business extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'business';
}

或更改迁移以遵循 Laravel 做法并创建业务 table。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class BusinessTable extends Migration
{
    /**
    * Выполнение миграций.
    *
    * @return void
    */
    public function up()
    {
    Schema::create('businesses', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('mail');
    $table->string('web-site');
    $table->timestamps();
    });
    }

    /**
    * Отмена миграций.
    *
    * @return void
    */
    public function down()
    {
    Schema::drop('business');
    }
}

尝试在模型中指定 table

 protected $table = 'business';

您可以通过在模型中添加 table 名称来解决此问题。

Business.php模型中:

默认情况下 Laravel 尝试获取模型的 plural name。所以 Business 将是 businesss.

内部 Laravel 检查:

Str::plural('business') == "businesses"

class Business extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'business';
}

ref link https://laravel.com/docs/8.x/eloquent#table-names

创建迁移和模型的最佳实践

php artisan modelName -m ----> 确保模型名称是单数

这样,Laravel会自动为您创建复数版本的迁移。


 public function index()
 {
    $business = \App\Models\Business::all();
    return view('business', compact('business'));
 }

这里您要返回 business 的集合。这意味着 table.
的多行 所以你不能使用 $business->name 因为 business 有多行。

@extends('layouts.layout')
@section('title')Бізнес@endsection
@section ('main_content')
    <h1>Бизнес</h1>
    <p>
        @foreach ($business as $singleBusiness)
        <li>{{ $singleBusiness->title}}</li>>
        @endforeach
    </p>
@endsection