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

I have a problem with php artisan tinker, can't find the reason why he wants a 'businesses' table but not a 'business' table. Help me fix mistakes, I feel I did a lot of them) My Problem :

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

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

My Controller: 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');

    }
    }

My Model: 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');

    }
    }

My view file: business.blade.php

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

Image my error: my error



Solution 1:[1]

You can fix this by adding a table name in the model.

In the Business.php model:

By default Laravel tries to fetch the plural name of a model. So Business will be businesss.

Internally Laravel checked:

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

Best practice for creating a migration and model

php artisan modelName -m ----> make sure modelName is singular

In this way, Laravel automatically creates a plural version of a migration for you.


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

Here you are returning a collection of business. This means multiple rows of a table.
So you cannot use $business->name because business has multiple rows.

@extends('layouts.layout')
@section('title')??????@endsection
@section ('main_content')
    <h1>??????</h1>
    <p>
        @foreach ($business as $singleBusiness)
        <li>{{ $singleBusiness->title}}</li>>
        @endforeach
    </p>
@endsection

Solution 2:[2]

Laravel expect the database table name to be the plural version of the model.

In this case, your business model becomes a table of businesses.

Look here for more information.

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


You have two options to solve this.

Specify the table name you want to use in your business model.

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

Or change your migration to follow Laravel practices and create a businesses 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');
    }
}

Solution 3:[3]

Try to specify the table inside your model

 protected $table = 'business';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James Martinez
Solution 2
Solution 3 ml59