Laravel | Get the dropdown selected value after submit - laravel-5

I would like to get the dropdown selected value after submitting the form.
If I run a dd($_REQUEST); I can find the submitted value into an array :
"ordersdetail-productnameID" => array:1 [▼ 0 => "1" ]
I tried may variantes to get to my value from the array but without success :
$productnameID = array(
'ordersdetail-productnameID' => $input['productnameID']); or
$productnameID = $request->input('orders_detail.0.productnameID ');
Would appreciate some expertise.
Thanks, Cheers, Marc

Did you try this kind of solution in the controller?
If the name of the function in your controller is myFunction,
public function myFunction(Request $request){
$productnameID = $request->productnameID;
dd($productnameID);
}
check the output. You'll get the ID,

Solution is : $productnameID = $_REQUEST['ordersdetail-productnameID'][0];

Related

Passing data controller to view getting undefined variable, in Laravel 5.1

I want to passing data from controller to view
$participantView = view('section.participant', ['data' => $result['data']])->render();
the $result['data'] is array data(not empty)
I do foreach in view, and got:
undefined variable: data
I also do with compact and 'with', but also getting undefined variable
What is wrong in my code?
You must return the view, Try this :
return view('section.participant', ['data' => $result['data']]);
try using
$data = $result['data'];
return view('section.participant', compact('data'));
It will do work.
Try this:
$participantView = view('section.participant')->with([
'data' => $result['data']
])->render();
and in view you will have $data variable available.
I want to passing the data to view, then I will return $data. Which is:
$data=['data_array' => ['view' => $participantView ]];
in another function, the passing data was fine.

laravel queries in controller displays a variable

Right now i'm working with Crinsane Laravel Shopping cart. I'm using 5.0
My problem is: I cant turn the variable into values, instead system display it as it is (variable) .
I want to know how exactly query database with specific column base on id , and turn then into values not variable it self .
My code :
public function addcart (){
$products = Product::find(Input::get('id'));
$qty = Input::get('qty');
Cart::add('$products->id', 'products->name', $qty, $products->price, array('size' => 'large'));
$cart_content = Cart::content();
return view('pages.cart')->with('cart_content', $cart_content);
}
When run , it turn to display like this :
Don't wrap $products->id and $products->name in quotes:
Cart::add(
$products->id,
$products->name,
$qty,
$products->price,
['size' => 'large']
);

Magento: Custom attributes option's new value field

I want to add a custom field in Attribute option in Manage Attribute Options menu in admin. Like value column beside the position column in admin. I have attached an image.
What I have done ... (Magento 1.8)
created a new field in eav_attribute_option table named value after sort_order filed.
changed magento\app\design\adminhtml\default\default\template\eav\attribute\options.phtml this file to show the Value column beside the Position column.
changed getOptionValues() method in this file magento\app\code\core\Mage\Eav\Block\Adminhtml\Attribute\Edit\Options\Abstract.php to get data for my custom value column from database and show in admin side. It shows the default value of db in admin form.
But when I want to save from admin panel the data doesn’t save in db.
I've been trying to find the model or controller that actually handles writing into the database to make sure my new field saves too.
Any help would be appreciated.
(I'd post an image to make you understand, but I can't since I need 10 points to be able to do it.)
Got it!
Update: Mage/Eav/Model/Resource/Entity/Attribute.php
in function: _saveOption(Mage_Core_Model_Abstract $object)
change:
$sortOrder = !empty($option[’order’][$optionId]) ? $option[’order’][$optionId] : 0;
if (!$intOptionId) {
$data = array(
‘attribute_id’ => $object->getId(),
‘sort_order’ => $sortOrder,
);
$adapter->insert($optionTable, $data);
$intOptionId = $adapter->lastInsertId($optionTable);
} else {
$data = array(’sort_order’ => $sortOrder);
$where = array(’option_id =?’ => $intOptionId);
$adapter->update($optionTable, $data, $where);
}
for this:
$sortOrder = !empty($option[’order’][$optionId]) ? $option[’order’][$optionId] : 0;
$yourAttribute = (isset($option[’your_attr_field’]) && !empty($option[’your_attr_field’][$optionId])) ? $option[’your_attr_field’][$optionId] : ‘’;
if (!$intOptionId) {
$data = array(
‘attribute_id’ => $object->getId(),
‘sort_order’ => $sortOrder,
‘your_attr_field’ => $yourAttribute
);
$adapter->insert($optionTable, $data);
$intOptionId = $adapter->lastInsertId($optionTable);
} else {
$data = array(’sort_order’ => $sortOrder, ‘your_attr_field’ => $yourAttribute);
$where = array(’option_id =?’ => $intOptionId);
$adapter->update($optionTable, $data, $where);
}
I could use some help in making all this changes 'the Magento way'

Is there a shorter way than this to check values when using same Add form for Edit

I have the same code for an Add and an Edit form. Therefore in the controller I need a check to check if a) POST vars submitted (for saving), and if not then b) the original values (for editing) and if not then no value (blank for Adding). I put them in a $data array to pass to the view. Then in the form I can put:
value="<?php echo $member_id;?>"
So my question is, in Codeigniter is there a shorter way than the following to check if POST, then if not check if the original data exists, and if not then its nothing.
$data = array(
'member_id' => ( isset($_POST['member_id']) ? $_POST['member_id'] : (isset($member->member_id ) ? $member->member_id : '') )
);
I know about set_value() but looks like that wont add in the current data when editing a form, so have not used that.
You can allways make function for it.
function get_value_or_default($array, $key, $default) {
return isset($array[$key] ? $array[$key] :
isset($default) ? $default : '';
}
Or even better:
function update_from_post($object) {
$data = array();
foreach ($object as $prop_name => value) {
$value = get_value_or_default($_POST, $prop_name, $object->{$prop_name});
$data[$prop_name] = $value;
}
Assuming you have different methods in the controller for create vs edit: (you can use the same view in different methods by specifying it in $this->load->view()):
Your create method would assume it was new, and always read the $_POST variables (if $_POST)
Your edit method would first load the object from the database, and then overwrite with $_POST variables if present.
Finally, CodeIgniter has the input helper:
$this->input->post('field_name');
returns false if that field is not in $_POST.
To use your code above:
create
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : '')
);
edit
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : $member->member_id )
);

codeigniter update particular database row

I'm trying to setup my codeigniter install to allow me to update my database. I have managed to do this using :
function updateValues(){
$this->load->model("get_db");
$newrow = array(
array(
"id" => "3",
"name" => "bruce"
),
array(
"id" => "4",
"name" => "billybob"
)
);
$this->get_db->update2($newrow);
echo "update successful" ;
}
This works and updates rows 3 and 4. I now need to update a single row based on my active user ID. I've tried several lines of code found in many different places via google. Code such as:
echo $this->db->insert_id()
All this does is return a '0'.
So my question is how to I echo or return the current active users ID stored in the database.
I'm pretty sure I've missed something basic as I'm new to codeigniter. Any help would be greatly appreciated.
Thanks
Tim
Simplest way to do this is store the userId in the session when they log in, then you can just retrieve the session variable any time you need it.
Login routine
$this->db->where('userName',$userName);
$this->db->where('password',$password);
$data = $this->db->get('users');
if($data->num_rows() ==1)
{
$userData = $data->result();
$this->session->set_userdata('userId',$userData->userId);
}
Then any time you need it you just recall that userId
$userId = $this->session->userdata('userId');
$newrow = array(
'id'=>$userId,
'name'='Cornelius'
);

Resources