I have two MSSQL tables so i created two models [Adress] and [Webshop]. The foreign key is Adresse in both tables.
1.Model [Adress]
class Adress extends Model
{
protected $table = "Adress";
protected $primaryKey = 'Adresse';
public $timestamps = false;
public function webshop()
{
return $this->hasOne('App\Webshop', 'Adresse');
}
}
2.Model [WebShop]
class Webshop extends Model
{
protected $table = "Webshop";
protected $primaryKey = 'Adresse';
public $timestamps = false;
public function adress()
{
return $this->belongsTo('App\Adress','Adresse');
}
}
I would like to make a table with some data from the first and second table like the webshopID, mobile is in [Webshop] table and adress in the [Adress] table. I think this is a one to one relationship between this two tables.
in php artisan tinker
:
App\Adress::all(); -> this is working
App\Adress::find(2910)->webshop -> this is also working
App\Adress::with('webshop')->get() -> this is NOT working
I would like to retrieve data from this two tables at the same time. Is this possible with a relationship or do i heave to use the joins?
EDIT: maybe my foreignKeys are wrong
Try to change your relationship in Address
model to
$this->hasOne('App\Webshop', 'Adresse', 'Adresse');
and in Webshop
model
$this->belongsTo('App\Address', 'Adresse', 'Adresse');
EDIT
Now to retrieve the relationships you can do
$webshop = App\Address::find($id)->webshop;
$address = App\Webshop::find($id)->address
Please try with this -
use App\Adress;
use App\Webshop;
$result = Webshop::with('Adress')->where('Webshop.id',$id)->get();
or
$result = Adress::with('Webshop')->where('Adress.id',$id)->get();
Hope this will help you.