Это мои схемы ТаблицыСоздание простой викторины логики в Laravel
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');
}
}
Мои QuizController
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()
Я пытался загрузить вопросы и ответы на этой странице. Он продолжает давать мне ошибку построителя запросов. Пожалуйста, не стесняйтесь звонить в него.
Я обновил эту ошибку. FatalThrowableError в Model.php line 869: Неустранимая ошибка: Class 'Answer' не найден Я назвал использование App \ Models \ Answer; сверху тоже. – manshu
@manshu Ах, это связано с вашими отношениями, которые неправильно называют модель. См. Мое обновление, вам просто нужно правильно ссылаться на модель 'Answer'. – James
SQLSTATE [42S02]: Базовая таблица или просмотр не найдены: 1146 Таблица« myapp.answer »не существует (SQL: select * from' answer' где 'answer'.'question_id' в (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