调用模型[App \ Salarie]上未定义的关系[pointage]吗?

时间:2019-07-07 19:55:35

标签: mysql laravel database-migration

我想显示ControllerSalarie的POINTAGE和SALARY表数据,但这给了我这个错误

积分模型

public function salarie()
{
    return $this->belongsTo('App\Salarie');
}

Salarie模型

public function pointages()
{
    return $this->hasMany('App\Pointage');
}

migration migration salarie_id

 /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('pointages', function (Blueprint $table) {
            $table->integer('salarie_id')->unsigned()->after('id');
            $table->foreign('salarie_id')->references('id')->on('salaries');  
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('pointages', function (Blueprint $table) {
             $table->dropForeign(['salarie_id']);
            $table->dropCulomn('salarie_id');
        });
    }

SalarieController

public function consulter()
{      
    $salaries=Salarie::with('pointage')->get();
    $pointages = DB::table('pointages')->pluck("pointage","id")->all();
    return view('salarie.consulter', compact('salaries', 'pointages'));
}

2 个答案:

答案 0 :(得分:4)

只需将with('pointage')更改为with('pointages')

答案 1 :(得分:2)

更改:

$salaries=Salarie::with('pointage')->get();

收件人:

$salaries=Salarie::with('pointages')->get();