AwadGorg
4 months ago

please help setting relationships using eloquent

Posted 4 months ago by AwadGorg

Hello, am building this new website and I have three table browser games are the parent and browsergamesMedia is the first child and BrowsergamesRating is the second child what I want is to show the result from the Browsergames table and the BrowsergamesMedia table this is the code I am using to do so

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Browsergames extends Model
{
    protected $table = "browsergames";
    public function BrowsergamesMedia(){
        return $this->hasOne('App\BrowsergamesMedia', 'g_id');
    }
    public function browsergamesRating(){
        return $this->hasMany('App\browsergamesRating', 'game_id');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BrowsergamesMedia extends Model
{
    protected $table = 'browsergames_media';
    public function Browsergames(){
        return $this->belongsTo('App\Browsergames', 'id');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class browsergamesRating extends Model
{
    protected $table = "browsergames_rating";
    public function Browsergames(){
        return $this->belongsTo('App\Browsergames', 'id');
    }
}

not that the games has one Media row in the browsergamesMedia table and hasMany rows in the rating table

public function index()
    {
        $games = Browsergames::orderBy('created_at', 'DESC')->paginate(16);
        /*
        * explode the game_main_img to get the name of the image without the ext and replace the old ext with the jpg ext to use as a fullback 
        */
        $data = array('games' => $games);
        return view('browser-games.index')->with($data);
    }

now this code is working very nice displing content on my view but what I want is to make another var that will display games based on the ratings they got from the BrowsergamesRatingtable and i think that can only be done in the controller but i don't know how to do so am new in laravel

Please sign in or create an account to participate in this conversation.