首页 > 解决方案 > 未找到基表或视图:1146 表“cricbangla.batter_firsts”不存在(SQL:选择计数(*)作为来自“batter_firsts”的聚合)-Laravel-8

问题描述

这是我在 .env DB_DATABASE=cricbangla 中的数据库名称

这是我的数据库和所有表的屏幕截图 这是我的数据库 的图像 您可以看到没有名为batter_firsts 的表,我不想要任何具有此名称的表

我在 laravel-8 中做一个 CRUD 项目,在那里我创建了一个表名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();
        });

我的 web.php 在哪里

<?php

use App\Http\Controllers\BatterFirstController;

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

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

我的模型名称是 BatterFirst.php,它是

class BatterFirst extends Model
{
    use HasFactory;

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

我的控制器是 BatterFirstController.php,它是

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

这是我在 BatterFirst 文件夹 BatterFirst/index.blade.php 中的 index.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 class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif

    <table class="table table-bordered">
        <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 class="btn btn-info" href="{{ route('BatterFirst.show',$value->id) }}">Show</a>
                    <a class="btn btn-primary" href="{{ route('BatterFirst.edit',$value->id) }}">Edit</a>
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
    {!! $data->links() !!}
@endsection

这是我的 BatterFirst/create.blade.php 文件


@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 class="alert alert-danger">
        <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 class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                <input type="text" name="name" class="form-control" placeholder="Enter Name">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Runs:</strong>
                <input type="number" name="runs" class="form-control" placeholder="Enter Runs">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Balls:</strong>
                <input type="number" name="balls" class="form-control" placeholder="Enter Balls">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Sixs:</strong>
                <input type="number" name="sixs" class="form-control" placeholder="Enter Sixs">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Fours:</strong>
                <input type="number" name="fours" class="form-control" placeholder="Enter Fours">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>

</form>
@endsection

我不知道为什么会出现这个问题。注意:我刚开始学习laravel

标签: laravellaravel-8

解决方案


它正在寻找正确的命名表,因为你有它在骆驼它应该是batter_first你可以通过下面的模型来改变它;

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

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

推荐阅读