![](/img/trans.png)
[英]SQLSTATE[42S02]: Base table or view not found: 1146 Table X doesn't exist
[英]SQLSTATE[42S02]: Base table or view not found: 1146 Table 'docgenerator.curricula' doesn't exist
我已經看到了類似的問題,但是沒有一個能真正幫助我。
以下是代碼:
控制器 (創建和存儲方法)
namespace App\Http\Controllers;
use App\Curriculum;
use App\Company;
use App\User;
use App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class CurriculumsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$curriculums = Curriculum::all();
return view('curriculums.index',['curriculums' =>$curriculums]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
//
$companies = Company::where('comptype_id', '=', 1)->get();
$users = DB::table('users');
//dd($companies);
return view('curriculums.create', ['companies'=>$companies,'users'=>$users]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$curriculum = $request->validate([
'curriculum_code' => 'required|unique:curriculums|max:10|min:9',
'curriculum_title' => 'required',
]);
$curriculum = Curriculum::create([
'curriculum_code' => $request->input('curriculum_code'),
]);
if ($curriculum) {
return redirect()->route('curriculums.show', ['curriculums' => $curriculum->id])
->with('success', 'Curriculum header created Successfully');
}
return back()->withInput()->with('error' , 'Curriculum header could not be created');
}
該模型
class Curriculum extends Model
{
//
protected $fillable = ['curriculum_code',
'curriculum_title',
'company_id',
'user_id',
];
public function company()
{
return $this->belongsTo('App\Company');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
刀
<form method="post" action="{{route('curriculums.store')}}">
{{csrf_field()}}
<div class="box-body">
<div class="row">
<div class="col-12">
<div class="form-group {{$errors->has('curriculum_code') ? 'has-error': ''}}">
<label for="name" class="col-sm-2 col-form-label">Curriculum Code<span class="danger">*</span> </label>
<div class="col-sm-10">
<input class="form-control" name="curriculum_code" type="text" value="" id="example-text-input">
@if($errors->has('curriculum_code'))
<span class="help-block">{{$errors->first('first')}}</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('curriculum_title') ? 'has-error': ''}}">
<label for="name" class="col-sm-2 col-form-label">Curriculum Title<span class="danger">*</span> </label>
<div class="col-sm-10">
<input class="form-control" name="curriculum_title" type="text" value="" id="example-text-input">
@if($errors->has('curriculum_title'))
<span class="help-block">{{$errors->first('first')}}</span>
@endif
</div>
</div>
<div class="form-group ">
<label for="company" class="col-sm-2 col-form-label">Development Quality Partner<span class="danger">*</span> </label>
<div class="col-sm-10">
<select class="form-control select2" style="width: 100%;" name="company_id">
@foreach($companies as $company)
<option value="{{$company->id}}">{{$company->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
</div>
<div class="text-xs-right">
<button type="submit" class="btn btn-info" value="Submit">Occupational Information</button>
</div>
</div>
</form>
我知道在嘗試將數據插入數據庫時控制器中出現問題。 我在這方面苦苦掙扎的主要原因是:數據庫不包含任何名為課程的表,控制器也不包含單詞課程。 我是laravel的新手,所以任何幫助都將不勝感激
為表Curriculum
應該叫curricula
,因為它是復數的curriculum
。
如果您使用的是其他名稱,請在模型中執行以下操作:
protected $table = 'custom_table_name';
在我的最佳實踐回購中了解有關Laravel命名約定的更多信息。
如果表名不是模型名的復數,則應在表變量中指定它,如下所示。
添加受保護的$table = 'curricula';
像這樣建模
class Curriculum extends Model
{
protected $table = 'curricula';
//
protected $fillable = ['curriculum_code',
'curriculum_title',
'company_id',
'user_id',
];
public function company()
{
return $this->belongsTo('App\Company');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.