Cannot do autocomplete on laravel 8 - Base table or view not found: 1146 Table 'workshop.nama_suppliers' doesn't exist

Viewed 347

When I tried to do autocomplete on Laravel 8, the error is like this when i do inspection in my browser

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'workshop.nama_suppliers' doesn't exist (SQL: select Supplier.namasupplier from nama_suppliers where Supplier.namasupplier LIKE %abcd%)

I've already do php artisan migrate and this error still occurs. Even though I don't do type nama_suppliers table at all in my code because in my database there is no table named "nama_suppliers".

And this is my codes I've made :

/database/migrations/2021_05_16_142836_create__supplier_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSupplierTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(Schema::hasTable('Supplier')) return;
        Schema::create('Supplier', function (Blueprint $table) {
            $table->id();
            $table->string('namasupplier');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('Supplier');
    }
}

app/Models/NamaSupplier.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class NamaSupplier extends Model
{
    use HasFactory;

    protected $fillable = [
        'namasupplier'
    ];
}

app/Http/Controllers/homeController.php

public function getnamasupplier(Request $request)
{
    $data = NamaSupplier::select("Supplier.namasupplier")
            ->where("Supplier.namasupplier","LIKE","%{$request->input('query')}%")
            ->get();
   
    return response()->json($data);
}

routes/web.php

Route::get('getnamasupplier', [homeController::class, 'getnamasupplier'])->name('getnamasupplier');

resources/views/tambah_transaksimasuk.blade.php

<div class="col-md-10 themed-grid-col">
    Supplier     <br>
    <div class="form-floating mb-3">
        <input type="text" class="nsup" name="namasupplier" value=""><br></br>
    </div>
        <script type="text/javascript">
            var path = "{{ route('getnamasupplier') }}";
            $('input.nsup').typeahead({
                source:  function (query, process) {
                return $.get(path, { query: query }, function (data) {
                        return process(data);
                    });
                }
            });
        </script>
</div>

Note : for jquery, I used this for this blade :

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>

Can you explain why this happened and what solution should I do?

1 Answers

Add protected $table in your Model, change your model into

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class NamaSupplier extends Model
{
    use HasFactory;

    protected $table = 'Supplier';
    protected $fillable = [
        'namasupplier'
    ];
}

For the next development, consider using plural (kata benda jamak) as table's name and singular (kata benda tunggal) as model's name

e.g : for table supplier, use suppliers as table name (lowered case) and use Supplier as model name, if you do this, you won't need to add protected $table within your model

Related