Adding a div row for a new row in Laravel - php

I am beginner in Laravel. I make my project in Laravel 8.
I have this code:
#foreach($productIngredients as $productIngredient)
#php
if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true){
$item = \App\Models\SelectedProductIngredient::where('product_id', $product->id)->where('ingredient_id', $productIngredient->id)->first();
$weight = $item->weight;
} else {
$weight = null;
}
#endphp
<div class="col-6">
<div class="form-check py-2">
<input id="productIngredientId-{{ $productIngredient->id }}"
class="form-check-input enableInput" style="margin-top:10px"
name="productIngredient[]" type="checkbox"
value="{{ $productIngredient->id }}"
#if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true) checked #endif>
<label class="form-check-label" for="flexCheckChecked">
{{ $productIngredient->name }} [{{ $productIngredient->short_name }}]
</label>
<input id="productIngredient-{{ $productIngredient->id }}" type="text"
name="productIngredient-{{ $productIngredient->id }}" maxlength="10"
class="form-control weight-input weightMask"
style="width:100px;display: inline; margin-left:20px" placeholder=""
value="{{ $weight }}">
</div>
</div>
#endforeach
It's work fine.
I would like there to be 2 records in 1 row.
So I would like to add at the beginning and close div in case of the last record
How can I do this?
Please help me.

Do you know about the Laravel collection? It can help you.
You can use chunk method, it breaks the collection into multiple, smaller collections of a given size:
#foreach($productIngredients->chunk(2) as $chunk)
<div class="row">
#foreach($chunk as $productIngredients)
<div class="col-6">
...
</div>
#endforeach
</div>
#endforeach

Related

Check only one checkbox and uncheck others using livewire

I'm trying to check only one checkbox at a time and others uncheck, but when I want to check a new checkbox then the previous will uncheck, and so on.
my code in blade:
#foreach($addressEmployer as $address)
<div class="col-12 col-lg-3 p-2 m-2 rounded" style="border: dashed #a1a1a1;">
<label for="check{{$address->id}}"></label>
<div class="row">
<div class="col-2 mt-5">
<input wire:model="addressSelected.{{$address->id}}"
value="{{$address->id}}"
class="form-check-input" type="checkbox"
id="check{{$address->id}}">
</div>
<div class="col-10">
<p> {{$address->province->name}} - {{$address->city->name}}</p>
<p> {{$address->address}}</p>
<a wire:click="setAddress({{$address->id}})" class="float-end"
data-bs-toggle="modal"
href="#editAddressModal"
role="button">{{__('Edit')}}</a>
<a wire:click="$emit('addressId',{{$address->id}})" class=" me-3 float-end"
data-bs-toggle="modal"
href="#deleteAddressModal"
role="button">{{__('Delete')}}</a>
</div>
</div>
</div>
#endforeach
I read the addresses from the database and display them with foreach and I want to select one of the displayed addresses.
I am looking for the right solution to this issue. Thanks if you have a solution.
When you have multiple options but want to limit selection to a single option, radio buttons are your friend.
If the previously selected radio button is not de-selecting, it suggests you haven't grouped your radio button elements correctly using the name attribute.
Here is a simplified example to get you on the right path.
Component
class AddressComponent extends Component
{
public $addresses = Address::all();
public $selectedAddressId;
public function mount()
{
$this->addresses = Address::all();
$this->selectedAddressId = 1;
}
public function render()
{
return view('livewire.address-component-view');
}
}
Component view
<div>
#foreach ($addresses as $address)
<div class="mt-1">
<input type="radio" id="{{ $address->id }}" name="address" value="{{ $address->id }}" wire:model="selectedAddressId" />
<label for="{{ $address->id }}">{{ $address->address }}</label>
</div>
#endforeach
<h3>Selected Address ID: {{ $selectedAddressId }}</h3>
</div>
I'm not shure why you're trying this with checkbox, instead of radiobutton. But maybe you can handle something like this
public $addressSelected = [];
protected $rules = [
'addressSelected.id' => 'nullable'
];
public function updatingAddressSelected($value)
{
$this->addressSelected = [];
}
// in blade add the wire:key directive to checkbox's root div
<div class="col-2 mt-5" wire:key="checkbox-id-{{ $address->id }}">
<input wire:model="addressSelected.{{$address->id}}"
value="{{$address->id}}"
class="form-check-input" type="checkbox"
id="check{{$address->id}}"
#if(in_array($address->id,$addressSelected)) checked #endif
>
</div>

Empty array from controller to view - Laravel

I'm new at Laravel, and I'm having some problems setting up an update for my application. I'm trying to pass an id from a view to a controller through routing to select an specific line, and after that I need to pass from the controller to another view. Idk where I'm doing wrong.
Here's my view code that passes de id:
#forEach($line as $data)
<tr>
<td><i class="icon ion-md-create"></i></td>
<td>{{$data->name}}</td>
<td>{{$data->time}}</td>
</tr>
#endforEach
Here's the route:
Route::get('/lineEdit/{id}', 'LineController#formEdit')->name('edit.line')->middleware('auth');
Here's the controller function from route:
public function formEdit($id){
$line = Line::find($id);
$lineUp = Line::select('*')
->where('id', $line)->get();
return view('lineEdit')->with('line', $lineUp);
}
And here's the piece of the view that will recieve the array:
<div class="card-body">
#forEach($line as $data)
<form method="POST" action="{{route('update.line', $data->id)}}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">
{{__('Name')}}
</label>
<div class="col-md-8">
<input type="text" name="name" class="form-control {{$errors->has('name') ? 'is-invalid' : ''}}" value={{$data->name}} required autofocus >
#if($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{$errors->first('name')}}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="time" class="col-md-4 col-form-label text-md-right">
{{__('Time')}}
</label>
<div class="col-md-8">
<input type="number" name="time" class="form-control {{$errors->has('time') ? 'is-invalid' : ''}}" value={{$data->time}} required >
#if($errors->has('time'))
<span class="invalid-feedback" role="alert">
<strong>{{$errors->first('time')}}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Save') }}
</button>
</div>
</div>
</form>
#endforEach
</div>
Everything inside the forEach doesn't render. I can't see the problem.
In your controller:
public function formEdit($id){
$line = Line::find($id);
return view('lineEdit', compact('line'));
}
I use the compact method to send the $line variable to the view.
Also, if you want to get the Line model to edit you dont need this:
$lineUp = Line::select('*')->where('id', $line)->get();
you only need this to find your model:
$line = Line::find($id);
Try this in your controller:
public function formEdit($id){
$line = Line::find($id);
return View::make('lineEdit', compact('line'));
}
This should pass a variable $line to your view
Looks like the problem is in the controller code.
$line = Line::find($id);
returns you an object (just one object!) of class Line if a) $id is an integer b) $id is the primary key c) this key exists in the DB
So, either $line is null or a model object. The next query
$lineUp = Line::select('*')->where('id', $line)->get();
cannot be successful in either case.
As I understand your intent $id is just an attribute (because you expect to have a collection of objects). So, try
$lineUp = Line::select('*')->where('id', $id)->get();

How to update checkbox field and option field in Laravel 5.2?

I can update text field of a form but i can not update checkbox field,option field and file field in Laravel 5.2. I i going to update then i can see all text value is perfectly shown in update blade view but in option field, checkbox field i see it is default value its don't comes from database. Again if i update with another option then its not saving.What i have tried so far:
My Controller:
public function update(Request $request, $id=0)
{
//
$id = $request->input("id");
$product = Product::find($id);
$product->product_name = $request->input('product_name');
$product->product_storage = $request->input('product_storage');
$product->product_percentage = $request->input('product_percentage');
$product->can_draw = $request->input('can_draw');
$product->real_price = $request->input('real_price');
$product->product_image = $request->input('product_image');
$product->save();
$request->session()->flash('alert-info', 'Product Successfully Updated!');
return Redirect::to('admin/product/all');
}
My update.blade.php View:
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.can_be_draw') }}</label>
<div class="col-md-9">
<div class="input-icon">
<i class="fa fa-money" aria-hidden="true"></i>
<select name="can_draw" class="form-control">
<option value="{{ $product->can_draw }}">{{ trans('common.open') }}open</option>
<option value="{{ $product->can_draw }}">{{ trans('common.close') }}close</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.real_prize') }}</label>
<div class="col-md-9">
<div class="input-icon">
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}"> {{ trans('common.yes') }}yes
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}" checked="checked"> {{ trans('common.no') }}no
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile" class="col-md-3 control-label">{{ trans('common.product_picture') }}</label>
<div class="col-md-9">
<input type="file" id="exampleInputFile" name="product_image" value="{{ $product->product_image }}">
<small>{{ trans('common.pic_summery') }}</small>
</div>
</div>
Just use this:
<input type="radio" {{$product->can_draw == 'Yes' ? 'checked' : ''}} class="form-control" name="real_price" value="Yes"> {{ trans('common.yes') }}yes
<input type="radio" {{$product->can_draw == 'Yes' ? '' : 'checked'}} class="form-control" name="real_price" value="No" > {{ trans('common.no') }}no
EDIT
I have edited the above*
You should change the value to Yes and No and use this to check if the value saved is Yes or No. It does not matter how much is real_price
your option field have same value ({{ $product->can_draw }})
<option value="value1"{{( $product->can_draw=='value1'?selected)}}>{{ trans('common.open') }}open</option>
<option value="value2 " {{( $product->can_draw=='value1'?selected)}}>{{ trans('common.close') }}close</option>
and for file field must use file not input:
$product->product_image = $request->file('product_image');
You just can save option value by requesting from select name.
In your example:
select name = "can_draw"
Just save value from request:
$product->can_draw = $request->can_draw;
For checkbox same:
$product->real_price = $request->real_price;
Not necessary indicate input() helper in request.
To retrieve all value for option and check box value, please use foreach methods:
#foreach($can_draws as $can_draw)
<option value="{{ $can_draw->id }}">{{ $can_draw->name}}</option>
#endforeach

