简体   繁体   中英

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'docgenerator.curricula' doesn't exist

I've seen similar questions to problem I have, but none of them really assisted me.

Below is the code:

The controller (The create and store methods)

 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');
    }

The Model

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');
    }
}

Blade

<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>

I understand that the problem occurs in the controller when trying to insert data into the database. The main reason I'm struggling so much with this is: The database doesn't contain any table called curricula, and neither does the controller contain the word curricula. I am new to laravel, so any help will be greatly appreciated

The table for the Curriculum should be called curricula because it's plural for curriculum .

If you're using a different name, do this in the model:

protected $table = 'custom_table_name';

Learn more about Laravel naming conventions in my best practices repo.

if the table name is not the plural of the model name you should specify it in table variable like this.

add protected $table = 'curricula'; like this to model

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');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM