Generate form with drop down using Gii in Yii - drop-down-menu

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'),
))

Related

Laravel Nova - Map database json column to field

I have a database model with json column type called attributes. This column will contain additional details of the model.
For example:
attributes : {'desc' : 'test', 'width' : '500'}.
I would like to display individual attribute (desc & width) in attributes column as a html field in Laravel admin resource.
I checked supported field types and doesn't seem supports.
Any suggestions how I can do this?
Here's how I was able to accomplish this in Nova 2.0. In my case I have a JSON column called fields. In the model, fields is cast to an array.
protected $casts = [
'fields' => 'array'
];
Then in the resource, the column is mapped using the -> operator.
Text::make('Name', 'fields->name')
I'm not sure if it's the recommended answer but i've found a way that should work for you.
Use an Eloquent Mutator: Eloquent Mutators
Then you define a getDescAttribute and a setDescAttribute and pass it in nova like a property on the model like so:
Text::make('Desc', 'desc')
Hope this helps.
For example the value for 'desc' column is something like :
"{attributes : {'desc' : 'test', 'width' : '500'}}"
You can directly put it in table format :
KeyValue::make('Desc', 'desc**.attributes**')
Hope this helps
There is a package that allows you to achieve what you want to without any unnecessary changes to the behaviour of your model.
https://github.com/64robots/nova-fields
This would allow you to define each key of JSON array as actual fields that can be modified.

Add row manually to eloquent result in Laravel 4.2

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

cakebake not working with prefix in cakephp3

I am using cakephp 3.4.9. When I am using a table with prefix n field its working properly after baking but if I use prefix in table fields its not working.
Like when I am using post with following fields like
id,
post,
date
it's working fine but if I use following fields its not working
p_id,
p_post,
p_date
it is adding extra codes in model
$this->belongsTo('Ps', [
'foreignKey' => 'p_id',
'joinType' => 'INNER'
]);
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['p_id'], 'Ps'));
return $rules;
}
why ps is adding here? If I use articales table like same its become As.
Please help.
I would like to suggest you, read this article.
CakePHP naming convention documentation
In cakePHP framework everything you have to keep in mind while creating the table is the CakePHP naming conventions. In your case, This is happening because cakePHP expects the primary column of any table will be only 'id', and the foreign key for the table will be the Related table name with an underscore id
(ex: If product table BelogsTO categories you have to make a column in your product table as category_id)
In your case cakePHP considering p_id as a foreign key for the table P. And by default cakePHP has a validation for the forein key that the existsIn which means that while saving that p_id, it will check for the existance of id in P table.
In one sentense this is because of the naming convention issue. You can change only p_id to id and keeping other things same will work for you.
HAPPY CODING :)

Laravel validation: unique with multiple columns and soft_delete

