简体   繁体   中英

laravel eloquent relationships issue

i'm a litle bit confused and i need some help since im new in laravel ! i have 3 table !! questions , category and theme

a question have a category and a theme a theme have many categories a category belong to one theme

what im asked to do is when i will add a question i only choose a category from the list and it will be added with her correspondant theme in the question table !! i hope i explained good my question :)

the category migration 
Schema::create('category', function(Blueprint $table)
    {
        $table->increments('categoryId');
        $table->string('categoryName');
        $table->integer('themeId')->unsigned();
        $table->foreign('themeId')->references('themeId')->on('theme');
    });
the theme migration 
Schema::create('theme', function(Blueprint $table)
    {
        $table->increments('themeId');
        $table->string('themeName');
    });

the questio migration i didn't make relation since i didn't find a way to do it 
Schema::create('question', function(Blueprint $table)
        {
        $table->increments('questionId');
        $table->string('question', 200);
        $table->string('rightAnswer', 50);
        $table->string('explanation', 500);
        $table->string('wrongAnswer1', 50);
        $table->string('wrongAnswer2', 50);
        $table->string('wrongAnswer3', 50);
        $table->string('theme');
        $table->string('category');
        $table->integer('difficulty');
        $table->timestamps();
        });

Theme Model

class Theme extends Eloquent 
{   
    protected $primaryKey = 'themeId';
    protected $guarded = array();
    public function category()
    {
        return $this->hasMany('Category');
    }
}

Category Model

class Category extends Eloquent 
{
    protected $primaryKey = 'categoryId';

    protected $guarded = array();

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

Questions Model

class Question extends Eloquent 
{
    protected $primaryKey = 'questionId';

    protected $guarded = array();

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

You do not make the Eloquent relations in the migrations. Migrations are only for creating the database table structure. Rather, the relations in Eloquent are defined in the models you make:

// Theme Model
public function categories()
{
    return $this->hasMany('Category');
}

//  Category Model
public function theme()
{
    return $this->belongsTo('Theme');
}

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

// Question Model
public function category()
{
    return $this->belongsTo('Category');
}

These methods in the model define the relationship in Eloquent, and let you do things like this:

// Given an instance of a theme
foreach($theme->categories as $category)
    // ...

// Given an instance of a question
echo $question->category->theme->themeName;

That being said, the methods above won't precisely work given your table structure. Eloquent relies also on convention, and the convention is that foreign keys should be named in a specific manner, namely theme_id and category_id (vs themeId as you have in your categories table). You can override this per the documentation using this format:

return $this->belongsTo('Theme', 'themeId');

Though you're probably better off sticking to convention. This convention also states that the primary key of each table should be named id .

As for your question table, you can create a relationship to the category the same as you did for the relationship between the themes and categories: add a column to your questions table that references the category id:

// Your migration:
$table->integer('category_id');
// Add a foreign key as well if you wish, though it is not 
// required for the relationship in Eloquent

Then in your Question model place the category method I outlined above. Simple as that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM