Here is my parent model which uses default database connection of mysql type.
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Property extends Model
{
use SoftDeletes, HybridRelations;
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [];
/**
* Get the property type for the property.
*/
public function propertyType()
{
return $this->hasOne('App\RentHisto\Models\PropertyType');
}
}
And here is my mongodb model which has relation with property
class PropertyDetail extends Model
{
/**
* database connection
*/
protected $connection = "mongodb";
protected $collection = "property_details";
public function propertyDetails(){
return $this->belongsTo(Property::class);
}
}
And here is my query to get the data of property with property_details
$properties = Property::with('propertyDetails')->get();
return $properties;
But the problem is that when I'm trying to get data it returns null in case of property details. It maybe due to, we are calling the mongodb model data on the connection with mysql instance. So is there any way by which I make relationship(hasOne) between two models i.e mysql model and mongodb model. Thanks !
+1
I'm looking to do the exact same thing, storing the _id hash of MongoDB with a MySQL model and relating the two. Any luck?
Set $connection = 'mysql' in your Property class
Any Progress on this issue??
I was struggling "eager load" problem like this. When I'm using with method all collection returning as empty. Then I tried to add the load method like below and then it works. Maybe this will helps.
return (new GPS())->setCollection(gps_table($from))
// This doesn't work. Returns empty
//->with(['flight' => function ($q) { $q->select('id', 'flight_code'); }])
->whereBetween('timestamp', [$from, $to])
->get()
// And then I tried like this and it's worked.
->load(['flight' => function ($q) { $q->select('id', 'flight_code'); }]);
Most helpful comment
Set
$connection = 'mysql'in your Property class