I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.
Related
I have a select box which needs to fetch from the database.
view blade:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($service as $sn)
{
<option value="">dd($sn)</option>
}
#endforeach
</select>
controller:
public function index()
{
$service = DB::select('select service_type from services');
return view("service",['service'=>$service]);
}
I am getting an error like
Undefined variable: service (View: /home/devadmin/.config/composer/vendor/laravel/installer/serenseprj/resources/views/package.blade.php)
You can use Eloquent instead of doing this
$service = DB::select('select service_type from services');
Using Eloquent,
$services = ModelName::pluck('name', 'id')->get();
You can pass the variable to the targeted view like below:
return view('view_name', ['variable_name' => $variable]);
And, you can access the variable name like below
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($variable_name as $var)
{
<option value="{{$var->id}}">{{$var->name}}</option>
}
#endforeach
</select>
You can use this code as the references. Hope you have service model.
$services= Service::pluck('name', 'id');
return view('services', compact('services'));
then in view file you can use laravel collevtives like.
<div class="form-group col-sm-6">
{!! Form::label('service_id', 'service Id:') !!}
{!! Form::select('service_id', $services, null, ['class' => 'form-control'])!!}
</div>
you can use this too. in you blade file
<select name="service " id="service " class="form-control">
<option value=""> -- Select One --</option>
#foreach ($services as $service )
<option value="{{ $service->id }}">{{ $service->name }}</option>
#endforeach
In my blade template, I use a foreach to create one form per iteration. Currently, each form has a submit button, but I want to use one submit button to submit all the form, because now I cannot submit all forms at the same time. How can I fix this?
#if(count($task_criterias) > 0)
#foreach($task_criterias as $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
<div class="form-group" hidden >
{{ Form::label('criteria_id', 'criteria_id') }}
{{ Form::text('criteria_id', $task_criteria->id, ['class'=>'form-control']) }}
</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark', '',['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
{!! Form::close() !!}
</td>
#endforeach
<input type="button" class="btn btn-info " value="Submit" onclick="submitForms()" />
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'/criteria_marks/post',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
#endif
There can be only one post submit per request. One of the easiest solution is to perfom multiple requests using ajax:
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'* route to CriteriaMarksController#store *',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
Or you can make one big form:
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
#foreach($task_criterias as $key => $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark['.$task_criteria->id.']', '',
['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
</td>
#endforeach
<input type="submit" class="btn btn-info " value="Submit" />
{!! Form::close() !!}
This will form associative array selfmark in post with id => value pairs
Not sure how to do it with the Form facade.
What you have now is:
<input name="criteria_id" value="{{ $task_criteria->id }}" />
<input name="selfmark" value="{{ $task_criteria->id }}" />
But what you need to have is:
<input name="criteria_id[]" value="{{ $task_criteria->id }}" />
<input name="selfmark[]" />
Note the [] in the input names.
I have a form for creating projects. It contains 2 foreign keys domain and owner (domain_id and owner_id). in fact these are 2 drop-down lists. when i try to submit the form, and check my database, I found out that owner and domain have NULL as value, eventhough i selected values from the drop-down list.
this is projectController :
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$domain_id = Domain::all()->pluck('nameDomain', 'id');
$owner_id = Owner::all()->pluck('nameOwner', 'id');
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
and this is create.blade.php :
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>
I read a lot of posts about this problem, but none of them is working for me.
You can try this
in controller's store function
$domain_id = Domain::all('nameDomain', 'id');
$owner_id = Owner::all('nameOwner', 'id');
In you view
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
and for second select option
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
You was not fetching id of your object. The $id you were fetching will just give you the index.
This should work :)
Change
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
to
#foreach($owners as $owner)
<option value={{ $owner->id }}>{{ $owner->nameOwner}</option>
#endforeach
And repeat with the other foreach loop.
If that doesn't work, in your controller add dd($request); to see what info is being passed to it and share with us, please.
Seems close but a few tweaks are needed. Also check your Project model and see if there is a $fillable array. If there is you'll need all the fields in your validate call in it e.g.
protected $fillable= [
'title'
'code'
'domain_id'
'owner_id'
];
Remove the calls in your store function.
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
Make sure you're adding the right ID.
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>
I want to apply a css class 'pure-control-group' to all div of a generated form.
<form name="form" method="post" class="pure-form pure-form-aligned">
<div>
<label for="form_Title" class="required">Titre</label>
<select id="form_Title" name="form[Title]" class="pure-control-group">
<option value="Modification" >Modification</option>
<option value="Construction" >Construction</option>
<option value="Autre" >Autre</option>
</select>
</div>
<div>
<label for="form_ContactWay" class="required">Moyen de Contact</label>
<select id="form_ContactWay" name="form[ContactWay]" class="pure-control-group">
<option value="Telephone" >Téléphone</option>
<option value="Email" >Email</option>
<option value="Direct" >Direct</option>
<option value="Autre" >Autre</option>
</select>
</div>
<div>
<label for="form_Log" class="required">Journal</label>
<textarea id="form_Log" name="form[Log]" required="required" class="pure-control-group"></textarea>
</div>
<div>
<button type="submit" id="form_Enregistrer" name="form[Enregistrer]">Enregistrer</button>
</div>
<input type="hidden" id="form__token" name="form[_token]" value="c19WunU5AgDgc954I3DRJXLqEhQwpOyDCBZEpF7akJs" />
</form>
I tried :
$this->logForm = $this->createFormBuilder($log, array('allow_extra_fields' => true))
->add('Title', ChoiceType::class, array(
'label' => 'Titre',
'choices' => array(
'Modification' => 'Modification',
'Construction' => 'Construction',
'Autre' => 'Autre'),
'attr'=> array('class'=>'pure-control-group')))
->add('ContactWay', ChoiceType::class, array(
'label' => 'Moyen de Contact',
'choices' => array(
'Téléphone' => 'Telephone',
'Email' => 'Email',
'Direct' => 'Direct',
'Autre' => 'Autre'),
'attr'=> array('class'=>'pure-control-group')))
->add('Log', TextareaType::class, array(
'label'=> 'Journal',
'attr'=> array('class'=>'pure-control-group')))
->add('Enregistrer', SubmitType::class)
->getForm();
The problem is that the class is add to the input. label_attr do the job but for the label.
How can I do for the div?
Notice that I would appreciate not to render each field by hand.
You can modify the template for the form row. Do this in the template that the form is rendered in:
{% form_theme form _self %}
{%- block form_row -%}
<div class="pure-control-group">
{{- form_label(form) -}}
{{- form_errors(form) -}}
{{- form_widget(form) -}}
</div>
{%- endblock form_row -%}
I am trying to list all entries which fall under a where condition. If I do the following, I get all the entries returned
$users = User::lists('userName', 'id');
However, I am looking to return only the users who have a department id of 3. So I am doing
$users = User::lists('userName', 'id')->where('departmentId', 3);
However, this returns an empty result set. In my database, I do have users with this department id.
How can I get the lists statement working?
Just a note, the following returns the result I need
$users = User::select('userName', 'id')->where('departmentId', 3)->get();
However, in my edit form, because I have this
!! Form::select('csManager', $users, Input::old('users'), ['class' => 'csManager']) !!}
The old input is not selected and the data is showing up as an array. I know the way to fix this is to do my select like this
<select class="csManager" name="csManager">
#foreach($users as $user)
<option value="{{ $user->id }}">{{ $user->userName }}</option>
#endforeach
</select>
But then I am not sure how to display the old user within the above select.
Any help appreciated.
Thanks
To mark a old selection try this:
<option value="{{ $user->id }}"
{{ (Input::old('users') == $user->id)? 'selected' : '' }}>
{{ $user->userName }}
</option>
As #TimVanUum say, you can save a little bit of code if you prefer. Both are equals.
<option value="{{ $user->id }}"
{{ (Input::old('users') !== $user->id)?: 'selected'}}>
{{ $user->userName }}
</option>
In your controller:
$users = User::where('departmentId', 3)->lists('userName', 'id');
In blade form:
{!! Form::select('csManager', $users, null, ['class' => 'csManager']) !!}
To get the old data in edit form instead of
{!! Form::open([your attributes]) !!}
Use model binding. something like this:
{!! Form::model($user,[your attributes]) !!}
In this case your edit method should have $user=findOrFail($id);