SQLSTATE [42S02]:未找到基表或视图:1146表' docgenerator.curricula'不存在

时间:2018-01-21 13:49:00

标签: php mysql laravel

我已经看到了与我遇到的问题类似的问题,但他们都没有真正帮助过我。

以下是代码:

控制器(创建和存储方法)

 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的新手,所以任何帮助都将不胜感激

2 个答案:

答案 0 :(得分:5)

Curriculum的表格应该被称为curricula,因为它是curriculum的复数形式。

如果您使用其他名称,请在模型中执行以下操作:

protected $table = 'custom_table_name';

在我的最佳做法回购中详细了解Laravel naming conventions

答案 1 :(得分:3)

如果表名不是模型名的复数,则应在表变量中指定它,如下所示。

将这样的受保护$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');
    }
}