【问题标题】:Creating Simple Quiz Logic in Laravel在 Laravel 中创建简单的测验逻辑
【发布时间】:2016-08-17 11:20:42
【问题描述】:

这些是我的架构表

Schema::create('question', function(Blueprint $table) {
            $table->increments('id');
            $table->text('title');
            $table->string('hint')->nullable();
            $table->text('explanation')->nullable();
            $table->string('referencematerial')->nullable();
            $table->string('imagepath')->nullable();
            $table->boolean('published')->default(1);
            $table->timestamps();
            $table->softDeletes();
        });

Schema::create('answers', function(Blueprint $table) {
            $table->increments('id');
            $table->string('text');
            $table->boolean('iscorrect')->nullable();
            $table->timestamps();
        });

这是我的问答课程。

class Answer extends Model
{
    protected $table = 'answer';

    public $timestamps = true;

    protected $fillable = array('text', 'iscorrect');

    public function Question()
    {
        return $this->belongsTo('Question');
    }

}

class Question extends Model
{
    protected $table = 'question';
    public $timestamps = true;

    protected $dates = ['deleted_at'];
    protected $fillable = array('title', 'hint', 'explanation', 'referencematerial', 'imagepath', 'published');


    public function Answer()
    {
        return $this->hasMany('Answer');
    }

}

我的测验控制器

public function index()
{
    $questions = Question::with('answers')->get();
    return $questions;
}

BadMethodCallException in Builder.php line 2251:
Call to undefined method Illuminate\Database\Query\Builder::answers()

我一直在尝试在页面上加载问题和答案。它不断给我查询生成器错误。请随意加入。

【问题讨论】:

    标签: laravel laravel-4 laravel-5 laravel-5.1 laravel-5.2


    【解决方案1】:

    错误基本上是告诉你你调用的关系(answers)不存在。

    您在模型中调用了关系Answer(),但在查询中将其引用为answers

    您要么需要将模型中的关系更新为answers(),要么将查询更改为$questions = Question::with('Answer')->get();

    更新

    您的关系没有正确调用模型,应该是:

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

    【讨论】:

    • 我现在更新了,就是这个错误。 Model.php 第 869 行中的 FatalThrowableError:致命错误:找不到类“答案”我已调用使用 App\Models\Answer;也在上面。
    • @manshu 啊,这是因为你的关系没有正确调用模型。看我的更新,你只需要正确引用Answer模型..
    • SQLSTATE[42S02]:未找到基表或视图:1146 表“myapp.answer”不存在(SQL:select * from answer where answer.question_id in ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26、27、28、29、30、31、32、33、34、35、36、37、38、39、40、41,
    • @manshu 这很不言自明-您的Answer 模型引用myapp.answer 的表在您的数据库中尚不存在。您在迁移中调用了表answer,但在模型中调用了myapp.answer
    • SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'answer.question_id'(SQL:select * from answer where answer.question_id in (1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 , 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2016-02-26
    相关资源
    最近更新 更多