I have xml_document table with these columns:
id
general_information_id
I have general_information table with these columns:
id
domain
log_file
The relationship between them is one to one.
Have I build the relationship correctly? Or I need to add xml_document_id column to the general_information table.
Secondly:
I have added a row to the xml_doucment and now I want to add a row to the general_information and link this new row to the xml_document
I tried this:
$xmlDocument = XmlDocument::find(Input::get(4));
$generalInformation = new GeneralInformation($dataGeneralInformation);
$generalInformation->xmlDocument()->associate($xmlDocument);
//$xmlDocument->generalInformation()->attach($generalInformation);
$generalInformation->save();
$xmlDocument->save();
but I got error that xml_document_id column doesn't exist in the general_information table.
I tried to replace associate with attach but I got that attach is not an existed function.
please help me I am tried of this one to one relationship, I couldn't know what is the correct way to do id. I don't know where to add columns in the database and what to do in the models. I have tried a lot of things but still so confused.
Update 1
class GeneralInformation extends Eloquent{
.....
public function xmlDocument(){
return $this->belongsTo('XmlDocument');
}
}
class XmlDocument extends Eloquent {
....
public function generalInformation(){
return $this->hasOne('GeneralInformation','general_information_id');
}
}
To make a one-to-one relationship, you need to store the primary key of parent table in the child table as a foreign key. So if xml_document is parent and if it contains many general_information then the id field of the xml_document should be present in in the general_information table as xml_document_id.
So, you may build the one-to-one relation like this:
// xml_document model
public function generalInfo()
{
return $this->hasOne('GeneralInformation');
}
Then the declare the GeneralInformation model.
This is what you want:
class GeneralInformation extends Eloquent
{
public function xmlDocument()
{
return $this->hasOne('XmlDocument');
}
}
class XmlDocument extends Eloquent
{
public function generalInformation()
{
return $this->belongsTo('GeneralInformation','general_information_id');
}
}
Related
I have two tables that I need to join in order to access more data. My first table thought_journal_entries contains many id values from the second table emotions. The primary key in emotions is id and the foreign key in thought_journal_entries is em_id.
This is in the Emotions model
public function thoughtJournalEntry() {
return $this->belongsTo(ThoughtJournalEntry::class, 'id');
}
This is in the ThoughtJournalEntry model
public function emotions() {
return $this->hasMany(Emotions::class, 'id');
}
This is how far I've got, not sure if this is correct and how I would join these in the controller?
If you set em_id in thought_journal_entries table, that means that thoughtJournalEntry belongs to Emotions, and Emotions hasMany thoughtJournalEntry, so in this case relations must be declared this way
// Emotions class
public function thoughtJournalEntries() {
return $this->hasMany(ThoughtJournalEntry::class, 'em_id');
}
// ThoughtJournalEntry class
public function emotion() {
return $this->belongsTo(Emotions::class, 'em_id');
}
// access from controller
$emotion->thoughtJournalEntries;
$thoughtJournalEntry->emotion;
If this type of relations is not what you wanted, then probably you have to change your db tables
I have three table which I wanna associate. Shipment_methods, Ship_companies and Payment_methods.
Relations:
Shipment_methods <- pivot1 -> Ship_companies
pivot1 <- pivot2 -> Payment_methods
Well, what I wanna do is e.g. ShipmentMethod_A is attached to ShipCompany_B and for this record (from pivot1) I wanna attach record from Payment_method table through pivot2 table.
ShipmentMethod Model:
public function ship_companies()
{
return $this->belongsToMany(ShipCompany::class, 'shipment_methods_ship_companies', 'shipment_method_id', 'ship_company_id')->withPivot('price_kc', 'price_ha');
}
ShipCompany Model:
public function shipment_methods()
{
return $this->belongsToMany(ShipCompany::class, 'shipment_methods_ship_companies', 'ship_company_id', 'shipment_method_id');
}
What I need to do is I wanna retrieve all Payments for ShipCompany of specific ShipmentMethod like
ShipmentMethods->ship_companies->pivot->payments_methods
Thanx.
I think best way is for you to have a Model that extend Pivoted class for you pivot1. the pivot1 should have id column to use in pivot2. so the code should be like this,
ShipmentMethod Model:
public function ship_companies()
{
return $this->belongsToMany(ShipCompany::class, 'shipment_methods_ship_companies', 'shipment_method_id', 'ship_company_id')->using('App\pivot1')->withPivot('id','price_kc', 'price_ha');
}
note that I have put id in withPrivot and chain using() method
your pivot1 model should be like this,
pivot1 Model:
use Illuminate\Database\Eloquent\Relations\Pivot;
class pivot1 extends Pivot
{
public function Payment_methods()
{
return $this->belongsToMany(Payment_methods::class, 'pivot2_table_name', 'pivot1_id', 'payment_method_id');
}
}
and at the end you can do like this to save to pivot2
ShipmentMethods->ship_companies->pivot->payments_methods()->sync([payment_method_ids])
as all pivot relationship return a collection of array note that you need to loop ShipmentMethods->ship_companies relationship to get to pivot relationship.
Hope this helps!
I have a simple join to make, but I can't do it.
I have a tournament that has a categoryId field in the table.
The table TournamentLevel is a 10 entry table.
So I would like to be able to retrieve level->name with Eloquent.
I tried to do the following:
In Tournament, I tried to define a hasOne relationship:
class Tournament extends Model
{
....
public function level()
{
return $this->hasOne('App\TournamentLevel');
}
But then, Eloquant is looking for tourmanent_id inside of TournamentLevel Table
So I tried the opposite,
In Model TournamentLevel:
class TournamentLevel extends Model
{
...
public function tournament()
{
return $this->belongsTo('App\Tournament');
}
}
And tried to reach :
tournament->level->name
but without success
It seems pretty elementary, but I can't do it... Any idea???
How wherePivot() actually works internally in laravel 5 ?
For Example I was practicing by watching a tutorial and the teacher was using wherePivot() for construing relationship:
public function friendsOfMine(){
return $this->belongsToMany('Chatty\Models\User','friends','user_id','friend_id');
}
public function friendOf(){
return $this->belongsToMany('Chatty\Models\User','friends','friend_id','user_id');
}
public function friends(){
return $this->friendsOfMine()->wherePivot('accepted',true)->get()->merge($this->friendOf()->wherePivot('accepted',true)->get());
}
Thanks Guys .. but I think i found my answer
A pivot table is a database table that only exists to serve a many-to-many relationship. Say you have a table “customer” and a table “drinks”. If you want to know which customer ordered which drink you have to create a pivot table customer_drinks(customer_id, drink_id).
Define Pivot table
class Customer extends \Eloquent {
public function drinks()
{
return $this->belongsToMany('Drink', 'customer_drinks', 'customer_id', 'drink_id');
}
}
Create a Record
$customer = Customer::find($customer_id);
$customer->drinks()->attach($drink_id); //this executes the insert-query
Remove record from pivot Table
$customer = Customer::find($customer_id);
$customer->drinks()->detach($drink_id); //this executes the delete-query on the pivot table
I have 3 tables : hotels, hotels_data, hotels_types
Table hotels have id, type, stars, etc... type field is set as foreign key referencing type_id in hotels_types. I'm managing to get the correct data from hotels_data but have an empty result on getting hotels_types title and I don't understand why.
The code is the following :
class Hotel extends Eloquent {
public function getList() {
$data = Hotel::select('id','stars')->with('HotelData', 'HotelType')->paginate(10);
return View::make('hotels.index')->with('hotels', $data);
}
public function HotelData()
{
return $this->hasOne('HotelsData')->select('id','hotel_id','title');
}
public function HotelType()
{
return $this->hasOne('HotelType','type_id', 'type')->select('id','type_id','title');
}
}
You're using the wrong relationship for HotelType()
Your Hotel model should use the inverse of hasOne, which is belongsTo, because it contains the foreign key to HotelType (when a table contains a foreign key, it always belongs to the table being pointed to).
The following should work:
public function hotelType() {
return $this->belongsTo('HotelType','type', 'type_id')->select('id','type_id','title');
}
I've obtained the desired result with the following:
Hotel::select('hotels.*','hotels_types.title as type')
->join('hotels_types', 'hotels.type', '=', 'hotels_types.type_id')
->with('HotelData');