How to set the multiple default value in laravel livewire - php

i have a multiple product when i get the default value of that product, but when i get that value in input field the default value doesn't show ?
<x-filament::page>
<form wire:submit.prevent="submit">
<div>
#if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
</div>
<select wire:model="user_type">
<option value="">Select User Type.....</option>
<option value="admin">Admin</option>
<option value="distributor">Distributor</option>
<option value="retailer">Retailer</option>
</select><br><br>
<input wire:model="name" type="text" placeholder="Name"><br><br>
<input wire:model="email" type="email" placeholder="Email Address"><br><br>
<input wire:model="password" type="password" placeholder="Password"><br><br>
<div align = "center">Products</div>
** #foreach($product as $val)
<br><br> {{ $val->name }} :
<input type="number" wire:model="default_price" value = "{{$val->default_price}}">
#endforeach<br><br>**
<button type="submit">
Submit
</button>
</form>
</x-filament::page>

Check these screencasts. They are very helpful! https://laravel-livewire.com/screencasts/data-binding
<input type="number" wire:model="default_price" value = "{{$val->default_price}}">
Livewire doesn't use value attribute!
If you want to update Product->default_price, you can use https://laravel-livewire.com/docs/2.x/properties#binding-models or if you want to only output the default_price, then remove wire:model.
Also #foreach($products as $val) $products should be plural,
and $val should be $product, because it is a product variable. Naming is important, if you don't want to confuse yourself and others constantly.

Related

How to store an array as single row for each value in my database

the whole idea is that when a user click on checkbox and save, the form should be submitted with an array containing the checked values, and save them in the database as single row for each value. i don't know if this is possible but i'm sure you guys have a solution or can help me?
and here's my form:
<div class="">
<form style="width: 70%" method="POST" action="{{ URL('/admin/projects/services/save') }}"
enctype="multipart/form-data">
#csrf
<input type="hidden" name="id" id="id" value="{{ #$id }}">
<input type="hidden" name="projectid" id="projectid" value="{{ #$projectid }}">
<div class="row topSpacer">
<div class="col-3">
<p class="bold">Selected Services: <span class="red">*</span></p>
</div>
<div class="col-9">
<input type="text" id="selectedServices" name="selectedServices" value="" readonly>
</div>
</div>
<div class="row topSpacer">
<div class="col-3">
<p class="bold">Select a service: <span class="red">*</span></p>
</div>
<div class="col-9">
#foreach ($services as $service)
<input type="checkbox" name="service" value="<?php echo stripslashes($service->id); ?>">
<label for="service"><?php echo stripslashes($service->name); ?></label>
<br>
<hr>
#endforeach
#error('service')
<span class="text-danger">{{ $message }}</span>
#enderror
</div>
</div>
<div class="row topSpacerHuge">
<div class="col-12 textRight">
<button type="button"
onclick="window.location='{{ url('admin/projects/services/index/'.$projectid) }}'"
class="goBackBtn">Cancel</button>
<input class="saveBtn" id="saveBtn" type="submit" value="Save">
</div>
</div>
</form>
</div>
and my script to get checkboxes value:
<script>
$(function() {
$("input:checkbox[name='service']").click(function() {
$value = [];
$.each($("input:checkbox[name='service']:checked"), function() {
$value.push($(this).val());
$array = $value.join();
});
// console.log(value.join(", "));
$("#selectedServices").val($array);
})
});
</script>
for implementing checked values I recommend to use the select tag
and you can give A name attribute with [] and it will collect all of your data
for example , look at my project's file which I'm trying to collect name of some users :
<select name="users[]" id="example-with-maxHeight" class="multiselect-group" multiple >
#foreach($users as $user)
<option #if($user->average < $averageParticipate )class="alert-danger" #endif value="{{$user->id}}">{{$user->name}} - {{$user->average}}</option>
#endforeach
</select>
have attention to users[] in name attribute
now in your request you can access all of your checked items .
if we look at your code ,can be some thing like this :
<label for="service"><?php echo stripslashes($service->name); ?></label>
<select name="services[]" id="" class="multiselect-group" multiple >
#foreach($users as $user)
<option value = "{{$sevice->id}}"> <?php echo stripslashes($service->name); ?></option>
#endforeach
</select>
now in your controllers you can access them :
request->get('services');
it will give you back an array of selected items .
Notice that id attribute helps me to interact with my java script code
you can rename it to yourselves .

Php Dropdown menu - Connect to another DB and show them

i work with vuejs3 and laravel 9 on one project.
i have two DB_table. Employee and company.
Company has only id and company name.
Employee contains: id, first name, last name, email, and company_id as forign key, which was joined to id in company_table.
If you want to create a new employee, you have to enter above mentioned infos and select a company name from selectbox (which is shown in previously entered companies) and save it in the table. How can I do then?
#extends('mitarbeiter.layout')
#section('content')
<div class="card">
<div class="card-header">Mitarbeiter Page</div>
<div class="card-body">
<form action="{{ url('mitarbeiter') }}" method="post">
{!! csrf_field() !!}
<label>Vorname</label></br>
<input type="text" name="vorname" id="vorname" class="form-control"></br>
<label>Nachname</label></br>
<input type="text" name="nachname" id="nachname" v-model="mitarbeiter.nachname" class="form-control"></br>
<label>Email</label></br>
<input type="text" name="email" id="email" class="form-control"></br>
<label>Company</label></br>
<select>
<option value="">------Select Company-------</option>
<option name="companyname">
{{option.companyname}}
</option>
</select></br>
</br>
<input type="submit" value="Save" class="btn btn-success"></br>
</form>
</div>
</div>
#stop
You can use foreach like this :
<select name="company" class="form-control">
#foreach ($companies as $company)
<option value="{{ $company->id }}">{{$company->name}}</option>
#endforeach
</select>
and in the controller :
$company = Companies::find($id);
$employee = new Employee();
$employee->user_id = $request->company; //name of select
$employee->company_id = $id;
$employee->save();
hope this would help

