This question is a follow-up/attempt to implement the answer from a prior question.
My attempt to pluck values from a collection and apply the current localization is as follows:
$prefix_array = ['' => trans('registration.prefixes.select')] +
$prefixes->pluck('prefix', 'prefix')->map(function($item, $key) {
return trans('messages.fields.prefixes.'.$item);
})->toArray();
However, this produces an array with values like:
"Mrs." => "messages.fields.prefixes.Mrs."
Instead of:
"Mrs." => "Sra." // eg: shortened Senora for Spanish translation
The localization path (messages.fields.prefixes.XYZ) is correct and references to it in other places displays as expected.
It may be trailing dot (period) is confusing the localisation. You may need to have your translation key as just 'mrs' => 'Mrs.'
Related
Pre Laravel 8 there seems to be an option to line up different states at once
$users = factory(App\User::class, 5)->states('premium', 'delinquent')->make();
(edit: I now realise that this was to apply 2 different states into the same factory record)
whereas I can't seem to find the equivalent of this in Laravel 8, does anybody know how?
Essentially I am trying to call one chain to create multiple records with different states for example:
User::factory()->verified()->unverified()->banned()->create()
Result: 3 different users one verified one unverified one banned
It can be achieved with below code, it just seems a little verbose
User::factory()->verified()->create();
User::factory()->unverified()->create();
User::factory()->banned()->create();
1. Get rid of states and just use sequences
User::factory()
->count(3)
->state(new Sequence(
['status' => 'verified'],
['status' => 'unverified'],
['status' => 'banned'],
))
->create()
2. Use variables as functions
Keep your current states, but dynamically access them from a predefined list.
$states = ['verified', 'unverified', 'banned'];
foreach ($states as $state) {
User::factory()->$state()->create();
}
3. Write each one out by hand.
Similar to number 2, but we don't use a loop.
User::factory()->verified()->create();
User::factory()->unverified()->create();
User::factory()->banned()->create();
I have a problem with laravel pluck on a map function.Generally I take payments from database like this:
$payments = Payment::with('translation')->active()->get();
And result is:
Each payments has different translations so I use function to get proper ones. For that I've added map function to query to get translation like this:
$payments = Payment::with('translation')->active()->get()->map(function ($payment) {
return ['key' => $payment->getName() . ' +' . $payment->cost, 'value' => $payment->id];
})
->pluck('key', 'value')
->prepend('Choose payment...');
And result is:
Array keys are different from payment ones. It should be like 1,9,12 etc.
If I switch order of pluck for value then key result will be with right keys:
But after that blade forms from htmlcollective ({!! Form::select() !!}) will show keys as dropdown text, and translations as value. (like opposite).
Is there any simple way to fix that?
It was prepend causing keys to be wrong. Any prepend without specified key will be automaticaly added as 0 (zero), and all other keys will count from there.
To fix change:
->prepend('Any text here');
to:
->prepend('Any text here', '');
Key will be set as '', and other keys will be not changed:
I have two collections: "Instructions" and "Known". Basically I am taking a new set of "Instructions" and checking whether anything is different to what is "Known".
So, the quantity is not massive. I retrieve the info:
$Instructions = Instruction::all();
$Knowns = Known::all();
Now, I'm looking for the differences, and I've tried each of these three methods:
$IssuesFound = $Instructions->diff($Knowns);
$IssuesFound = $Instructions->diffKeys($Knowns);
$IssuesFound = $Instructions->diffAssoc($Knowns);
The thing is, an "Instruction" or "Known" is an item with 17 attributes, and anyone of those attributes can be different. I want to compare the attributes of an "Instruction" with the matching attribute of a "Known". (Both items have the same keys, bot items have a Reference attribute to act as a unique identifier.
What I'm finding is that theese methods give me the item that is different, but doesn't tell me which individual attributes are the mismatch.
foreach ($IssuesFound as $issue)
{
dd($issue);
}
So a method like $IssuesFound = $Instructions->diffKeys($Knowns); will come up with item xxx being different, but I can't see how to find out which attribute of the item it is that is different. Not unless I start nesting loops and iterating through all the attributes - which I'm trying to avoid.
How do I do it?
Thanks in advance. (Laravel 5.6)
Straight from laravel docs, diffAssoc will return what you are asking:
$collection = collect([
'color' => 'orange',
'type' => 'fruit',
'remain' => 6
]);
$diff = $collection->diffAssoc([
'color' => 'yellow',
'type' => 'fruit',
'remain' => 3,
'used' => 6
]);
$diff->all();
// ['color' => 'orange', 'remain' => 6]
You get the attribute from the FIRST collection that is different on the SECOND collection, therefore if you get 3 attributes when calling $diff->all() you will know WHICH attributes ARE DIFFERENT, so you could access them or do whatever you want to, if you post more specific results of what you are getting and what you are trying we can help, but I think you are just not thinking how to use these methods
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
I'm importing a bunch of csv entries in my database with Laravel 4.
I can't really point at one column that has to be unique, it's a combination of 5 columns that makes it unique. However: how does one define this in Laravel?
Option 1: schema builder
You can use the $table->unique('email') method, but that only seems to allow one column, not a combination of columns.
Option 2: Validation
Less preferable, but I could validate the model before inserting it. However, again, using 'unique:[table]' validation rules, it will return an error when just one of the column values isn't unique, not a combination of them.
Can anyone tell me how I should go about this?
I'm sure I'm missing something, but I could use a push in the right direction :-)
Thanks,
Dieter
You can combine:
$table->unique( array('email','name') );
And pretty much everything in Laravel will accept arrays to do whatever you need to with 'more than one'.
Use Schema Builder's unique() method to define your data model, as Antonio mentioned.
Additionally, if you want to use validation on your model, consider my custom Validator rule for multiple UNIQUE indexes: https://github.com/felixkiss/uniquewith-validator
You can also do this;
$table->unique(["column1", "column2"], 'uq_columns');
Which means that you will have a unique column combination of all the columns i.e. column1 and column2
I know this question is for Laravel 4, but I just came across this on searches and found a solution for Laravel >= 5.3
Here it is:
Of course, the migration may look something like
$table->unique( array('email','name') );
Then to validate this, you do not need to use custom rules, just advanced rules:
'email' => Rule::unique('users')->where(function ($query) use ($request) {
return $query->where('name', $request->name);
}),
Of course, you may want to validate name before of this. The name should be required so that you may finish with something like this:
'name' => 'required|max:255',
'email' => Rule::unique('users')->where(function ($query) use ($request) {
return $query->where('name', $request->name);
}),
I hope it helps.
You can try this
$table->string("name");
$table->string("email")->unique("name")