[英]Cannot do autocomplete on laravel 8 - Base table or view not found: 1146 Table 'workshop.nama_suppliers' doesn't exist
When I tried to do autocomplete on Laravel 8, the error is like this when i do inspection in my browser当我尝试在 Laravel 8 上进行自动完成时,当我在浏览器中进行检查时,错误是这样的
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'workshop.nama_suppliers' doesn't exist (SQL: select
Supplier
.namasupplier
fromnama_suppliers
whereSupplier
.namasupplier
LIKE %abcd%)SQLSTATE [42S02]:未找到基表或视图:1146 表“workshop.nama_suppliers”不存在(SQL:select
Supplier
。来自namasupplier
的供应商namasupplier
其中Supplier
nama_suppliers
LIKE %abcd%)
I've already do php artisan migrate and this error still occurs.我已经做了 php artisan migrate 并且这个错误仍然发生。 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".
即使我根本没有在我的代码中键入 nama_suppliers 表,因为在我的数据库中没有名为“nama_suppliers”的表。
And this is my codes I've made:这是我制作的代码:
/database/migrations/2021_05_16_142836_create__supplier_table.php /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应用程序/模型/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 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路线/web.php
Route::get('getnamasupplier', [homeController::class, 'getnamasupplier'])->name('getnamasupplier');
resources/views/tambah_transaksimasuk.blade.php资源/视图/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:注意:对于 jquery,我将此刀片用于此刀片:
<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?你能解释一下为什么会发生这种情况,我应该怎么做?
Add protected $table in your Model, change your model into在 Model 中添加受保护的 $table,将 model 更改为
<?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对于下一个开发,考虑使用复数 (kata benta jamak) 作为表名和单数 (kata benta tunggal) 作为模型名
eg: 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例如:对于表供应商,使用
suppliers
作为表名(小写)并使用Supplier
作为 model 名称,如果这样做,则不需要在 model 中添加受保护的 $table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.