How could i add attribute in database with different languages - php

/*controller_ problem is that always add the some attribute for the different languages */
`
if ($request->isMethod('POST') && null !== ($request->request->get('ajouter')))` {
$pack = new Packs();
$pack->setPackPrice($request->request->get('pack_price'));
$pack->setDataCreated(new \DateTime('now'));
$pack->setDataStatus($request->request->get('data_status'));
foreach ($listLanguages as $language) {
$packLanguage = new Packs2lng();
$packLanguage->setLanguage($language);
$packLanguage->setPack($pack);
$packLanguage->setPack2lngWording($request->request->get('pack_2lng_wording'));
$packLanguage->setPack2lngDescription($request->request->get('pack_2lng_description'));
$em->persist($packLanguage);
}
//view_ I think that my input request must be variable
{% for i in listLanguages %}
<div class="form-group">
<label class="control-label col-md-3">Titre </label>
<div class="col-md-4">
<input type="text" name="pack_2lng_wording" data-required="1" class="form-control" value="{% if packLanguage.pack2lngWording is defined%}{{packLanguage.pack2lngWording}} {%endif%}"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Description </label>
<textarea cols="60" rows="5" class="span4" name="pack_2lng_description">
{% if packLanguage.pack2lngDescription is defined%}{{packLanguage.pack2lngDescription}} {%endif%}</textarea>
</div>
{%endfor%}

Let's say the users enters a French and English translation. With your current view, the post request would be this:
pack_2lng_wording=foo_FR
pack_2lng_description=bar_FR
pack_2lng_wording=foo_EN
pack_2lng_description=bar_EN
That won't work, because the first pack_2lng_wording would be overridden by the second.
To prevent it, you have to add [] to your name attribute of an input:
<input type="text" name="pack_2lng_wording[]" />
Or add {{ i.code }} to get a associative array (assuming that each Language has a language code, i.e. a ISO 639-1 code) :
<input type="text" name="pack_2lng_wording['{{ i.code }}']" />
That would lead to this post request:
pack_2lng_wording[FR]=foo_FR
pack_2lng_wording[EN]=foo_EN
pack_2lng_description[FR]=bar_FR
pack_2lng_description[EN]=bar_EN
In your controller you have to add [$language]:
foreach ($listLanguages as $language) {
$packLanguage = new Packs2lng();
$packLanguage->setLanguage($language);
$packLanguage->setPack($pack);
$packLanguage->setPack2lngWording($request->request->get('pack_2lng_wording')[$language->getCode()]);
$packLanguage->setPack2lngDescription($request->request->get('pack_2lng_description')[$language->getCode()]);
}
BTW: I would highly recommend to use Symfony Forms. You are now reinventing the wheel. That's OK if you want to learn, but it will make your life easier (and your application) better if you are using components that are well documented, well maintained, etc.

Related

Laravel 8 pass variable to be used in foreach loop for comparison

I'm trying to pass a predefined variable with a value into the #if part of the statement for comparison. This is for Laravel v8.0 on the blade.php. It only works if I hardcoded the value in it but not through the variable $value. I would need to have a placeholder variable to pass the value for comparison, as it will be inputted from user. Hence may I know what is the proper way to declare the variable as in this case? Sorry I'm just starting learning Laravel. Thanks for your help.
Eg:
$value = "abc123";
#foreach($surveys as $survey)
#if($survey->Unit_code == {{$value}})
<form action="" method="">
#csrf
<div class="form-group">
<label for="name">Name</label> <br>
<input type="text" name="Name" class="form-control" value="{{$survey->name}}">
</div>
<input type="submit" value="Save">
</form>
#endif
#endforeach
Just so this is in an answer form for other users that encounter this problem as a developer just learning laravel; the issue here is there was an output to the dom using {{}} where the blade directives (things that start with #) use the PHP vars as is an example of this would be as follows:
#if($survey->Unit_code == $value)
the other issue he was having, in this case, is assigning raw PHP on the blade file, this works the same way. Although, to add raw PHP to the dom you would use the #php directive
#php
$value = "abc123";
#endphp
#foreach($surveys as $survey)
#if($survey->Unit_code == $value)
<form action="" method="">
#csrf
<div class="form-group">
<label for="name">Name</label> <br>
<input type="text" name="Name" class="form-control" value="{{$survey->name}}">
</div>
<input type="submit" value="Save">
</form>
#endif
#endforeach
if the data of $value needed to be completely dynamic based on the backend then I would recommend passing it to the blade file via the view() function instead and assigning the value there or as nullable on the first pass of the function. Hope that helps!
You dont have to use {{ }} in #if condition .
When you use #if( ) everthing inside of it can translate into php .
So , you instead of using :
#if($survey->Unit_code == {{$value}})
Use :
#if($survey->Unit_code == $value )
Hope that works .

Laravel Livewire dynamically creating properties in a component

Is it possible to dynamically create properties in a livewire component for "model wiring" radio inputs, using foreach loop in mount method? I keep getting error (property not found on the component).
component:
component
view:
view
set the wire model correctly
don't set value. The value comes from the component. Use dotted notation
<label><div><input type="radio" wire:model="ksb.{{$key}}"></div>learning</label>
<div class="row">
<label for="Contact" class="col-form-label">learning</label>
<input type="text" wire:model="ksb.{{ $key}}.learning" class="form-control" placeholder="learning">
<label for="Contact" class="col-form-label">skills</label>
<input type="text" wire:model="ksb.{{ $key}}.skills" class="form-control" placeholder="skills">
<button wire:click="NextStep('{{ $key }}')">Next Step</button>
</div>
In Component Use This
public function NextStep($key)
{
$this->ksb[$key]['learning'] = '';
}

multiple value from dropdown in livewire

I have a dropdown and text input in a form. Form creates a course and I want to select classroom for course from dropdown and write its capacity to text input. But a course may have mutiple classrooms. How can I keep these classrooms and their capacity and show them in a table under the form?Like this:
Here's my code that keeps only one classroom and capacity
<div class="sm:col-start-1 sm:col-end-3 col-span-3 ">
<x-select x-on:change="isShowing = $event.target.value" name="classroom_id" label="{!! __('Classroom') !!}" wire:model="classroom_id"
id="classroom_id"
:options="$this->classrooms"/>
<x-jet-input-error for="classroom_id" class="mt-2" />
</div>
<div class="col-span-2 sm:col-span-1 " x-show="isShowing">
<label class="tf-form-label" for="capacity">
{{ __('Capacity') }}
</label>
<input wire:model.debounce.250ms="capacity" type="text" name="capacity" id="capacity" class="tf-input" />
<x-jet-input-error for="capacity" class="mt-2" />
</div>
$this->form->save();// firstly course saved
$classroom = new Classroom();
$classroom->course_id = $this->form->id;
$classroom->classroom_id = $this->classroom_id;
$classroom->capacity = $this->capacity;
$classroom->save();
Add the following codes in your component.
Component
public $classroom_id, $capacity;
$course = Course::create($validatedCourseData); // firstly course saved
// $this->classroom_id is array, because we set "multiple" in blade file
$course->classrooms()->attach($this->classroom_id);
Blade
Please add multiple in in your select box code.
<div class="sm:col-start-1 sm:col-end-3 col-span-3 ">
<x-select x-on:change="isShowing = $event.target.value" name="classroom_id" label="{!! __('Classroom') !!}" wire:model="classroom_id"
id="classroom_id"
:options="$this->classrooms" multiple/>
<x-jet-input-error for="classroom_id" class="mt-2" />
</div>
<div class="col-span-2 sm:col-span-1 " x-show="isShowing">
<label class="tf-form-label" for="capacity">
{{ __('Capacity') }}
</label>
<input wire:model.debounce.250ms="capacity" type="text" name="capacity" id="capacity" class="tf-input" />
<x-jet-input-error for="capacity" class="mt-2" />
</div>

How to show the old data in a form input in the Laravel

I write the form.blade.php like this:
<div class="form-group">
<label>Start at</label>
<div id="start_date" class="input-group date form_datetime col-md-4" data-date-format="yyyy-mm-dd hh:ii:ss">
<input class="form-control" name="start_at" size="16" type="text" value="{{ $activity->start_at }}">
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
</div>
Both Create and Edit method use the view to display the form. In the Edit method, the $activity is used and everything is normal. But in the Create method, the $activity is null, so I have a error in {{ $activity->start_at }}.
I know I can use if(!empty($activity)) to prevent this error. but I do not want to use this anywhere.
What is the better way?
You could use this,
{{ $activity->start_at or '' }}
I also like to reuse code and use the same view to create or edit an object. What I do is to pass a brand new instance of the object to the create view. This way I have the object on its initial state including defaults (if any) and I am able to prefill those defaults (if any) on the displayed form. It gives me an additional benefit: if anything goes wrong with the validation at the server level the user doesn't lose any data, I just need to do something like this in the view:
<div class="form-group">
<label for="test_field" class="control-label">Fecha de firma</label>
<input type="text" value="{{ ( !empty($isabi) ? $isabi->fecha_firma : old('fecha_firma')) }}"
name="fecha_firma" id="isabi-fechafirma" class="form-control" placeholder="Este campo es obligatorio">
</div>
This is the create method with extra functionality removed for clarity (check if the user is authenticated, linking the created object with the company the user works for and other stuff):
public function create()
{
return view('isabis.create', [
"isabi" => new Isabi()
])->with($this->data);
}
I use $this->data for view configuration. Hope this helps.

Laravel 4 When i retrieve info from database into input text, 1 extra space is added

I am doing a form where the user can update his information. The form shows several inputs and the info retrieved from the database is displayed inside them.
I have noticed that, when the info is displayed, one extra space is added. For example, if i retrieve a date '1965-08-29', then in the form i notice that an extra space is added at the end: '1965-08-29 '.
This is happening in all inputs causing validation fails.
Here is a sample code about how i retrieve the data inside the controller:
$id_user=Auth::user()->id;
$usuario=User::find($id_user);
if(isset($usuario->nombre_pila)){$nombrepila=$usuario->nombre_pila;}else{$nombrepila='';}
if(isset($usuario->ap_paterno)){$ap_paterno=$usuario->ap_paterno;}else{$ap_paterno='';}
if(isset($usuario->ap_materno)){$ap_materno=$usuario->ap_materno;}else{$ap_materno='';}
Here is how i send these data to the view:
return View::make('profile.edit',array('id_user'=>$id_user, 'nombrepila'=>$nombrepila, 'ap_paterno'=>$ap_paterno,'ap_materno'=>$ap_materno));
Here are some examples of the inputs in the view:
<div class="form-group #if ($errors->has('nombre_pila')) has-error #endif"><!--Nombre de pila-->
<label for="nombre_pila" class="col-sm-2 control-label">Nombre(s) de pila:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="nombre_pila" name="nombre_pila" maxlength="45" value="#if(isset($nombrepila)){{$nombrepila}} #endif">
</div>
#if($errors->has('nombre_pila'))
{{$errors->first('nombre_pila')}}
#endif
</div><!-- ----------fin nombre de pila ------------ -->
<div class="form-group #if ($errors->has('ap_paterno')) has-error #endif"><!--apellido paterno-->
<label for="ap_paterno" class="col-sm-2 control-label">Apellido paterno:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="ap_paterno" name="ap_paterno" maxlength="45" value="#if(isset($ap_paterno)){{$ap_paterno}} #endif">
</div>
#if($errors->has('ap_paterno'))
{{$errors->first('ap_paterno')}}
#endif
</div><!-- ----------fin de apellido paterno ------------ -->
I am using bootstrap 3 forms (i don't know whether this has to do with the problem or not)
It could be that i should use something like the rtrim() function. But if someone out there could explain me why this is happening or what i am missing to do, i'll appreciate it.
You have a space before your #endifs. To fix that either do
value="#if(isset($ap_paterno)){{$ap_paterno}}#endif"
or (a bit nicer in my opinion)
value="{{ (isset($ap_paterno) ? $ap_paterno : '') }}"

Categories