Make dynamic checkbox "checked" when id match - php

<div class="tab-content">
#foreach($groups as $group)
<div role="tabpanel" class="tab-pane" id="grp-{{ $group->id }}">
#foreach($ingreds as $ingred)
#if($ingred->id_ingredient_group==$group->id)
<label>
<input type="checkbox" name="ingredients[]" #foreach($item->ingredients as $ingredient) #if($ingred->id==$ingredient->data->id) {{"checked"}} #endif #endforeach value="{{$ingred->id}}"> {{ $ingred->name}}
</label>
#endif
#endforeach
</div>
#endforeach
</div>
I want if "$ingred->id==$ingredient->data->id" than my checkbox become checked but it is not happening.

Related

Get checked value in edit blade Laravel 8

im trying to get selected checkbox in edit blade. but nothing work.
Here is my code, maybe someone could tell me where i miss
<label>Permasalahan</label>
<div class="row">
<div class="col">
#foreach ($problems as $problem)
<div class="form-check">
<input type="checkbox" id="" name="problem_id[]" {{ $problem->id == 'problem_id' ? 'checked' : '' }}/>
<input type="checkbox" id="" name="problem_id[]" value="{{ $problem->id }}">
<label class="fs-6 fw-light">{{ $problem->name }}</label>
</div>
#endforeach
</div>
</div>

add Product size to cart (laravel shoppingcart)

