Php 未定义的属性:illumb\Database\elount\Relations\BelongsTo::$diffi\u lvl\u name

Php 未定义的属性:illumb\Database\elount\Relations\BelongsTo::$diffi\u lvl\u name,php,laravel,Php,Laravel,在创建关系时,Laravel 5.7中出现错误,我创建的关系为:Question.php: public function diffi_lvl() { return $this->belongsTo('App\DifficultyLevel'); } public function questions() { return $this->hasMany('App\Question'); } public function index() { $questi

在创建关系时,Laravel 5.7中出现错误,我创建的关系为:Question.php:

public function diffi_lvl() {
    return $this->belongsTo('App\DifficultyLevel');
}
public function questions() {
    return $this->hasMany('App\Question');
}
public function index()
{
    $questions = Question::all();
    return view('questions.index', compact('questions'));
}
public function up()
{
    Schema::create('difficulty_levels', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('diffi_lvl_name');
        $table->timestamps();
    });
}
public function up()
{
    Schema::create('questions', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->text('question');
        $table->unsignedInteger('difficulty_level_id');
        $table->foreign('difficulty_level_id')->references('id')->on('difficulty_levels');
        $table->timestamps();
    });
}
DifficultyLevel.php:

public function diffi_lvl() {
    return $this->belongsTo('App\DifficultyLevel');
}
public function questions() {
    return $this->hasMany('App\Question');
}
public function index()
{
    $questions = Question::all();
    return view('questions.index', compact('questions'));
}
public function up()
{
    Schema::create('difficulty_levels', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('diffi_lvl_name');
        $table->timestamps();
    });
}
public function up()
{
    Schema::create('questions', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->text('question');
        $table->unsignedInteger('difficulty_level_id');
        $table->foreign('difficulty_level_id')->references('id')->on('difficulty_levels');
        $table->timestamps();
    });
}
之后,我使用:

@foreach ($questions as $question)
    {{ dd($question->diffi_lvl()->diffi_lvl_name) }}
@endforeach
QuestionController.php:

public function diffi_lvl() {
    return $this->belongsTo('App\DifficultyLevel');
}
public function questions() {
    return $this->hasMany('App\Question');
}
public function index()
{
    $questions = Question::all();
    return view('questions.index', compact('questions'));
}
public function up()
{
    Schema::create('difficulty_levels', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('diffi_lvl_name');
        $table->timestamps();
    });
}
public function up()
{
    Schema::create('questions', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->text('question');
        $table->unsignedInteger('difficulty_level_id');
        $table->foreign('difficulty_level_id')->references('id')->on('difficulty_levels');
        $table->timestamps();
    });
}
难度级迁移:

public function diffi_lvl() {
    return $this->belongsTo('App\DifficultyLevel');
}
public function questions() {
    return $this->hasMany('App\Question');
}
public function index()
{
    $questions = Question::all();
    return view('questions.index', compact('questions'));
}
public function up()
{
    Schema::create('difficulty_levels', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('diffi_lvl_name');
        $table->timestamps();
    });
}
public function up()
{
    Schema::create('questions', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->text('question');
        $table->unsignedInteger('difficulty_level_id');
        $table->foreign('difficulty_level_id')->references('id')->on('difficulty_levels');
        $table->timestamps();
    });
}
和迁移问题:

public function diffi_lvl() {
    return $this->belongsTo('App\DifficultyLevel');
}
public function questions() {
    return $this->hasMany('App\Question');
}
public function index()
{
    $questions = Question::all();
    return view('questions.index', compact('questions'));
}
public function up()
{
    Schema::create('difficulty_levels', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('diffi_lvl_name');
        $table->timestamps();
    });
}
public function up()
{
    Schema::create('questions', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->text('question');
        $table->unsignedInteger('difficulty_level_id');
        $table->foreign('difficulty_level_id')->references('id')->on('difficulty_levels');
        $table->timestamps();
    });
}
但它给了我这个错误作为

未定义的属性:illumb\Database\elounce\Relations\BelongsTo:$diffi\u lvl\u name

注意:我也使用了这个
{dd($question->diffi\u lvl->diffi\u lvl\u name)}
获取试图获取非对象的属性


您已使用修复了第一个错误

$question->diffi_lvl->diffi_lvl_name
对于第二个错误,有一些可能性

  • 您的问题模型没有任何困难级别
  • 我们必须查看您的表结构。可能您错误地映射了外键。您的外键是否在“问题”表中命名为“难度\级别\ id”

按以下方式更改您的关系:

public function diffi_lvl() {
    return $this->belongsTo('App\DifficultyLevel', 'difficulty_level_id');
}

public function questions() {
    return $this->hasMany('App\Question', 'difficulty_level_id');
}
然后,以下各项应起作用:

@foreach ($questions as $question)
    {{ dd($question->diffi_lvl->diffi_lvl_name) }}
@endforeach
有关更多信息,请阅读上的文档

编辑:实际上,似乎只要做以下几点就可以了:

public function diffi_lvl() {
    return $this->belongsTo('App\DifficultyLevel', 'difficulty_level_id');
}

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

你能给我们看看你的模型吗?是的,我现在编辑了chk谢谢。我回答了。我认为你在做错误的关系,因为问题属于1个不同级别,难度级别有很多问题,不是吗?顺便说一句,感谢你的回答,它现在工作得很有魅力,你能告诉我在关系中添加
难度级别id
的作用是什么吗???既然你将方法名称定义为
diffi\u lvl
,Laravel将尝试在您的表中查找不存在的
diffi\u lvl\u id
。因此,您必须显式定义另一个外键。文档中提到了这一点。好运:)不需要更改关系,只需添加外键,因为问题属于1个困难级别,难度级别有很多问题,不是吗?关系很好。您只需要指定外键yes。