Home > front end >  Base table or view not found: 1146 Table 'cricbangla.batter_firsts' doesn't exist (SQ
Base table or view not found: 1146 Table 'cricbangla.batter_firsts' doesn't exist (SQ

Time:10-22

This is my database name in .env DB_DATABASE=cricbangla

This is a screenshot of my database and all table This is The Image of my database You can see there is no table called batter_firsts and i don't want any table with this name

I am doing a CRUD project in laravel-8 where i create a table name batterfirst.php

Schema::create('batterfirst', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('runs');
            $table->integer('balls');
            $table->integer('sixs');
            $table->integer('fours');
            $table->timestamps();
        });

Where my web.php is

<?php

use App\Http\Controllers\BatterFirstController;

Route::get('/', function () {
    return view('welcome');
});

Route::resource('BatterFirst', BatterFirstController::class);

Where my model name is BatterFirst.php which is

class BatterFirst extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'runs', 'balls', 'sixs', 'fours'
    ];
}

And My Controller is BatterFirstController.php which is

<?php

namespace App\Http\Controllers;

use App\Models\BatterFirst;
use Illuminate\Http\Request;

class BatterFirstController extends Controller
{

    public function index()
    {
        $data = BatterFirst::latest()->paginate(5);

        return view('BatterFirst.index',compact('data'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

    public function create()
    {
        return view('BatterFirst.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'runs' => 'required',
            'balls' => 'required',
            'sixs' => 'required',
            'fours' => 'required',
        ]);

        BatterFirst::create($request->all());

        return redirect()->route('BatterFirst.index')
                        ->with('success','Batter created successfully.');
    }

    public function show(BatterFirst $batterFirst)
    {
        return view('BatterFirst.show',compact('batterfirst'));
    }

    public function edit(BatterFirst $batterFirst)
    {
        return view('BatterFirst.edit',compact('batterfirst'));
    }

    public function update(Request $request, BatterFirst $batterFirst)
    {
        $request->validate([
            'name' => 'required',
            'runs' => 'required',
            'balls' => 'required',
            'sixs' => 'required',
            'fours' => 'required',
        ]);

        $batterFirst->update($request->all());

        return redirect()->route('BatterFirst.index')
                        ->with('success','Batter updated successfully');
    }

    public function destroy(BatterFirst $batterFirst)
    {
        $batterFirst->delete();

        return redirect()->route('BatterFirst.index')
                        ->with('success','Batter deleted successfully');
    }
}

This is my index.php file inside BatterFirst Folder BatterFirst/index.blade.php


@extends('BatterFirst.layout')

@section('content')
    <div class="row" style="margin-top: 5rem;">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Laravel 8 CRUD Example from scratch - laravelcode.com</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('BatterFirst.create') }}"> Create New Post</a>
            </div>
        </div>
    </div>

    @if ($message = Session::get('success'))
        <div hljs-string">">
            <p>{{ $message }}</p>
        </div>
    @endif

    <table hljs-string">">
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>Runs</th>
            <th>Balls</th>
            <th>Sixs</th>
            <th>Fours</th>
            <th>Strick Rate</th>
        </tr>
        @foreach ($data as $key => $value)
        <tr>
            <td>{{   $i }}</td>
            <td>{{ $value->name }}</td>
            <td>{{ $value->runs }}</td>
            <td>{{ $value->overs }}</td>
            <td>{{ $value->balls }}</td>
            <td>{{ $value->sixs }}</td>
            <td>{{ $value->fours }}</td>
            {{-- <td>{{ $value->runs/$value->balls*100 }}</td> --}}
            <td>@if ($value->runs > 0 and $value->runs ==0)
                    {{ $value->runs*100 }}
                @elseif ($value->balls>0 and $value->runs ==0)
                    {{ $value->balls*$value->runs }}
                @elseif ($value->balls==0 and $value->runs ==0)
                    {{ $value->balls *  $value->runs }}
                @elseif ($value->runs>0 and $value->balls>=0)
                    {{ $value->runs/$value->balls*100 }}
                @endif
            </td>
            <td>
                <form action="{{ route('BatterFirst.destroy',$value->id) }}" method="POST">
                    <a hljs-string">" href="{{ route('BatterFirst.show',$value->id) }}">Show</a>
                    <a hljs-string">" href="{{ route('BatterFirst.edit',$value->id) }}">Edit</a>
                    @csrf
                    @method('DELETE')
                    <button type="submit" hljs-string">">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
    {!! $data->links() !!}
@endsection

This is my BatterFirst/create.blade.php file


@extends('BatterFirst.layout')

@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New Product</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('BatterFirst.index') }}"> Back</a>
        </div>
    </div>
</div>

@if ($errors->any())
    <div hljs-string">">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<form action="{{ route('BatterFirst.store') }}" method="POST">
    @csrf

    <div hljs-string">">
        <div hljs-number">12 col-sm-12 col-md-12">
            <div hljs-string">">
                <strong>Name:</strong>
                <input type="text" name="name" hljs-string">" placeholder="Enter Name">
            </div>
        </div>
        <div hljs-number">12 col-sm-12 col-md-12">
            <div hljs-string">">
                <strong>Runs:</strong>
                <input type="number" name="runs" hljs-string">" placeholder="Enter Runs">
            </div>
        </div>
        <div hljs-number">12 col-sm-12 col-md-12">
            <div hljs-string">">
                <strong>Balls:</strong>
                <input type="number" name="balls" hljs-string">" placeholder="Enter Balls">
            </div>
        </div>
        <div hljs-number">12 col-sm-12 col-md-12">
            <div hljs-string">">
                <strong>Sixs:</strong>
                <input type="number" name="sixs" hljs-string">" placeholder="Enter Sixs">
            </div>
        </div>
        <div hljs-number">12 col-sm-12 col-md-12">
            <div hljs-string">">
                <strong>Fours:</strong>
                <input type="number" name="fours" hljs-string">" placeholder="Enter Fours">
            </div>
        </div>
        <div hljs-number">12 col-sm-12 col-md-12 text-center">
                <button type="submit" hljs-string">">Submit</button>
        </div>
    </div>

</form>
@endsection

I don`t know why this problem is showing. Note: I just started to learn laravel

CodePudding user response:

Its looking for the correct named table, since you have it in camel it should be batter_first you can change this via the model below;

class BatterFirst extends Model
{
    use HasFactory;
    
    protected $table = 'batterfirst';

    protected $fillable = [
        'name', 'runs', 'balls', 'sixs', 'fours'
    ];
}
  • Related