Laravel Relationship: a table having ids from two other tables - laravel

I have two tables: 'tours' and 'tourists' in my DB.
A 'tour' can have MANY 'tourist's, and 'tourist' can participate in MANY 'tour's.
So, I have successfully related them via 'begonsToMany" relationship (when adding rows to "tours' and 'tourists" an intermediate table 'tour_tourist' is given a row also.
"tour_tourist" table structure is:
tour_id | tourist_id
1 | 37
1 | 38
1 | 39
(tourists with id's 37, 38 and 39 go are assinged to the tour with id = 1)
THE PROBLEM : One of 'tourists' (37 or 38 or 39 from example abouve) can be a 'buyer' of the 'tour' (1 from above table). So only 1 person pays for the tour. Also this person can go to the tour/only pay for it. I pass this data (tour, tourists, who is buyer and does he goes to the tour) when submitting a tour via web-form.
So I want to create a 3rd table, called 'buyers' which represent a "tourist" who pays for the tour:
tour_id | tourist_id | is_tourist
1 | 39 | 0
(1. one 'tour_id' can match only ONE 'tourist_id')
(2. 'tourist_id' should be one of those who 'belong' to the 'tour_id' in the previous "tour_tourist" table - tourist 39 belongs to tour 1 - see table above).
(3. 'is_tourist' is 1/0, means the buyer goes to the tour/only pays, in my example it's 0 - he only pays, doesn't go).
I am wondering how can I do this using Eloquent relationships (one-to-one? hasManyThrough?) .
Appreciate any help.

Probably a simpler approach for that is to create two other columns called is_payer and is_tourist in your tour_tourist table in order to avoid creating your third table.
The result of your tour_tourist table could be something like this:
tour_id | tourist_id | is_payer | is_tourist
1 | 37 | 1 | 0/1
1 | 38 | 0 | 1
1 | 39 | 0 | 1
Then with eloquent you can simply access your extra columns' using withPivot method chaining your belongsToMany method.
For example, your Tour model can be something like this:
Tour
class Tour extends Eloquent {
protected $table = 'tours';
public function tourists() {
return $this->belongsToMany('Tourist')->withPivot('is_payer', 'is_tourist');
}
}
In the result you will have is_payer & is_tourist.
You can then filter them out using Collection methods.

Related

BelongsToMany relation with specific condition

I've the following tables:
users (list of users) :
id | type
5 | client
10 | expert
clients (assigning many users to many groups):
uid | gid
5 | 1
client_groups (groups defined by experts):
id | title | expert_uid
1 | diabetic | 10
Every expert can define a group of clients and put any of his clients in one of them.
A client may belong to many experts.
Now what I want is to get the group of the current user which is defined by specific expert.
I've tried the following relation:
class User{
public function client_groups()
{
return $this->belongsToMany(\App\Models\client_group::class,"clients","uid","gid");
}
}
User::with("client_groups")->
whereIn("id",$client_ids)->
orderby($orderBy,$order)->
get();
But as you may guess it will return all client_groups for the current client But I want the ones that has been defined by the current expert and not all of them.
Thanks in Advance

How to find all items that have no related objects?

There are two related tables A and B. The relation is one to many - A may point to the multiple entities in B.
Example:
Table A
id | title
--------
1 | 'title 1'
2 | 'title 2'
Table B
id | id_from_a
--------
1 | 1
2 | 1
I want to find all entities in table A that do not have anything in table B using graphQL.
For time being I am using the following contraption:
query getData {
table_a(where: {_not: {table_b: {id_from_a: {_neq: -9999}}}}) {
id
title
}
}
It works, but it is ugly as hell, so I guess there should be some better graphql solution.

Laravel, how to search for product features? get the ids where every item in array is present in table

I have a table with product features
table structure below
..|product_id | feature_id|created_at
..|1 | 5 |time_stamp
..|1 | 6 |time_stamp
..|2 | 6 |time_stamp
..|2 | 9 |time_stamp
.
.
.
how to write query to search with array [5,6] and get result of product id 1
You can try like
product_feature_data is a model name
Proudct::whereHas('product_feature_data',function($q) {
$q->whereIn('feature_id', [5,6]);
})->get();

Relationship Model in Laravel?

This question is about the Laravel style of doing things:
Everything in Laravel can be done in an elegant way.
I currently have a many to many relationship between mongrels and breeds through a table named breed_mongrel but this table also has a certainty value describing how certain the dog is that he's indeed a mix of that specific breed.
mongrel_id | breed_id | certainty|
----------------------------------
| 1 | 4 | 50 |
| 1 | 2 | 25 |
| 2 | 5 | 75 |
this means that mongrel#1 is 50% sure she's of breed#4 and 25% sure she's also a mix with breed#2. However mongrel#2 is a whooping 75% certain he is breed#5.
My question is what is the elegant way to add records to this table?
I could use something like this:
DB::table('breed_mongrel')->insert(
array('mongrel_id' => 3, 'breed_id' => 5, 'certainty' => 37));
But in Laravel I would normally do things more eloquently like:
$relation = new Relation;
$relation->breed_id = 1;
$relation->save();
But because it is a relationship and I can't follow the normal model named in singular camel caseform and DB table named in plural lower (snail) case I'm not sure what is Laravelly way to add entries to this database table?
You can use attach. It also accepts a second parameter allowing extra values to be added to the pivot table.
$mongrel = Mongrel::find(1);
$breed = Breed::find(4);
$mongrel->breeds()->attach($breed, array('certainty' => '50'));

set values in form fields in edit mode when data comes from multipal tables

I am developing a custom module for a project. I created a custom form and that custom form's data saved in two tables. Now when form open in edit mode I am not able to get saved data from both tables. I have no idea how can I get resolve this issue, please help me.
Here are my two tables structures:
Table1-
-------------------------
id | page_id | title
1 | 3 | ABC
2 | 4 | PQRS
3 | 10 | XYZ
Table2-
--------------------------------
id | page_id | child | position
1 | 3 | 8 | left
2 | 3 | 7 | right
3 | 3 | 15 | right
4 | 4 | 14 | right
5 | 4 | 15 | left
6 | 10 | 15 | left
--------------------------------
Here i am attaching a screen-shot to more explain myself. I want to selected saved option values in 'left' & 'right' text-area in edit mode, values comes from table2.
Please suggest me. Thanks in advance.
Left and Right are here multiselect field types and these kind of fields receives values in comma separated string. so the example you presented will work in this way.
lets consider you have models Table1 and Table2 and you pass your Table Model from edit action in you controller you have written
$table1Id = $this->getRequest()->getParam('id');
$table1Model = Mage::getModel('page/table1')->load($table1Id);
if ($table1Model->getId()) {
Mage::register('page_data', $table1Model);
...
In your form file Block/Adminhtml/Edit/Tab/Form.php there is method $form->setData()
if ( Mage::getSingleton('adminhtml/session')->getPageData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getPageData());
Mage::getSingleton('adminhtml/session')->setPageData(null);
}
elseif ( Mage::registry('page_data') ) {
$values = Mage::registry('page_data')->getData();
$values['left'] = '8';//You can get this value from Table2 collection on basis of $values['page_id'] you got
$values['right'] = '7,15';////You can get this value from Table2 collection on basis of $values['page_id'] you got
$form->setValues($values);
}
I found the solutions of my query.
This extension is the best example click here
To get resolve this need to edit many files like model, resource and block files. That's why I mentioned this link to understand complete process.
most file that are need attention are :
app/code/community/[PackageName]/[ModuleName]/Model/Resource/Pagesummary.php
app/code/community/[PackageName]/[ModuleName]/Model/Pagesummary.php
app/code/community/[PackageName]/[ModuleName]/Block/Adminhtml/Cms/Page/Edit/Tab/Pagesummary.php
hope this helps others!

Resources