Unable to Get Eloquent to Automatically Create Joins Unable to Get Eloquent to Automatically Create Joins laravel laravel

Unable to Get Eloquent to Automatically Create Joins


Clearly with('Country') or with('country') doesn't do any different due to the fact that he managed to get following error:

Column not found: 1054 Unknown column 'tournament_id' in 'where clause'SQL: SELECT * FROM `countries` WHERE `tournament_id` IN (?)

What wrong is how the relationship is defined: A tournament must have a country would be a tournament need to belong to a country, and not has one country. So to solve this change the relationship to

public function country(){    return $this->belongs_to('Country');}


According to the Eloquent docs:

Note: All methods available on the query builder are also available when querying Eloquent models.

So with that in mind something like:

DB::table("tournament")->join("countries","tournaments.country_id","=","countries.id")->get();

Should be replicable in Eloquent. I personally use the query builder version just now which you may want to use but I will try and test with Eloquent when I get the chance and update this.

UPDATE:

Yep, using Eloquent's query builder methods you can have:

Tournament::join("countries","tournaments.country_id","=","countries.id")->get();

This will return a Tournament model that includes the fields of both tables.

HTH


Your 'with' clause asks for 'Country', but your code declares it as 'country'.

So, should:

$tournaments = Tournament::with('Country')->all();

Be:

$tournaments = Tournament::with('country')->all();

Because in your Tournaments Model, you've defined this as:

public function country(){    return $this->has_one('Country');}

Does making this change solve it?