Checkbox Duplication

I am trying to create an update page and i've completed most of the page, with exception to the checkbox section. For some reason that i've yet to figure out, the checkboxes are duplicated. I am using laravel.
This is the code for that particular section of the form.
<div class="form-group">
<label>Focus Area</label>
<br>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" checked>
#else
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}">
#endif
#endforeach
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>
Some extra information:
The number of elements in the array generated by FocusArea::all() is 5.
The number of elements in the array generated by getIdsOfFocusAreas() is 2.
I know that it is duplicating twice because of point number 2, im just not exactly sure why it is duplicating in the first place.
Try this:
<div class="form-group">
<label>Focus Area</label>
<br>
<?php $selectedFlug = 0; ?>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" {{ ($selectedFocusArea == $focusArea->focus_area_id) ? 'checked' : '' }} >
<?php $selectedFlug = 1; ?>
#break
#else
<?php $selectedFlug = 0; ?>
#endif
#endforeach
#if($selectedFlug == 0)
<input type="checkbox" name="focus-area[]" value="{{ $focusArea->focus_area_id }}">
#endif
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>

Post checkbox value's when using dynamic fields

I'm building a task list where I can add and delete fields dynamically for each user. The problem I have, is saving the data from the checkbox.
I'm using the following code:
<form>
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
#endforeach
/*The JS will add the new items here. JS example:*/
<div class="col-md-9">
<input id="taskfield" name="dynamic[]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
</form>
For saving it dynamically, I'm using this in my controller:
DB::table('tasks')->where('user_id', '=', $_POST['uid'])->where('week', '=', $_POST['week'])->where('year', '=', $_POST['year'])->delete();
if(isset($_POST['dynamic'])){
$takenvdweek = $_POST['dynamic'];
$finished = $_POST['check'];
var_dump($finished);
foreach($takenvdweek as $key => $value) {
DB::insert('insert into tasks (name, finished, user_id, week, year) values (?, ?, ?, ?, ?)', array($value, $finished[$key], $_POST['uid'], $_POST['week'], $_POST['year']));
}
}
So my Saving method requires always a value for the checkbox, 0 when unchecked and 1 when checked. I tried it with a hidden field with a 0, but it didn't work because the 0 was always in the POST array. Also when I click the a label, the first foreach label is changing, not the second where I click on. I tried to fix this with a class, but was no success.
Edit:
The foreach is for tasks that exists, but I can also add new items with a piece op JS, they are also inside the form, so I can't give them a foreach-id. I updated my question with the JS example.
Try to set the task id as index of dynamic and check
See below
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[$task->id]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[$task->id]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
#endforeach
--Edit---
if task id is not available then you can set index like below
$index = 0;
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[$index]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[$index]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
$index++;
#endforeach
This should do:
#foreach($clubs as $club)
<input type="checkbox"
id="{{$club->getCode()}}"
name="clubs[]"
value="{{$club->getId()}}" /> {{$club->getName()}}<br>
#endforeach

Categories