I have a SQLite Database from another software wich I need to integrate into my own API. I started with that task (multiple databases) successfully by adding models and relations to my API in Laravel.
But now I came to an point where I cannot continue because I don't know how to build a relationship between 2 tables in that foreign DB by using the "rowid" as key.
What I've tried:
Added a usual relationship to both models like that:
Model A: Has a column named "BU_ZIMMER" that stores the "rowid" of the other Model (B)
class LdBooking extends Model
{
protected $connection = 'lodgitDB';
protected $table = 'b_buchungen';
protected $primaryKey = 'rowid';
public $timestamps = false;
public function rentalunit()
{
return $this->belongsTo('App\Models\LodgitDesk\LdRentalunit', 'BU_ZIMMER', 'rowid');
}
}
Model B: Has the rowid that is linked by other models.
class LdRentalunit extends Model
{
protected $connection = 'lodgitDB';
protected $table = 'o_mieteinheit';
protected $primaryKey = 'rowid';
public $timestamps = false;
public function bookings()
{
return $this->hasMany('App\Models\LodgitDesk\LdBooking', 'rowid', 'BU_ZIMMER');
}
}
The Controller: Fetches all data by using the "with()" method. This works perfectly for all "normal links", but not for a link to a SQLite rowid.
public function show($id)
{
return $this->respond( LdBooking::with('rentalunit')->with('category')->select('rowid', '*')->findOrFail($id) );
}
The Result:
The Problem:
I think the problem is, that the standard query doesn't SELECT the rowid from a table. That's why I usually add them manually to a Eloquent-Query ->select('rowid', '*')
. But I don't know how to teach my relationship, that if it's joining the other table by the key "rowid" it needs to SELECT this "rowid" first.
Is there any way to do that? Maybe modify the model? Or is it possible to workaround this problem?
I could not find a valid solution to this yet.
Aucun commentaire:
Enregistrer un commentaire