无法在 laravel 8 上执行自动完成 - 基础 table 或未找到视图:1146 Table 'workshop.nama_suppliers' 不存在
Cannot do autocomplete on laravel 8 - Base table or view not found: 1146 Table 'workshop.nama_suppliers' doesn't exist
当我尝试在 Laravel 8 上执行自动完成时,在我的浏览器中进行检查时出现这样的错误
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%)
我已经进行了 php artisan 迁移,但仍然出现此错误。即使我根本不在我的代码中输入 nama_suppliers table 因为在我的数据库中没有名为“nama_suppliers”的 table。
这是我编写的代码:
/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>
注意:jquery,我用这个 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>
你能解释一下为什么会这样吗?我应该怎么做?
在您的模型中添加受保护的$table,将您的模型更改为
<?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'
];
}
为了下一步的发展,考虑使用复数 (kata benda jamak) 作为 table 的名字,并使用单数 (kata benda tunggal) 作为模特的名字
例如:对于 table 供应商,使用 suppliers
作为 table 名称(小写)并使用 Supplier
作为型号名称,如果您这样做,您就赢了不需要在模型中添加受保护的 $table
当我尝试在 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%)
我已经进行了 php artisan 迁移,但仍然出现此错误。即使我根本不在我的代码中输入 nama_suppliers table 因为在我的数据库中没有名为“nama_suppliers”的 table。
这是我编写的代码:
/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>
注意:jquery,我用这个 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>
你能解释一下为什么会这样吗?我应该怎么做?
在您的模型中添加受保护的$table,将您的模型更改为
<?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'
];
}
为了下一步的发展,考虑使用复数 (kata benda jamak) 作为 table 的名字,并使用单数 (kata benda tunggal) 作为模特的名字
例如:对于 table 供应商,使用 suppliers
作为 table 名称(小写)并使用 Supplier
作为型号名称,如果您这样做,您就赢了不需要在模型中添加受保护的 $table