I am trying to retrieve old value from my form on the edit form, but it seems impossible to find an answer, because everyone in the world decided to use Form::select from laravelcollective/html library. I am trying to use the regular HTML to work this, and it's not able to retrieve old value from the form.
This is my HTML Code.
<div class="col-xs-12 form-group">
<label class="control-label">Role*</label>
<select name="roles[]" data-placeholder="Choose a Role for this User..." class="chosen-select" multiple tabindex="4">
<option value="">Select</option>
#foreach ($roles as $key => $value)
<option value="{{ $key }}" {{ in_array($key, old("roles")) ? "selected":"") }} >{{ $value }}</option>
#endforeach
</select>
</div>
This is my controller code.
public function edit($id)
{
$roles = Role::get()->pluck('name', 'id');
$user = User::findOrFail($id);
return view('admin.users.edit', compact('user', 'roles'));
}
I am trying to convert the following into regular html
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('role', trans('global.users.fields.role').'*', ['class' => 'control-label']) !!}
{!! Form::select('role[]', $roles, old('role') ? old('role') : $user->role->pluck('id')->toArray(), ['class' => 'form-control select2', 'multiple' => 'multiple', 'required' => '']) !!}
<p class="help-block"></p>
#if($errors->has('role'))
<p class="help-block">
{{ $errors->first('role') }}
</p>
#endif
</div>
</div>
Try this:
<div class="col-xs-12 form-group {{ $errors->has('role') ? 'has-error' : '' }}">
<label for="role" class="control-label">Role *</label>
<select class="form-control select2" name="role[]" id="role" required multiple>
#foreach ($roles as $id => $label)
<option value="{{ $id }}" #if (collect(old('role', $user->role->pluck('id') ?? []))->contains($id)) selected="selected" #endif>{{ $label }}</option>
#endforeach
</select>
<span class="help-block">
#if ($errors->has('role'))
{{ $errors->first('role') }}
#endif
</span>
</div>
In your controller's update/store function, make sure you either use the built-in validation methods, which will populate the session on any errors.
or
If you have other checks, make sure you return the input when redirecting back.
return redirect()->back()
->withInput() // <- return input with redirect, will populate session
->with('error', 'Persist failed'); // <- optional
Related
i'm trying to build a polls system using livewire but i got some errors
here is my blade
#foreach ($questions as $index => $question)
<div class="row">
<div class="box-header with-border">
<i class="fa fa-question-circle text-black fs-20 me-10"></i>
<h4 class="box-title">{{ $question->title }}</h4>
</div>
<div class="box-body">
<div class="demo-radio-button">
#foreach ($answers as $key => $answer)
<input wire:model="question{{ $question->id }}" type="radio"
id="answer{{ $question->id }}{{ $key }}"
class="radio-col-primary" value="{{ $key }}">
<label
for="answer{{ $question->id }}{{ $key }}">{{ $answer }}</label>
#endforeach
#error('question')
<div class="text-danger text-bold">{{$message}}</div>
#enderror
</div>
</div>
</div>
#endforeach
my Livewire class
public $question = [];
public function render() {
$category = PollCategory::findOrFail($this->category->id);
$subCategories = PollSubCategory::where('poll_category_id', $category->id)->get();
$answers = PollAnswer::all();
return view('livewire.polls', compact('category', 'subCategories', 'answers'));
}
The Error is
Property [$question41] not found on component: [polls]
Any help please ?
you did it wrong way,
solution:
<input wire:model="question.{{ $question->id }}"
$question{{ $question->id }} equal $question41
$question.{{ $question->id }} equal to $question[41]
that's why u receive error Property [$question41] not found on component
<div class="row">
<div class="col-md-6">
<h3>Create New Expenses</h3>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form" method="POST" action="{{ route('exp.store') }}" autocomplete="off">
<div class="form-group">
<label>Seller :</label>
<input type="text" name="seller_name" class="form-control" value="{{ old('seller_name',isset($exp) ? $exp->seller_name : '') }}" placeholder="Seller">
</div>
<div class="form-group">
<label>Date :</label>
<input type="date" name="date" class="form-control" value="{{ old('date',isset($exp) ? $exp->date : '') }}">
</div>
<div class="form-group">
<label>Select Type :</label>
<select class="form-control" name="category" value="">
<option value=""> Select Type </option>
<option value="1"{{ isset($exp) ? ($exp->category == 1 ? 'selected' : '') : '' }}> Communication </option>
<option value="2"{{ isset($exp) ? ($exp->category == 2 ? 'selected' : '') : '' }}> Transport </option>
<option value="3"{{ isset($exp) ? ($exp->category == 3 ? 'selected' : '') : '' }}> Tools </option>
<option value="4"{{ isset($exp) ? ($exp->category == 4 ? 'selected' : '') : '' }}> Raw Material </option>
</select>
</div>
<div class="form-group">
<label>Amount(RM) :</label>
<input type="number" name="amount" class="form-control" value="{{ old('amount',isset($exp) ? $exp->amount : '') }}" placeholder="0.00">
</div>
<br>
<button type="submit" class="btn btn-primary btn-sm">Save</button>
<a class="btn btn-danger btn-sm" href="{{ route('exp.index') }}">Cancel</a>
</form>
</div>
</div>
This is my saving data function
public function store(Request $request){
$this->validate($request, [
'seller_name' => 'required|max:50',
'date' => 'required',
'category' => 'required',
'amount' => 'required'
]);
DB::beginTransaction();
try{
$exp = new Exp();
$this->saveData($exp, $request);
DB::commit();
return redirect()->route('exp.index');
} catch (\Exception $ex){
DB::rollback();
return back()->withInput()->withErrors('Fail to save.');
}
}
private function saveData($exp, Request $request){
$exp->seller_name = $request->input('seller_name');
$exp->date = $request->input('date');
$exp->category = $request->input('category');
$exp->amount = $request->input('amount');
$exp->save();
}
This is my Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Exp extends Model
{
protected $table = 'exp';
public $primaryKey = 'exp_id';
}
I'm new using this MVC .... i don't know why cant't saving but all my code look no error.
I try to find it but i find nothing ..
I need some help...
i already provide my view, controller and model
and below is my web.php
Route::get('/exp/create', 'ExpController#create')->name('exp.create');
Route::post('/exp/store', 'ExpController#store')->name('exp.store');
TQ.
Seems like you forgot to add mass assignment fields to your Exp model, hence the fields are not saved in your database. Add all the fields you want to be mass assigned to your model, and it should be working as expected:
protected $fillable = ['field_1', 'field_2', .... 'etc'];
Read more about this on official documentation
I tried to do the input dropdown filter using this code:
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category){
<option value="{{$category->Umur}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
However, my code does not run as I expected (in picture below) - the code only call the first row in database. What should I edit with the code?
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category){
<option value="{{$category->Umur}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
remove the { after #foreach & change the value like
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category)
<option value="{{$category->id}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
I too had this issue myself, this is what i used:
<div class="form-group">
<div class="btn-group">
<input list="brow" class="form-control"name="instagram_account_id" id="instagram_account_id" placeholder="Search..">
<datalist id="brow">
#if($users->count() > 0)
#foreach($users as $user)
<option value="{{$user->id}}">{{ $user->username }}</option>
#endforeach
#else
No User(s) Found
#endif
</datalist>
</div>
</div>
As for my controller:
public function index()
{
$comments = Comment::paginate(10);
$users = InstagramAccount::all();
return view('instagramComments', [
'comments' => $comments,
'users' => $users,
]);
}
And (if you're going to use it an a form) my insert in my controller:
public function insert(Request $req)
{
$instagram_comment = $req->input('instagram_comment');
$instagram_account_id = $req->input('instagram_account_id');
$data = array('comment'=>$instagram_comment, 'account_id'=>$instagram_account_id);
DB::table('comments')->insert($data);
return redirect('/comments');
}
(if using the insert above, use this in your web route):
Route::post('/locationsInsert', 'InstagramLocationsController#insert')->middleware('auth');
Hope any of this helps!
I'm trying to create a page that lets the user select a product and then when they press "add to cart" that product then gets sent to the cart page.
On my product page I can have the same item that has 2 or more codes attached to it, so I've created a selection that has the codes and the user then has to pick one of them
and then press "add to cart" button to go to the cart page.
So the way I have it at the moment is that the form grabs the id of the product page and not of the selection box.
What I would like to know is how would I go about changing the route depending on what I've selected.
Here is my code
<form action="{{ route('product.addToCart', ['id' => $product->id]) }}" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-12 pl-0">
<select class="form-control mb-2" id="supplier_code" name="supplier_code">
#foreach($parent_product as $parent)
<option value="{{ $parent->supplier_code }}">{{ $parent->supplier_code }}</option>
#if(count($parent->parent))
#foreach($parent->parent as $child)
<option value="{{ $child->supplier_code }}">{{ $child->supplier_code }}</option>
#endforeach
#endif
#endforeach
</select>
</div>
</div>
{{ Form::submit('Add to Cart', array('class' => "btn btn-dark btn-lg btn-block")) }}
</form>
This is my function
public function getAddToCart(Request $request, $id)
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$contacts = Contact::all();
$product = Product::find($id);
$supplier_code = $request->supplier_code;
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id, $supplier_code);
$request->session()->put('cart', $cart);
return view('public.shopping-cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice, 'menus_child' => $menus_child, 'contacts' => $contacts, 'supplier_code' => $supplier_code]);
}
This is my product list page where the user will click on a product to get its details.
<div class="col-xl-9 col-lg-8 col-md-12 product-list">
<div id="parent">
<div class="row grid-container">
#foreach($products as $product)
<?php
$cat_slug = "";
?>
#foreach($product->category as $category)
<?php
$cat_slug .= " ".$category->slug
?>
#endforeach
<?php
$product_image = getImagesArray($product->image);
?>
<div class="product_items col-xl-2 col-lg-4 col-md-4 col-sm-4 {{ $cat_slug }}">
<a href="{!! route('product.item', [$product->slug]) !!}">
<div>
#if(!empty($product_image))
<img src={!! "product_images/products/$product_image[0]" !!}>
#endif
<p>
{!! $product->title !!}
</p>
</div>
</a>
</div>
#endforeach
</div>
</div>
</div>
I'm not sure if I need to add anything else, so if I do need to please let me know
Change this:
<a href="{!! route('product.item', [$product->slug]) !!}">
to this:
<a href="{{ route('product.item', $product->id) }}">
Hi i have this in my controller
$listPatient = Patient::get();
return view('backend.consultations.create')->with([
'patient' => $this->patient,
'patient_id' =>$this->patient_id,
'listPatient' => $listPatient,
]);
and in my view i have
<div class="form-group">
<div class="col-md-2">
List clients
</div>
<div class="col-lg-10">
<select name="patient_id">
<option value="0">Veillier séléctionner un patien </option>
#foreach($listPatient as $key)
<option value="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-10-->
</div><!--form control-->
and it work fine,i want to use Form::select but it doesn't work can anyone help me please
In your controller you would want the following to fetch $listPatient
$listPatient = Patient::lists("nom_patient","id")->toArray();
and in your view you would want
{!! Form::select('patient_id', [0 => 'Veillier séléctionner un patien'] + $listPatient, null) !!}
a simple solution
enter code here : <div class ="form-group">
{{ Form::label('list_patient', trans('validation.attributes.backend.consultations.nom_patient'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
<select id="patient_id" name="patient_id" class="form-control select2 box-size" required="required" placeholder=" Non définie" >
<option value="">Non définie</option>
#foreach($listPatient as $key)
<option value ="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-3-->
</div><!--form control-->