【问题标题】:Problem 'SQLSTATE[42S02]: Base table or view not found问题 'SQLSTATE[42S02]: 未找到基表或视图
【发布时间】:2021-04-01 22:34:00
【问题描述】:

我对 php artisan tinker 有疑问,找不到他想要“businesses”表而不是“business”表的原因。 帮我改正错误,我觉得我做了很多) 我的问题:

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 laravel-artisan laravel-migrations tinker


【解决方案1】:

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

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

参考链接https://laravel.com/docs/8.x/eloquent#table-names

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

php artisan modelName -m ----> 确保 modelName 是单数

这样laravel会自动为你创建复数版本的migration


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

在这里,您返回 business 的集合表示多行表格
所以你不能使用$business-&gt;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

【讨论】:

  • 谢谢,你还能告诉我如何正确显示文件 business.blade.php 中的数据库,因为我有一个错误
  • @Mineral 看看我也添加了这些
  • 你需要修复这个view('business', compact('business'));
  • 对不起,我不明白怎么了
  • 这是你的控制器问题
【解决方案2】:

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

在这种情况下,您的business 模型变成了businesses 的表。

查看这里了解更多信息。

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


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

指定要在业务模型中使用的表名。

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

或者更改您的迁移以遵循 Laravel 实践并创建一个业务表。

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

【讨论】:

    【解决方案3】:

    尝试在模型中指定表格

     protected $table = 'business';
    

    【讨论】:

      猜你喜欢
      • 2018-03-11
      • 2014-02-09
      • 2020-11-24
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多