【问题标题】:Call to undefined relationship [pointage] on model [App\Salarie]?调用模型 [App\Salarie] 上的未定义关系 [pointage]?
【发布时间】:2019-11-17 10:05:41
【问题描述】:

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

积分模型

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

薪酬模式

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

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

工资控制员

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

【问题讨论】:

  • 很好奇,为什么我的答案被接受了,然后又不被接受了……这是同一个答案,而且比一个多小时前就来了……?!

标签: mysql laravel database-migration


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    改变:

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

    到:

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

    【讨论】:

    • 对不起,我知道你的答案是第一位的,但是我看到了Ahmed的答案,我编辑了代码,所以请注意其他答案,谢谢兄弟:)
    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2019-12-01
    • 2020-01-28
    • 2020-06-20
    • 1970-01-01
    • 2022-08-02
    相关资源
    最近更新 更多