I have a situation where belongsTo
realationship is null in the Eloquent model.
Here is my query:
$lead = Lead::where([
'email' => $input_data['email']
])->firstOrFail();
Also here is the class definition for Lead. It is related to another model.
class Lead extends Model {
protected $table = 'leads';
public function broker() {
return $this->belongsTo('App\Broker');
}
}
class Broker extends Model{
protected $table = 'brokers';
public function leads(){
return $this->hasMany('App\Lead');
}
}
I was under the impression that the email is a unique field in the database, but it was not, so there are situations where the query I am running has multiple results. But there is a firstOrFail()
, so it should not matter.
The problem is that the broker relationship is null when there are multiple results for the query
$lead->broker //holds a broker object, if there was one result in the query
$lead->broker //is null when the query returned more than on row
Very strange. Am I doing something wrong, or is it a bug?
The project is built with Laraval 5.2.45
via Martin Taleski