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'));
Related
I have a data table that resembles the structure here:
| Prof | PI | Class |
|:----:|:------:|:-----:|
| Dr.K | Louisa | A |
| Dr.L | Jenny | B |
| Dr.X | Liu | C |
Filter 1: I'd like to create two dropdown, single selection parameter-filters, the first of which contains the headers of the columns. So, filter one would contain the option to select: Pro, PI, or Class.
Filter 2: The second filter would then dynamically change to represent values of the selected column. If a user chose "Prof" in Filter 1, Filter 2 would show: Dr. K, Dr. L, and Dr. X. The table in the dashboard would then reflect the chosen filters.
I believe choosing "only relevant values" on Filter 2 would take care of some of the issues, but I still don't understand how I can turn column headers into a list, and those values still retain the integrity of the original columns. Thank you for any help you can provide!
IF [Parameter 1] = STR("Prof") THEN [Prof] ELSEIF [Parameter 1] STR("PI") THEN [PI] END
I am trying to optimize some of my pages. Here is my current situation.
There is one table, where I list all Bookmakers. Here are the columns of the table and their types:
ID (attribute) | Name (attribute) | Games (relation) | Links (relation) ...
1 | Some name | 5 | 10
I have optimized the number of queries by using eager load, unfortunately, on that page, I am loading all relations of each Bookmaker, but I need only the number of each relationship.
Here is my query:
return Bookmaker::select(['*'])->withCount(['links', 'games', 'restrictions', 'licenses', 'favourites'])
->with(['bonuses', 'links', 'games', 'restrictions', 'licenses', 'favourites']);
The debugbar is showing me, that it has loaded also X Bonus models, X Game models, and so on.
How can I disable that load?
Having the following tables:
----------- ----------------- ---------------
| PROJECT | | ACCESSES | | ENVIRONMENT |
----------- ----------------- ---------------
| id | | id | | id |
| title | | project_id | | title |
----------- | environment_id| ---------------
| username |
| password |
-----------------
My goal is to get all the environments used by a project through the accesses table
In my Project model:
public function environments(){
return $this->belongsToMany('App\Models\Environment', "accesses");
}
My problem is that if I have multiple rows with the same project_id and environment_id values in the accesses table, it will fetch multiple time the same environment.
How may I force it to retrieve each environment only once?
This is an old question, but the benefit of future travellers:
The distinct() method can help in this situation:
public function environments() {
return $this
->belongsToMany('App\Models\Environment', "accesses")
->distinct();
}
From the docs:
The distinct method allows you to force the query to return distinct results:
$users = DB::table('users')->distinct()->get();
As far as I can tell, this works at least as far back as Laravel 4.x, so it should be fine for all currently supported versions.
You can achieve this using sync method or toggle method it depends on your use case.
From the docs:
The many-to-many relationship also provides a toggle method which "toggles" the attachment status of the given IDs. If the given ID is currently attached, it will be detached. Likewise, if it is currently detached, it will be attached:
$project->environments()->toggle([1, 2, 3]);
You may also use the sync method to construct many-to-many associations. The sync method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the given array will exist in the intermediate table
$project->environments()->sync([1, 2, 3]);
For more information please have a look in the docs.
https://laravel.com/docs/5.6/eloquent-relationships#updating-many-to-many-relationships
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.
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!