I have followed a tutorial to create a laravel shopping cart https://www.youtube.com/watch?v=Jzi6aLKVw-A&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=3 and composer https://github.com/hardevine/LaravelShoppingcart
I want make button select size product
CartController.php
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
Cart::add($request->id, $request->name, 1, $request->price, $request->data)->associate('App\Product');
return redirect()->route('cart.index')->with('success_message', 'Item was added to your cart!');
}
Product.blade.php
<h1 class="topic">{{ $product->name}}</h1>
<h4>{{ $product->price }}</h4>
<div class="size-b">
<div class="title">
<h5>Trousers</h5>
</div>
<form action="{{ route('cart.store') }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $product->id }}" >
<input type="hidden" name="name" value="{{ $product->name }}" >
<input type="hidden" name="price" value="{{ $product->price }}" >
<div class="btn-group" role="group" aria-label="Basic">
<button> <input type="checkbox" name="data" value="S" class="btn btn-secondary"><span>S</span></button>
<button><input type="checkbox" name="data" value="M" class="btn btn-secondary">M</button>
<button><input type="checkbox" name="data" value="L" class="btn btn-secondary">L</button>
<button> <input type="checkbox" name="data" value="XL" class="btn btn-secondary">XL</button>
</div>
<button type="submit" class="btn btn-secondary">ซื้อสินค้า</button>
</form>
cart.blade.php
#if (Cart::count() >0 )
<h4>{{ Cart::count() }}Item(s) in cart</h4>
<div class="table-cart-destop">
#foreach (Cart::content() as $item)
<div class="row">
<div class="col-lg">
<div class="remove">
<form action="{{ route('cart.destroy', $item->rowId) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-secondary">X</button>
</form>
</div>
</div>
<div class="col-lg-2">
<div class="image-border">
<img src="{{ $item->model->photo1}}" class="img-cart" alt="product-cart">
</div>
</div>
<div class="col-lg-5">
<div class="content">
<h4>{{ $item->model->name}}</h4>
<p>{{ $item->model->details}}</p>
</div>
</div>
<div class="col-lg">
<p class="size">size {{ $item->model->data }} </p>
</div>
<div class="col-lg">
<div class="quality">
<form action="">
<div class="form-row align-items-center">
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="3">5</option>
</select>
</div>
</div>
</form>
</div>
</div>
<div class="col-lg">
<p class="size">{{ $item->data}}฿</p>
</div>
</div>
#endforeach
<div class="tatal">
<p>ยอดชำระเงินทั้งหมด</p>
<p class="price-tatal">{{ Cart::subtotal() }}฿</p>
</div>
<div class="buy-now">
<a href="{{ route('checkout.index') }}">
<button type="submit" class=" btn btn-secondary">สั่งซื้อสินค้า</button>
</a>
</div>
I try dd($request->all());
run php artisan serve --> size product (don't show)

Passing dates from form into blade view

I have a shopping cart that has a pick-up and return date option that needs to be selected. I cannot get either of them to be echoed into the cart view when they are selected. I am using the Laravel Shopping Cart library to build the cart, which has an array for the extra options. I have passed the values into there to pass into the view, but it doesn't seem to work.
Here is the form mark:
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">Pick Up Date</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1of2" style="float: left;">
<label class="label" for="location" style="float:left;">Pick Up Location</label>
<select name="location" id="location" class="location">
<option>please choose</option>
<option value="bakersfield">Bakersfield</option>
<option value="chico">Chico</option>
<option value="fresno">Fresno</option>
<option value="hayward">Hayward</option>
<option value="manteca">Manteca</option>
<option value="oakley">Oakley</option>
<option value="redwood_city">Redwood City</option>
<option value="sacramento">Sacramento</option>
<option value="salinas">Salinas</option>
<option value="san_jose">San Jose</option>
<option value="san_jose_fusion">San Jose Fusion</option>
<option value="santa_rosa">Santa Rosa</option>
</select>
</div>
</div>
</fieldset>
<input type="hidden" name="id" value="{{ $rental->id }}">
<input type="hidden" name="title" value="{{ $rental->title }}">
<input type="hidden" name="pickupDate" value="{{ $rental->pickupDate }}">
<input type="hidden" name="returnDate" value="returnDate">
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Add to Cart</button>
</div>
</div>
</form>
Here is the cart view:
<article>
#if(session()->has('success_message'))
<div class="alert alert-success">
{{ session()->get('success_message') }}
</div>
#endif
<h1>Shopping Cart</h1>
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Cart::count() > 0)
<h2>{{ Cart::count() }} item(s) in Shopping Cart</h2>
<div>
<div>
#foreach (Cart::content() as $item)
<fieldset>
<article class="js-cart-product">
<p class="prod-title">Name: {{$item->model->name}} </p>
<p class="pu-date">Pick up date: {{ ($item->options->has('pickupDate') ? $row->options->pickup : '') }} </p>
<p class="rtn-date">Return Date: {{ ($item->options->has('returnDate') ? $row->options->pickup : '') }}</p>
<p class="loc">Location: {{$item->location}}</p>
<form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Delete Item</button>
</div>
</div>
<!-- <div class="cart__footer">
<p class="cart__text">
<a class="button" href="#" title="Buy products">
Check Out
</a>
</p>
</div> -->
</form>
</article>
</fieldset>
#endforeach
</div>
</div>
#else
<h3>No items in Cart!</h3>
Return to Rental Equipment
#endif
</div>
</article>
And here is the create item part of the controller:
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
$this->validate($request, array(
'location'=>'required',
));
Cart::add($request->id, $request->title, 1, $request->location, $options = ['pickup' => 'pickupDate', 'returnDate' => 'returnDate'])
->associate('App\Rental');
Session::flash('success', 'The item was successfully save!');
return redirect()->route('cart.index');
}
From your form blade
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">
Pick Up Date
</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
then in the bottom somewhere of your master/app blade you put
<script>
$(function() {
$("#pickupDate" ).datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
<script type="text/javascript">
$(function() {
$("#returnDate").datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
be sure to include these necessary files jquery.min.js, and if you use bootstrap then you need bootstrap.min.js, bootstrap-datepicker.min.js
Please let me know if it works

How to add model variable to another model in Laravel

My problem is this I have $collaborator (model) variable in my view. In the view there is form related with $permission model. Now I need add $permission to My view file. How can I do it my blade view is this
I need print status value of the permission table on this blade file. That I need get $permission here.
My blade file is
#if($collaborators) // I need add $permission here
#foreach( $collaborators as $collaborator)
<div>
<div>
<span>
<img src="{{ $collaborator->user()->first()->getAvatarUrl() }}" />
{{ $collaborator->user()->first()->username}}
{{ $collaborator->user()->first()->id}}
</span>
</div>
<button class="btn btn-sm btn-danger delete" style="margin-top:5px;padding:4px;width:35px;"
data-action="/projects/{{ $project->id }}/collaborators/{{ $collaborator->collaborator_id }}"
data-token="{{csrf_token()}}">
<i class="fa fa-trash-o"></i>
</button>
<!-- permission start -->
<form class="form-vertical" role="form" method="post" action="{{ route('projects.collaborators.permission', $project->id,$collaborator->id) }}">
<!--<div id="cid" name="cid">{{ $collaborator->user()->first()->id}}</div>-->
<input type="hidden" id="cid" name="cid" value="{{ $collaborator->user()->first()->id }}" />
<div class="form-group{{ $errors->has('status') ? ' has-error' : '' }}">
<label for="status" class="control-label">Choose Permission</label>
<select name="status" id="status">
<option value="">Choose a status</option>
<option value="3">View Only</option>
<option value="2">Edit Tasks</option>
<option value="1">Admin</option>
</select>
#if ($errors->has('status'))
<span class="help-block">{{ $errors->first('status') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Create</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token()

Laravel modal loads only last entry

I have been trying to use modal to create and edit entries in Laravel. I am using simple modal instead of ajax. I can add entries, but when I try to edit an entry, it only loads the last entry in the modal.
Here is my index (from where I am calling modal)
#foreach ($clients as $client)
<tr>
<td>{{ $client->name }}</td>
<td><button data-target="modal2" class="btn modal-trigger1">Edit</button></td>
</tr>
#endforeach
Here is edit file (modal file)
<div id="modal2" class="modal">
<div class="panel panel-default">
<div class="modal-content">
<p class="flow-text">Edit client</p>
<form class="form" role="form" method="POST" action="{{ url('/client/'. $client->id) }}">
<input type="hidden" name="_method" value="patch">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="name" value="{{ $client->name }}">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</form>
</div>
</div>
</div>
And the edit in controller is
public function edit($id)
{
$clients = Client::findOrFail($id);
return view('client.edit', compact('clients'));
}
It only loading the last entry in the table, not the entry id in the row.
I am using Laravel 5.3. Please help
Set data to your edit link
#foreach ($clients as $client)
<tr>
<td>{{ $client->name }}</td>
<td>Edit</td>
</tr>
#endforeach
//On edit button click get data and set data to modal
$(document).on('click', '.edit-city-link', function(){
var client_id = $(this).data('client-id');
var client_name = $(this).data('client-name');
$('#client-id').val(client_id);
$('#client-name').val(client_name);
$('#edit-client-modal').modal('show');
});
//This is modal
<div id="modal2" class="modal">
<div class="panel panel-default">
<div class="modal-content">
<p class="flow-text">Edit client</p>
<form id="edit-client-modal" class="form" role="form" method="POST" action="{{ url('/client/edit') }}">
<input type="hidden" name="_method" value="patch">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="control-label">Name</label>
<input id="client-id" type="hidden" class="form-control" name="client-id">
<input id="client-name" type="text" class="form-control" name="name">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</form>
</div>
</div>
</div>
This is your controller method
public function edit($request_data)
{
$obj_client = Client ::find($request_data['client-id']);
$obj_client = $request_data['client-name'];
$obj_client->save();
}
Add {{$client->id}} to your data-target in the button and also add it to your modal id :-
<td><button data-target="modal2 {{$client->id}}" class="btn modal-trigger1">Edit</button></td>
<div id="modal2 {{$client->id}}" class="modal">
Finally, put the modal inside the foreach loop.

Categories