Can't send collection to bootstrap-select in Laravel form

Here is an edit form I've created
#extends('main')
#section('title', 'Edit Post')
#section('content')
<form class="form-group" action="/categories/update" method="POST">
{{csrf_field()}}
<input name="_method" type="hidden" value="PUT">
<label for="category_id" id="category_id">Select Category</label>
<select class="form-control" id="category_id" style="height:auto;" name="category_id">
#foreach($categories as $category)
<hr class="custom-hr-heading"/>
<option value="{{$category->id}}" {{($category->id == $post->category->id)?"selected='selected'":""}}>
{{$category->name}}
</option>
#endforeach
</select>
<label for="title" class="mt-2" name="title" id="title">Your Post Title</label>
<input type="text" class="form-control mb-2" value="{{$post->title}}" name="title" id="title">
<textarea class="form-control" name="body">{{$post->body}}</textarea>
<label for="tags" id="tags" name="tags">Select Tags..</label>
<select class="selectpicker mt-3" id="tags[]" name="tags[]" multiple="multiple">
#foreach($tags as$tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
#endforeach
</select>
<br/>
<button type="submit" class="btn mt-3 btn-success btn-sm">Update</button>
</form>
<script type="text/javascript">
tinymce.init({
selector: 'textarea',
theme: 'modern',
height: 300
});
$('.selectpicker').selectpicker({
style: 'btn-info'
});
</script>
#stop
Controller
public function edit($id)
{
$post = Post::find($id);
$categories = Category::orderBy('name', 'asc')->get();
$tags = Tag::orderBy('name', 'asc')->get();
return view('posts.edit', compact('post', 'categories', 'tags'))
}
I get a themed select box that just says nothing selected. Am I passing the tags collection to the select box correctly?
I'm using Laravel 5.5, PHP 7.2, and Bootstrap 4.
I've tried bootstrap-select via CDN and local files.
You are missing a space
Change:
#foreach($tags as$tag)
To:
#foreach($tags as $tag)
As per Bootstrap 4's documentation, this is a short attribute, not a name-value pair, so I would do it like so (and clean it up a little):
<select class="form-control" id="category_id" style="height:auto;" name="category_id">
<option value="">Choose an Option</option>
#foreach($categories as $category)
<hr class="custom-hr-heading"/>
<option value="{{ $category->id }}" #if($category->id == $post->category->id) selected #endif >
{{ $category->name }}
</option>
#endforeach
</select>
The problem was infact two problems.
Bootstrap 4 (final) support is only available in the latest bootstrap-select beta release here
Also my code was a little out. Here is the final working code.
<select class="selectpicker mt-3 form-control" name="tags[]" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{$post->tags->contains($tag->id)?"selected='selected":""}}>{{$tag->name}}</option>
#endforeach
</select>

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

Laravel pagination is not working with date search

I have a couple of search forms in my app in which pagination works great, but when it comes to date search I only get the first page, when I try to go to the second page I get an undefined $ticket variable. When I look at the URL I can see that it doesn't take the date values with him to the next page.
Here is my code:
<tbody class="searchTable">
#foreach($ticket as $tickets)
<tr>
<td>
{{Carbon::parse($tickets->created_at)->format('m/d/Y')}}
</td>
<td>{{$tickets->id}}</td>
<td>{{$tickets->clientName}}</td>
<td>
{{Carbon::parse($tickets->dueDate)->format('m/d/Y')}}
</td>
<td>{{$tickets->refNumber}}</td>
<td>{{$tickets->invoiceNumber}}</td>
<td>{{$tickets->jobLocation}}</td>
<td>{{$tickets->workDescription}}</td>
<td>{{$tickets->jobStatus}}</td>
</tr>
#endforeach
</tbody>
{!! $ticket->appends(Request::only('_token','search'))->render() !!}
This is the controller:
$ticket = DB::table('tickets')
->whereBetween('created_at', [$newDateFrom, $newDateTo])
->orderBy('created_at', 'desc')
->paginate(10);
return view('ticketsView', compact('ticket'));
Here is my solution:
I have the same view for different search requests so I append the results to the paginator so it will have it on the next pages as well. The name is the value of the name attribute in the search form. In my case I had multiple. Here is an example:
{!! $ticket->appends(Request::only(['dateFrom'=>'dateFrom', 'dateTo'=>'dateTo', 'search'=>'search', 'filter'=>'filter', 'dueDateFrom'=>'dueDateFrom','dueDateTo'=>'dueDateTo']))->render() !!}
So now if my search results will contain in the URL dateTo and dateFrom values for example then it will be saved to all pages. It's important to understand that these values come from the name attribute of your search form.
Here is an example:
<form class="form-horizontal" role="form" method="GET" action="/ticket/searchresults" accept-charset="UTF-8" enctype="multipart/form-data">
<div class="text-centered">
<p><strong>Search by dates:</strong></p>
</div>
<div class="form-group">
<label for="filter">Select Client (optional)</label>
<select class="form-control" name="filter" type="text">
<option disabled selected> -- select client -- </option>
#foreach($selectClient as $selectClients)
<option value="{{$selectClients->name}}">{{$selectClients->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="dateFrom">From:</label>
<input class="datepicker2 form-control" name="dateFrom" type="text" required/>
</div>
<div class="form-group">
<label for="dateTo">To:</label>
<input class="datepicker2 form-control" name="dateTo" type="text" required/>
</div>
<button type="submit" class="btn btn-primary"><span class="fa fa-fw fa-search"></span></button>
</form>

Categories