Add row manually to eloquent result in Laravel 4.2 - laravel

I am using Laravel 4.2 and i fetch all locations with this code:
$locations = Location::all();
This Locations are displayed in a select box afterwards. How can i add an additional row to the results in order to show an empty first option in the select box.
The options then should be:
choose a location
location 1
location 2
...
I just want to add an additional item to the result in $locations.
Thanks in advance

You can use:
{!! Form::select('location', ['' => 'Select your location'] + $locations, null , ['class' => 'form-control']) !!}
to update the view. In Laravel 5 there are attribute accessors to append an extra field with your eloquent collection.
Few other ways to do this are:
$locations[null] = 'choose a location';
Form::select('location', $locations);
Form::select('location',[null=>'Please Select'] + $locations);
Another way is to loop through the result and update it. Use json_decode() or 'toArray()` to convert your result into array format.
Otherwise you have to store choose a location as the first row value in your locations table(I know that is inappropriate for the requirement).

You should look at the put method for collections:
https://laravel.com/docs/5.6/collections#method-put
This method is not available in 4.2. You should make a custom Collection class and use it in your model by overwriting the newCollection method. https://laravel.com/docs/4.2/eloquent#collections

Related

The merge method doesn't work in voyager laravel

I override the Laravel controller but now i don't what's the problem is coz it show me that (Field 'lab_price' doesn't have a default value) I used the merge method when I debugged its good and has a value.
$val = $this->validateBread($request->all(), $dataType->addRows)->validate();
$l_price = DB::table('labs')
->where('id', $request->lab_id)
->value('price');
$request->merge(['lab_price' => $l_price])
->all();
$data = $this->insertUpdateData($request, $slug, $dataType->addRows, new $dataType->model_name());
Actually i have lab_price column in child table and i want to get the (price) from the parent table and store in the child table whenever the lab_name is selected from the fronted.
Also if this method doesn't possible here, then is it possible through laravel observer?
I also go through it but i didn't find join in observer??

Retrieve Value from Field in Laravel Cast Array

I have a table named settings, in it, there are several columns, id,company_id, key, value.
In my Laravel model for Setting, I have the following cast:
protected $casts = [
'value' => 'array',
];
But when I go to retrieve data that has been stored, I can't.
For example, I have a record with the following value: "{\"default_remit_address\":\"2395\"}"
And when I go to retrieve the record in a Blade, it does pull it up correctly, but I'm not sure how to grab a specific value from the value field (like default_remit_address).
If I print the return "{{$settings->value}}" directly in the Blade, this is what I get:
{"default_remit_address":"2395"}
So how can I go one level deeper?
As this json object is being cast to an Array, you can just use regular array syntax to access it's contents.
$default_remit_address = Settings::find(1)->value['default_remit_address'];
or in your blade template
{{ $settings->value['default_remit_address'] }}

Generate form with drop down using Gii in Yii

I am new to yii.I want to create model and form using Gii,In my database table i used enum data type for specific values to select by user.than i generate model and crude using Gii but it doesn't create drop-down list in form for that enum field.To create that drop-down using Gii what should i have to do. or change templates of Gii.
please help me. Thanks in advance.
change the _form and put your dropdown after creation by gii
echo $form->dropdownList($model , 'fieldName' , array(
'private' => Yii::T('model' , 'private'), // give data as an array to populate your drop down
'public' => Yii::T('model' , 'public'),
))

Adding an 'All' option into a codeIgniter form_dropdown

I have an array that returns locations from the database, which I am trying to output into a form drop down using the following code:
<?php form_dropdown('idLocation', $queryLocations, set_value('idLocation'); ?>
The $queryLocations has the locationID and the locationName. And this above code works fine to display all locations, now I need to add an option called 'All' having idLocation as zero to appear at the top of location list.
Can someone please help me do this?
Just prepend the $queryLocations array with your values. Here's one way:
form_dropdown(
'idLocation',
array('0' => 'All') + $queryLocations,
set_value('idLocation')
);
You could probably do this earlier, when you actually create the $queryLocations array as well.
$options[0] = 'All';
foreach ($results as $r) $options[$r->idLocation] = $r->locationName;
Something like that...

Magento addFieldToFilter allow NULLs

When using the Magento collection method addFieldToFilter is it possible to allow filtering by NULL values? I want to select all the products in a collection that have a custom attribute even if no value is assigned to the attribute.
I see you already found a solution, but there is also this option:
$collection->addFieldToFilter('parent_item_id', array('null' => true));
But if you want to use "NULL" => false, which DOESN'T WORK.
(and I noticed you can use elements such as "in", "nin", "eq", "neq", "gt"), you can do this:
$collection->addFieldToFilter('parent_item_id', array('neq' => 'NULL' ));
Hope this is still helpful...
This works for NOT NULL filters
$collection->addFieldToFilter('parent_item_id', array('notnull' => true));
Filtering a product collection by null/empty attributes has two possible solutions. Magento uses an INNER JOIN to grab the values of the attributes to filter. BUT if the product attribute is not assigned a value the join will fail, as a database table / relationship is missing.
Solution #1: Use addAttributeToFilter() and change the join type from "inner" (the default) to "left":
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('custom_attribute', array( ... condition options ..), 'left');
Solution #2: Make sure your custom attribute has a default value. Magento is conservative in this regard. Magento will only create the relationship between an attribute and a product if a value is given for the attribute. So in the absence of user-specified value or a default value the attribute will not be accessible for filtering a product even if the attribute appears in the product detail view under the admin panel.
Because the question does not match exactly the title of the question and I found the them multiple times by searching for a condition like: special VALUE OR NULL
If you want to filter a collection matching a VALUE OR NULL, then you can use:
$collection->addFieldToFilter('<attribute>', array(
array('eq' => '<value>'),
array('null' => true)
));
You don't need to use addFieldToFilter.
now the solution:
attributes name is known as code in magento, you just need to use the code below to get all of the products which have a specific attribute as an array
$prodsArray=Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('custom_attribute_code')
->getItems();
you can also specify certain conditions for attribute's value in addAttributeToFilter in the second parameter of addAttributeToFilter.
you can find this method in this file (for further study):
app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php

Resources