Laravel 5 Request Rows on Table From View/Store - php

I'm working on developing a system of applications with Laravel 5.2. I have some <select>s to select the products and report the amount. It will generate a table with a list of products selected for the application. How do I retrieve this information table in my controller?
[updating]
Excuse me English, but my question is as shown in the image, by clicking the button of "submit order" I can read the <table> <tr> <td> with added products and save the products in Order_Details table.
enter image description here

Actually, for a select you'll need two fields: the value (in the most of cases the Id) and another element to display as key. Eloquent provides an useful method called "lists": it returns a collection of key/value elements.
$model = Model::lists('key', 'value');
Replace your 'key' and 'value' fields with your column's name. The next step is using the retrieved data in your view. You've got two different ways to solve this part of the problem:
Manually iterate through the collection to compose your select statement
Use the LaravelCollective Html helper
The La ravelCollective HTML helper has a lot of methods that I'm sure you'll find useful in your coding. It even includes a method for creating dropdowns.

In order to retrieve the information that is on that table in a controller, you need to POST the data.
First you'll need a post route in your App\Http\routes.php file:
Route::post('pedidos', [
'as' => 'post-pedidos',
'uses' => 'PedidosController#post'
]);
Now you can create a form which will gather all the data that will be sent to that route. It should contain the table which your products dropdown input will generate:
<form method="post" action="{{ route('post-pedidos') }}">
{{ csrf_field() }}
<table>
// Put table rows + input here
</table>
</form>
Now, all you need to do is make sure your rows have actual inputs on them, which will be submitted with the form. Something like this?
<input type="hidden" name="codigo[0]" value="30628">
<input type="hidden" name="quantidade[0]" value="90">
To see what you'll need on PedidosController's post method, check out how to retrieve data from a request and how to validate it before inserting it into the database.

Related

Accessing name value of an array

my first post here, and first proper Laravel project.
I have a table, from which I am pulling out at random 3 values. Each of those contain an (1) action, that I am printing, and a (2) number of points associated with this action. The table currently does not have any more rows.
Each of the actions when displaying is now assigned a checkbox, which user can tick (or not) before submitting the form. Currently it looks like this:
#foreach (App\Models\Action::randomiser() AS $action)
<label class="container"><sup>{{ $action->action }}</sup>
<input type="checkbox" name="action[]" value="{{ $action->point }}">
<span class="checkmark"></span>
</label>
#endforeach
The trouble is, I need to collect the values of each ticked box. Now, the method is POST, but the there is no table of reference for it - you tick the boxes, and submit, and output comes back, or at least it COULD come back if I could access the value of the action.
When I've done it in similar fashion, but without using foreach, and I could access value quite easily, and all of it added nicely together:
$total = (
+(isset($_POST["action[1]"]) ? ($_POST["action[1]"]): 0) +
+(isset($_POST["action[2]"]) ? ($_POST["action[2]"]): 0) +
+(isset($_POST["action[0]"]) ? ($_POST["action[0]"]): 0));
then $total gets evaluated in next function.
I tried to access it via each item in an array, using +(isset($_POST["action[0]"]) etc, but that comes back with 0 no matter how many items I tick in .
Is there some smart way to access these values? Or if that's impossible, can you perhaps suggest best way to create the table to collect the information? I will need to pull foreign key of $action->point, and each value would have to count as boolean (true or not true)...
Any help would be much appreciated.

Symfony 3 - Form builder: Storing select field in database with doctrine

I'm new to Symfony and I would like your help in deciding the best way to design a form and store into database using Doctrine. Here is my task:
I have a form created with the FormBuilder containing various types of input fields. I have created entity which connects my database to form and it works both ways, both to fetch data and to put data into database.
However, I want to add a Select multiple field to a form which would be populated from a table from the database. And when I select the choices from that select element, I would like to save them into a database in either csv of id's or some simple serialized object or array.
So far, what I have managed to do is to add that Select field as EntityType and have placed ORM Annotation in entity for that element: Type("object"), which populates the Select element flawlessly. From the $_REQUEST variable I can see that it returns the id(s) of selected options. But, what I get when I submit the form is a very large serialized object in the field which is supposed to store selected element.
I suppose that it is a normal behavior (to store the entire object (an entire row of data)), but is there some cleaner, leaner way of storing only the id's that I would be able to later on use to populate the same select field, also using doctrine (since I don't have a need for other data in that field except for their IDs)?
Thank you in advance.

Symfony Set Drop Down Value Based Upon ID

I am still getting my hands dirty with Symfony, so I am a bit ignorant on how to do something that should be pretty simple.
I have a form that I am creating in the controller and passing to the view:
$form = $this->createForm(new PurchaseOrderType($account), $purchaseOrder);
The form displays exactly how I need it to, no problems at all! I am trying to now make the form more dynamic so that it can auto select a drop down list based upon an "id" variable that I am passing into the form. The id equals 23 by the way.
So, I have a drop down of suppliers and one of the options value is 23. How do I automatically select this option? Sorry for my ignorance :)
Thanks!
Without the code of your form type, I can make only suggestion.
If I get it correctly, inside the purchase order entity, there is an other entity mapped, and that one is represented with an id.
The object, $purchaseOrder has to have the other entity, then in the form type, when you set up the drop down field, you have to specify - with the correct name it shouldn't be a problem - the foreign id.
But you need of course data for the drop down field what can be the result of a SELECT * query.

Singleform with different database

I want to make a form in php where there will be two categories. One for the Student profile details and another for the student's
marks details. Now the main problem is all this will be in one single form means in one page and the categories like student details will be save in student detail table of the database and the marks will be save in another database called marks database. I also need CRUD for these two categories.So how to do that?
Any suggestion and help will be highly appreciable.
Well there is no direct link between a form and a database table, so what is your problem?
Why don't you do the mysql_query() on each table one after another when you handle the form post?
You can build your form like this in order to separate the tables:
<input type="text" name="data[student][name]" />
<input type="text" name="data[category][name]" />
you PHP script then goes through the data and save it in the corresponding tables:
foreach($_POST['data'] as $table=>$row){
// do your insert statemnet here (don't foregt to escape the Parameters!)
}

HTML form with multiple submit options

I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for transfering data.
For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.
However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).
Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?
The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.
<input type="submit" name="delete[1]" value="delete">
if (isset($_POST['delete'])) $id=key($_POST['delete']);
it seems to submit always just the last ID
It submits all of them, but since the name doesn't end with [], PHP discards all by the last.
Is there any way to submit just the ID field of the corresponding item without using multiple forms?
No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.
If I understand your goals correctly, you have two main options.
Put one form per row (in the cell with the delete button)
Encode the id value into the name of the submit button
You could get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}

Categories