samedi 23 avril 2016

Creating Simple Quiz Logic in Laravel

These are my Schema Tables

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();
        });

Here is my Classes for Question and Answer.

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

}

My 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()

I have been trying to load the questions and answers on the page. It keeps giving me query builder error. Please feel free to chime into it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire