Search code examples
laravellaravel-artisanlaravel-migrationstinker

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

  • 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