How to create a form that contains data for multiple entities? - laravel

I've been trying to think of the best possible way to submit data for multiple entities in a single form.
My use case is that I have a table that contains multiple rows of different entities. I want the ability to go into 'edit' view where some of the columns will become textfields and allow for the value to be changed.
Example
Type | Name | Age
Animal | Eevee | 4
Animal | Sandy | 7
Human | Bob | 24
[Submit]
In edit mode they would be able to change the age for any of the entities. When the user hits save button for the entire list, I want the form to submit all the changes. The problem is that in the update method, I need to know which entities to update with which values
I was thinking of using name attribute to store the entity id of each entity on the input
<input name="{{$entity->id}} value="{{$entity->age}}"></input>
but Animals and Humans are stored in different DB tables. So in the update method of the controller I would have to take the name (which would be the id of the entity) and if it exists as a human
entityToUpdate = Human::find(id)
if entityToUpdate does not exist
entityToUpdate = Animal::find(id)
We use UUIDs in our DB so this would work but it doesn't feel right.
is there a way to group a couple inputs together in a form?
group1
type=animal
id = (Sandys ID)
age = 8
group2
type=human
id =(bobs ID)
age = 25

You could use array notation in your input names in order to get what you want on the server side:
#foreach($entities as $entity)
<input type="hidden" name="group[{{$loop->index}}][type]" value="{{$entity->type}}"/>
<input type="hidden" name="group[{{$loop->index}}][id] value="{{$entity->id}}"/>
<input type="text" name="group[{{$loop->index}}][age] value="{{$entity->age}}"/>
#endforeach
Then on the server side:
foreach(request()->get('group') as $entity) {
// $entity contains ['id'], ['type'], ['age']
}

Related

Laravel : How to show a multiple rows from DB in one row in view blade

In my Laravel project, I have a multiple rows in my DB table, similar to each other in every thing except ID, other column called stage
Stages table
ID
name
1
first
2
second
3
third
subjects Table
ID
name
stage_id
1
English
1
2
English
2
3
English
3
I need to show em in a blade like this
#
name
stage
1
English
first, two, three, etc
I already have the relations in my modals, and I am already showing them in my blade by loop, but I just want to group the rows by name and show the different stages
I hope that I explained the case well, I am not very good at explaining
Updates : before editing I wrote a dummy data now I hope I explained the issue in more details
The project idea is that you can create more than faculty
and every faculty has Stages and sections and every section has specialties and every specialty has subjects
now about subjects and stages
user can create subject like English and make it available to the first stage and 2nd and third ..etc ( the available stages in this faculty)
The Stages belongs to Faculty
The Subjects belongs to Stage
so the relations are
Subject Model
public function stage()
{
return $this->belongsTo(Stage::class);
}
Stage Model
public function subjects()
{
return $this->hasMany(Subject::class);
}
As you have "English" Multiple Times, you should have a subject Table :
Stages table
ID
name
1
first
2
second
3
third
Subject table
ID
name
1
English
stage_subject Table :
ID
stage_id
subject_id
1
1
1
1
2
1
1
3
1
Subject Model
public function stage()
{
return $this->belongsToMany(Stage::class);
}
Stage Model
public function subjects()
{
return $this->belongsToMany(Subject::class);
}
Then in your foreach:
#php($subjects = Subject::with('stages')->get())
#foreach($subjects as $subject)
{{ $subject->id }} | {{$subject->name}} | {{$subject->stages->pluck('name')->implode(', ')}}
#endforeach
If you don't / can't do a pivot table, then you can use collection :
#php($subjects = Subject::with('stages')->get())
#foreach($subjects->groupBy('name') as $name => $subjects)
{{ $subjects->first()->id }} | {{$name}} | {{$subjects->pluck('stage.name')->implode(', ')}}
#endforeach

How to call multiple filters in DHTMLX Grid

I would like to call multiple functions in DHTMLX Grid.
I have a table which has the following 7 columns
EX:
---------------------------------------------------------------
No. | Name | Age | Gender | Marital Status | Education | City |
---------------------------------------------------------------
I would like this grid to be filtered by multiple filter conditions.
For Ex: Filter the grid to have only the gender 'Male' whose age is below 35.
Currently my doFilter() function looks like this.
function doFilter() {
mygrid.filterBy(3,'M',true);
mygrid.filterBy(2,function(a){ return (a > 55);} );
}
But the grid is only filtered by age not by Gender column.
Please let me know how to apply multiple filter condition in DHTMLX Grid.
You need to use it as
mygrid.filterBy(3,'M');
mygrid.filterBy(2,function(a){ return (a > 55);}, true);
second filterBy call must have true as last parameter, to preserve results of previous filterBy call.

