Home > Back-end >  How can I display Foreign Key ID in Laravel on blade file
How can I display Foreign Key ID in Laravel on blade file

Time:08-25

I don't know how to get the data from this foreign key. I have followed all steps from the documentation, but I still don't know what needs to be done. This is my teachers tables(Main part of function up):

public function up()
{
    Schema::create('teachers', function (Blueprint $table) {
        $table->id();
        $table->unsignedbiginteger('levels_id');
        $table->foreign('levels_id')->references('id')->on('levels');
        $table->string('teacher_name'); 
        $table->string('teacher_email')->unique();           
        $table->string('teacher_home_phone');
        $table->string('teacher_mobile_phone');
        $table->string('teacher_work_phone');
        $table->string('teacher_home_address');
        $table->string('teacher_suburb');
        $table->string('teacher_postcode');
        $table->string('teacher_username');
        $table->string('teacher_password');                      
        $table->timestamps();
    });
}

This is my Levels Tables:

public function up()
{
    Schema::create('levels', function (Blueprint $table) {
        $table->bigIncrements('id');            
        $table->string('levels_name');
        $table->timestamps();
    });
}

This is my model Teacher.php:

class Teacher extends Models {

    protected $fillable = [
        'levels_id', 
        'teacher_name',
        'teacher_email',
        'teacher_home_phone',
        'teacher_work_phone',
        'teacher_mobile_phone',
        'teacher_home_address',
        'teacher_suburb',
        'teacher_postcode',
        'teacher_username',
        'teacher_password',
        'teacher_level',
    ];

    public function levels()
    {
        return $this->hasMany(Levels::class);
    }               
}

This is my levels.php file:

class Levels extends Model
{
    use HasFactory;

    protected $fillable = [
        'levels_id',
        'levels_name',
    ];

    public function levels()
    {
         return $this->belongsTo(Teacher::class);
    }
}

This is a blade file for teachers\partials\form.blade.php:

<div >
                <label for="Teacher_Name" >Mention levels Instructor's going to teach </label>
                <div >
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 1" @if(isset($teacher->levels_id) && @in_array('Level 1', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 1</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 2" @if(isset($teacher->levels_id) && @in_array('Level 2', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 2</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 3" @if(isset($teacher->levels_id) && @in_array('Level 3', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 3</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 4" @if(isset($teacher->levels_id) && @in_array('Level 4', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 4</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 5" @if(isset($teacher->levels_id) && @in_array('Level 5', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 5</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 6" @if(isset($teacher->levels_id) && @in_array('Level 6', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 6</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 7" @if(isset($teacher->levels_id) && @in_array('Level 7', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 7</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 8" @if(isset($teacher->levels_id) && @in_array('Level 8', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 8</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 9" @if(isset($teacher->levels_id) && @in_array('Level 9', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 9</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 10" @if(isset($teacher->levels_id) && @in_array('Level 10', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 10</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 11" @if(isset($teacher->levels_id) && @in_array('Level 11', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 11</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="Level 12" @if(isset($teacher->levels_id) && @in_array('Level 12', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Level 12</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="undergraduate" @if(isset($teacher->levels_id) && @in_array('undergraduate', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Undergraduate</label>
                    </div>
                    <div >
                        <input  type="checkbox" name="levels_id[]" value="postgraduate" @if(isset($teacher->levels_id) && @in_array('postgraduate', @$teacher->levels_id)) {{'checked'}} @endif>
                        <label >Postgraduate</label>
                    </div>

CodePudding user response:

You are using wrong relationship in your model your table structure defines that you should use belongsTo Rlelation here.

public function levels()
{
    return $this->belongsTo(Levels::class);
}    

CodePudding user response:

The relationship is in the other way around (according to your tables structure). One Teacher belongs to a Level.

If so, you should use:

public function level()
{
    return $this->belongsTo(Levels::class);
}

Also, makes more sense to name the relationship in singular, since it only belongs to a single level, not to many (you should do the same for your model).

For the other way around (to access the teacher models from a $level instance, you can define the Has Many relationship from the Levels model:

public function teachers()
{
    return $this->hasMany(Teacher::class);
}

For more info, check the documentation regarding this.

  • Related