I am trying to do a Laravel validation rules as follow:
"permalink" => "required|unique:posts,permalink,hotel_id,deleted_at,NULL|alpha_dash|max:255",
The explanation to the rules is:
I have a table "Posts" in my system with the following fields (among others): hotel_id, permalink, deleted_at. If MySQL would allow make an unique index with null values, the sql would be:
ALTER TABLE `posts`
ADD UNIQUE `unique_index`(`hotel_id`, `permalink`, `deleted_at`);
So: I just add a new row IF: the combination of hotel_id, permalink and deleted_atfield (witch must be NULL) are unique.
If there is already a row where the permalink and hotel_id field are the same and 'deleted_at' field is NULL, the validation would return FALSE and the row wouldnt be inserted in the database.
Well. I don't know why, but the query Laravel is building looks like:
SELECT count(*) AS AGGREGATE FROM `posts`
WHERE `hotel_id` = the-permalink-value AND `NULL` <> deleted_at)
What the heck...
The query I was hoping Laravel build to validation is:
SELECT count(*) AS AGGREGATE FROM `posts`
WHERE `permalink` = 'the-permalink-value' AND `hotel_id` = ? AND `deleted_at` IS NULL
Could someone explain me how this effectively works? Because everywhere I look it looks like this:
$rules = array(
'field_to_validate' =>
'unique:table_name,field,anotherField,aFieldDifferentThanNull,NULL',
);
Does anyone could help me?
Thank you
all.
Finally, I got a proper understanding of the validation (at least, I think so), and I have a solution that, if it is not beautiful, it can helps someone.
My problem, as I said before, was validate if a certain column (permalink) is unique ONLY IF other columns values had some specific values. The problem is the way Laravel validation string rules works. Lets get to it:
First I wrote this:
"permalink" => "required|unique:posts,permalink,hotel_id,deleted_at,NULL|alpha_dash|max:255",
And it was generating bad queries. Now look at this:
'column_to_validate' => 'unique:table_name,column_to_validate,id_to_ignore,other_column,value,other_column_2,value_2,other_column_N,value_N',
So. The unique string has 3 parameters at first:
1) The table name of the validation
2) The name of the column to validate the unique value
3) The ID of the column you want to avoid (in case you are editing a row, not creating a new one).
After this point, all you have to do is put the other columns in sequence like "key,value" to use in your unique rule.
Oh, easy, an? Not so quickly, paw. If you're using a STATIC array, how the heck you will get your "currently" ID to avoid? Because $rules array in Laravel Model is a static array. So, I had to came up with this:
public static function getPermalinkValidationStr() {
$all = Input::all();
# If you are just building the frozenNode page, just a simple validation string to the permalink field:
if(!array_key_exists('hotel', $all)) {
return 'required|alpha_dash|max:255';
}
/* Now the game got real: are you saving a new record or editing a field?
If it is new, use 'NULL', otherwise, use the current id to edit a row.
*/
$hasId = isset($all['id']) ? $all['id'] : 'NULL';
# Also, check if the new record with the same permalink belongs to the same hotel and the 'deleted_at' field is NULL:
$result = 'required|alpha_dash|max:255|unique:posts,permalink,' . $hasId . ',id,hotel_id,' . $all['hotel'] . ',deleted_at,NULL';
return $result;
}
And, in the FrozenNode rules configuration:
'rules' => array(
'hotel_id' => 'required',
'permalink' => Post::getPermalinkValidationStr()
),
Well. I dont know if there is a easiest way of doing this (or a much better approach). If you know something wrong on this solution, please, make a comment, I will be glad to hear a better solution. I already tried Ardent and Observer but I had some problems with FrozenNode Administrator.
Thank you.

Laravel - validation localization doesn't work

Whenever I use the trans() function to return a specific translation from the validation.php file, it works just perfectly. I have two languages in my application and the translations get returned for both of them.
However, whenever I use the Laravel validator, it returns messages in the default locale only. Is there something I need to specify in the validator? How do I make it work for both languages?
You need to pass as 3rd parameters your translations. Let's assume you have defined your fields, rules and validator like in the following code:
$data = Input::only('title');
$rules['title'] = 'required|min:20|max:80',
$validator = Validator::make($data, $rules,
Lang::get('forms.validation.entry'));
Now you need to define your translations. Let's assume you need translation for fr lang so you need co create lang/fr/forms.php file and put the following content into it:
<?php
return
array (
'validation' => array (
'entry' => array (
'title.required' => 'Your translation for title required',
'title.min' => 'Your translation for title min',
'title.max' => 'Your translation for title max',
)
)
);
Of course you can create file with simpler array but it's just example - instead of forms.validation.entry it could be for example just forms or validation.
The problem originated from my localization implementation. I've added App::setLocale(Session::get('lang')); to the App::before() method in the filters.php file and it all works now.
in resources/lang/en folder laravel have validation.php. When you create new language so you can copy this file to new created language folder and then edit it to this language.
for example you wanna create fr language
.
/resources/lang/fr/validation.php and translate it to this language

Resources