Change Group Labels in AdvancedDataGrid

I'm trying to use an AdvancedDataGrid to display some grouped data. Normally flex displays this in a "tree view" with a folder icon represent the group. I need to group the data based on an integer ID field in my object, but I'd like the label for the folder icon to display the groupName field in my object.
Here's a little example:
{groupName: group1, ID: 1234}
{groupName: group2, ID: 5678}
<mx:grouping>
<mx:Grouping label="Group"> <--- The label of the whole column
<mx:GroupingField name="ID">
</mx:Grouping>
</mx:grouping>
Resulting output:
=== Group ===
+ 1234
- child
- child
+ 5678
...
But I'd really like to output:
=== Group ===
+ group1
- child
- child
+ group2
...
If anyone has any tips I'd appreciate it.
-- Dan
Have a look at GroupingField#groupingFunction. From the adobe docs:
A function that determines the label for this group. By default, the group displays the text for the field in the data that matches the filed specified by the name property. However, sometimes you want to group the items based on more than one field in the data, or group based on something that is not a simple String field. In such a case, you specify a callback function by using the groupingFunction property.
private function myGroupingFunction(value:Object, field:GroupingField):String

DefaultModelBinder and binding action parameters from values in a table row

What I am doing is this. I have a basic table with a button at the end of each row for "selection" of that row for further actions. My action method has two simple parameters I'll call them id1 and id2 so:
public ActionResult DoSomething(int id1, int id2)
id1 will be bound from a hidden form field outside the scope of the table, this works fine.
id2 needs to come from a hidden column from an individual row in the table depending on the button clicked representing the row that requires further processing. Make sense?
When the form posts back to the action method, id1 is set perfectly, no problem, but id2 is always the value of the hidden column from row 1. How do I detect/make DefaultModelBinder pull the hidden column value from the row from which the button is being clicked?
Thanks for the assist as I am actively trying to figure this out.
Here is the hidden table column that is rendered per row that I want to capture it's value on the post from the row from which the button was clicked:
<td style="display:none;">#Html.Hidden("id2", viewModel.id2)</td>
You could use simple hyper links instead of forms on each row. Like this:
#Html.ActionLink("link text", "DoSomething", "SomeController", new {
id1 = viewModel.id1, id2 = viewModel.id2
}, null)
which assuming default routes should generate the following link:
/somecontroller/DoSomething?id1=123&id2=456
and when you click on it the DoSomething action will be invoked with correct parameters.
If you want to do this with POST you will need multiple forms, one for each row:
#using (Html.BeginForm("DoSomething", "somecontroller"))
{
#Html.Hidden("id1", viewModel.id1)
#Html.Hidden("id2", viewModel.id2)
<input type="submit" value="OK" />
}

Displaying data from multiple entities in a single NSTableView (Core-Data)

I have an entity called Pupil, and an entity called Loan.
The Pupil entity has the attributes: firstName, lastName, address, postCode, telephoneNumber.
The Loan entity has the attribute: loanID, and the relationship: pupilID, which is a relationship to the entity Pupil.
I wish to display the loanID, with the pupil (if any) related to the loanID in a table.
e.g.:
LoanID | firstName | lastName | address | postCode | telephoneNumber
1 | bob | smith | 98 Any Road | N1 1QW | 0123456789
How would I go about this? I am currently using the bindings for my other tables.
Thank you!
You can do this assuming that you have set the reverse relationship for pupilID (i.e. a relationship from Pupil to the Loan). If you call that relationship loan, and have an NSArrayController, PupilsController bound to the collection of Pupils, then your first table could be bound to PupilsController.arrangedObjects.loan.loadID and your other columns bound as you'd expect.
On a purely stylistic side note, the pupilID property would more appropriately be named pupil. Core Data is no an ORM and you're not in SQL JOIN land any more. Name the properties what they are, not how they're implemented under the hood by Core Data.

Resources