laravel queries in controller displays a variable - laravel

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']
);

Related

how to check if cart has already added not allow more and update if different cart

I'm using composer require gloudemans/shoppingcart I am not sure how to maintain amount.
When i'm using one route that says add item when i'm using this route adding multiple item
How can i conditionally setup to add item in cart if this is unique
public function bookItem($id) {
$item = Item::where([
'status' => '1',
'id' => $id
])->first();
$product = Cart::add($item->id, $item->name, 1, $item->price); // This should not call always if it has not generated a row id then it should call
Cart::update($product->rowId, ['price' => 200]); // Will update the price if it is differ
return redirect()->route('book.item', ['id' => $item->id]);
}
I am not sure how to manage it. please guide
Looks like the package has a getContents() function that gathers all items into a collection. It also has a search(Closure) function that calls getContents() and then uses Laravel's filter function on the collection and returns the result.
$search = Cart::search(function($key,$value) use ($item) {
return $value === $item->id;
})->first();
if(!empty($search)){
Cart::update($search->rowId, ['price' => 200]);
}
else {
$product = Cart::add($item->id, $item->name, 1, $item->price);
}
Definitely check out the Laravel collection docs if you aren't familiar. This is the entey on filters:
https://laravel.com/docs/5.8/collections#method-filter

Laravel pagination per_page/limit does not get added to nextPageUrl

When using Model::paginate($per_page) the pagination is returned including the per_page variable. The problem is that the when you get the nextPageUrl() it only provides a ?page=x url variable, so if you want to specify the per_page in the url e.g.
https://example.com/api/users?page=1&per_page=100
It doesn't get added by the LengthAwarePaginator to the next/prev urls.
Is there something I'm missing or do I have to manually append this to the URL?
If you want to add that to every single url there's a method in your paginator that allows you to add query strings.
Laravel 5.4+ :
//fetch the get parameter per_page, if doesn't exists defaults it to 100
$per_page = \Request::get('per_page') ?: 100;
$users = App\User::paginate($per_page);
$users->appends(['per_page' => $per_page])
return view('users', ['users' => $users]);
Old solution 5.3:
//fetch the get parameter per_page, if doesn't exists defaults it to 100
$per_page = \Request::get('per_page') ?: 100;
$users = User::paginate($per_page);
$users->addQuery('per_page', $per_page);
return view('users', ['users' => $users]);

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 )
);

Why does my data not pass into my view correctly?

I have a model, view and controller not interacting correctly, and I do not know where the error lies.
First, the controller. According to the Code Igniter documentation, I'm passing variables correctly here.
function view() {
$html_head = array( 'title' => 'Estimate Management' );
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('html_head', $html_head);
$this->load->view('estimates/view', $estimates);
$this->load->view('html_foot');
}
The model (short and sweet):
function get_estimates() {
$query = $this->db->get('estimates')->result();
return $query;
}
And finally the view, just to print the data for initial development purposes:
<? print_r($estimates); ?>
Now it's undefined when I navigate to this page. However, I know that $query is defined, because it works when I run the model code directly in the view.
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('estimates/view', $estimates);
You're loading the return of $this->Estimatemodel->get_estimates() as the array of view variables. In other words, all the children of $estimates (assuming it can be treated as an array) are available in your view. But not the parent element.
The key here is when loading a view the second parameter needs to be an array of values, not just a single value.
$this->load->view('estimates/view', array('estimates' => $estimates));
That should get the result you're looking for, in fact, you're already doing that for the html header view. Even though that view only has one variable, it's passed as the single element of an array:
$html_head = array( 'title' => 'Estimate Management' );
$this->load->view('html_head', $html_head);
The documentation shows that the object you pass to the view must be an associative array.
$data = array(
'estimates' => $estimates
);
$this->load->view('estimates/view', $data);
Docs here

Resources