In my laravel project, am showing select box which contains various clinics, each clinic have api_key and id . I want to pass both id and api_key to Laravel Controller when a form is submitted.
But now am getting only api_key .ID is not getting.
Following is my code in view page
<form method="post" action="{{ route('viewcode') }}" name="cliniclist" id="cliniclist" enctype="multipart/form-data" novalidate>
{{ csrf_field() }}
<select class="selectpicker" data-style="btn-info btn-fill btn-block" id="clinic_id" name="clinic_id" key="clinic_key">
#foreach($clinics as$clinic)
<option value="{!! $clinic->api_key !!}" id="{!! $clinic->api_key !!}" clinic_key="{!! $clinic->clinicID !!}">{!! $clinic->clinicName !!}</option>
#endforeach
</select>
<div class="col-xs-12 col-sm-12 col-md-12 form-action">
<button type="submit" class="btn btn-fill btn-info">Submit</button>
Cancel
</div>
</form>
Here in my code am printing clinic id in clinic_key attribute
Following is my code in controller
public function store(Request $request)
{
echo $request->clinic_key;
die();
$clinic_api_key = $request->clinic_id;
return view('locations.viewcode')->with(['clinic_api' => $clinic_api_key]);
}
What is the problem here
You can do it simply like
<option value="{!! $clinic->api_key.'-'.$clinic->id !!}">{!! $clinic->clinicName !!}</option>
And inside controller do it this way
$split = explode("-",$request->clinic_id);
$clinic_api_key = $split[0];
$clinic_id = $split[1];
Updated Answer. With little jquery you can do in this way.
<form method="post" action="{{ route('viewcode') }}" name="cliniclist" id="cliniclist" enctype="multipart/form-data" novalidate>
{{ csrf_field() }}
<select class="selectpicker" data-style="btn-info btn-fill btn-block" id="clinic_id" name="clinic_id" key="clinic_key">
#foreach($clinics as$clinic)
<option value="{!! $clinic->api_key !!}" data-clinic_id="{!! $clinic->clinicID !!}">{!! $clinic->clinicName !!}</option>
#endforeach
</select>
<input type="hidden" name="api_key" id="api_key">
<input type="hidden" name="id" id="id">
<div class="col-xs-12 col-sm-12 col-md-12 form-action">
<button type="submit" class="btn btn-fill btn-info">Submit</button>
Cancel
</div>
</form>
Jquery Code, as you can see i have placed two hidden inputs inside your form, so you will access these two inputs inside your controller except select name.
$(document).on('change','#clinic_id',function(){
var clinic_api_key = $('option:selected',this).val();
var clinic_id = $('option:selected',this).data('clinic_id');
$("#api_key").val(clinic_api_key);
$("#id").val(clinic_id);
});
Now in controller
$request->api_key;
$request->id;
Option tag can have only one value. Since you are trying to get 2 values there, you might want to join them with some special characters like hyphen "-" or maybe colon ":". In that way you can separate them by that special character in our controller later.
<select class="selectpicker" data-style="btn-info btn-fill btn-block" id="clinic_id" name="clinic_id" key="clinic_key">
#foreach($clinics as$clinic)
<option value="{!! $clinic->api_key.'-'.$clinic->id !!}" id="{!! $clinic->api_key !!}" clinic_key="{!! $clinic->clinicID !!}">{!! $clinic->clinicName !!}</option>
#endforeach
</select>
And then in your controller just get the value of clinic_id field and separate them.
public function store(Request $request)
{
//make sure clinic_id and id does not contain '-' in their values
$clinic_data = explode('-',$request->clinic_id);
$clinic_id = $clinic_data[1];
$clinic_api_key = $clinic_data[0];
return view('locations.viewcode',compact('clinic_id','clinic_api_key'));
}
for the above code to work. please make sure your api_key and id data from the database does not contain "-" or will break. In that case, use some other separator.
Related
So I know how to retrieve data to views(using classes in controller)
but how can I return some data to layout which doesnt(or rather i dont know where they are) have its classes where i could get code to do so.
here is my form in which i would like to put cities name.
I always do it by giving to my frontend controller data from db using
$events = DB::select('select * from events');
return view('frontend/index', ['event' => $events]);
Edit
How to pass $city->id from loop to action?
My app.blade form:
<form method="POST" action="{!! route('cityView', ['id' => $city->id]) !!}" class="form-inline">
<div class="form-group">
<select name="city_id" class="form-control">
<option>xd</option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button></a>
</form>
But how can I do it for a layout?
Would making a layout controller help with it? Or maybe make another class inside existing controller and return view of layout view?
Second solution would be adding this form to every single view since there will be only 3 of them but i prefer solution with app.blade
You can call data directly from layout something like this:
<form method="POST" action="#" class="form-inline">
<div class="form-group">
<select name="room_size" class="form-control">
<option></option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>
Controller:
public function index(){
$bike1 = Bike::get()->first();
$bike2 = Bike::get()->skip(1)->first();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Here I have passed only the first and second row to the blade view.
Blade view:
Select first bike
<form action ="{{ route('compare.get', [$bike1->id, $bike2->id]) }}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
<option value="{{ $bike1->id }}"> {{ $bike1->name }}</option>
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
<option value="{{ $bike2->id }}"> {{ $bike2->name }}</option>
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
The get controller passes the value to another view that displays the data. It works.
How can I pass all the rows to the blade view? I know I can use all functions and loop the variable in the blade to get all the rows. Like this
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
If I do this, then how can I use the $b1 and $b2 as route parameters in the action properties in the form?
Can I manipulate the solution from the controller leaving my blade as it is? Or how do I change the blade view in order to achieve my goal? I am stuck
well if I am getting you correctly your Controller should be like
public function index(){
$bike1 = Bike::all();
$bike2 = Bike::all();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Then you modify your view to
Select first bike
<form action ="{{ route('compare.get'}}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
For submitting you don't have to put them as route parameters,, just let the form submit and get the GET variables in the Controller that you are submitting the form to,,You can use the Request object to do this
$bike1 = Request->input('bike1');
$bike2 = Request->input('bike2');
hope you understand this,, just make sure the Request object is set as a parameter in the controller that you are submitting to
I have a dynamic buttons where it produced from my database
My code for that in blade file
<div class="input-group col-sm-12">
<!--start of the form-->
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<!--input type hidden department code below -->
#foreach($departments as $department)
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<!--buttons -->
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
#endforeach
</form>
<!--end-->
</div>
Whenever I click either one, they'all add a data in my call database based on the values per button. My problem is when I click the cashier button, the values that would add would be the assessments.
My code in CallController
public function store(Request $request)
{
$dept_id = Department::select('id')
->where('dept_name', $request->input('dept_name'))
->first();
$let = Department::select('letter')
->where('dept_name', $request->input('dept_name'))
->first();
$number = Department::select('start')
->where('id', $dept_id->id)
->first();
$call = Call::create([
'dept_id' => $dept_id->id,
'letter' => $let->letter,
'number' => $number->start,
'called' => $request->input('called')
]);
Department::where('id', $dept_id->id)
->increment('start');
return redirect()->route('call.index')->with('success' , 'NEW CALL');
}
I also dd each query and found out that the values would get are the values from assessment or the last value from the foreach loop in my blade file. How could I get the value of cashier when I click the cashier button instead of assessment.
I would show my database so that you understand my question
Department Table: id, dept_name, letter, start(int, it'll increment after producing a call)
Counter: id, counter_num, dept_id
Call Table: id, dept_id, letter, number, counter_id, called
When you click either button, they will submit their parent form. Because both are under the same form, the data from the first button will be submitted. You will have to make a separate form for each button in order to have them submit their own data.
<div class="input-group col-sm-12">
#foreach($departments as $department)
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
</form>
#endforeach
</div>
You're creating multiple submit buttons in the same form.
Add the tag inside the loop so you have a unique form per case.
When there's multiple submit buttons inside the same form, the loop will finish and only the last results will be mapped to the buttons, thus is will submit the last information in your loop.
#foreach($departments as $department)
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<!--buttons -->
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
</form>
#endforeach
Grettings.
I'm trying to call an index view from itself, to look for a person and displaying results to the left.
But when I call the view and send data It always get null, could you please give me a clue. I've even called the store function and request is always null.
It should be a modal window to get the request object? What should I do If I wanted to show the windows as I'm doing it?
My routes
Route::get('registroaccesos/destinationSearchGet/', 'RegistroAccesosController#destinationSearchGet')->name('registroaccesos.destinationSearchGet');
Route::post('registroaccesos/destinationSearchPost/', 'RegistroAccesosController#destinationSearchPost')->name('registroaccesos.destinationSearchPost');
The controller
public function destinationSearchGet(){
$headData = array('pageTitle' => 'Admin Home - View all destinations');
return view('registroaccesos.index', $headData);
}
public function destinationSearchPost(Request $request){
$headData = array('pageTitle' => 'Admin Home - Search results');
$formData = $request->input('strPatron');
dd($formData);
// $data = ParentRegionList::destinationSearch($formData);
return view('registroaccesos.index', $headData);//->with(compact('data'))
}
The view
Part of the code of the view
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Acciones</span>
<a class="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle"></span>
</a>
</h6>
<div class="form-group">
<label for="strSearch" class="col-form-label">Patrón de búsqueda</label>
<select name="strSearch" class="form-control">
<option value="1" selected> Código del usuario</option>
<option value="2" > Nombre del usuario</option>
</select>
</div>
<div class="form-group">
<input placeholder="Patrón de búsqueda"
id="strPatron"
required
name="strPatron"
spellcheck="false"
class="form-control"
value="Valor"
/>
</div>
<button class="btn btn-sm btn-outline-secondary"
onclick="
event.preventDefault();
document.getElementById('search-form').submit();
"
>Buscar</button>
<form id="search-form" action="{{ route('registroaccesos.store','el resultado' ) }}" method="POST" style="display: none;">
<!-- <input type="hidden" name="_method" value="delete">-->
{{ csrf_field() }}
</form>
The result
Well your <input> with name="strPatron" is not in your <form> so it will not get submitted when you submit the form. You need to have all the input elements inside the form or they won't get sent with the POST request.
The way you've got it right now only the CSRF field in your form is getting submitted. You can check this by doing a dd($request->all()) in your controller.
I'm trying to create a situation where a user can load a page, select a timesheet number, and then have the system delete all records associated with that timesheet number. This function loads the form page:
public function deletetimesheetLoad()
{
$timesheets = collect(DB::select("SELECT dbo.TIME.Source AS Source
FROM dbo.TIME
GROUP BY dbo.TIME.Source
ORDER BY dbo.TIME.Source ASC"));
return view('utilities/deletetimesheet',['timesheets' => $timesheets]);
}
This is the actual blade template:
<form method="post"
action="{{url('/time/deletetimesheet/process')}}"
enctype="multipart/form-data"
<div class="form-group">
<label class="col-md-12 control-label" for="TIMESHEETNUMBER">Timesheet Number</label>
<div class="col-md-12">
<select required id="TIMESHEETNUMBER" name="TIMESHEETNUMBER" class="form-control select2_field">
<option value=""></option>
#foreach ($timesheets as $row)
<option value="{{ $row->Source }}">{{ $row->Source }}</option>
#endforeach
</select>
</div>
</div>
<div id="saveActions" class="form-group">
<input type="hidden" name="save_action" value="Submit">
<div class="btn-group">
<button type="submit" class="btn btn-success">
<span class="fa fa-save"></span>
<span data-value="Submit">Submit</span>
</button>
</div>
<span class="fa fa-ban"></span> Cancel
</div>
This is the function that the form action is pointing to:
public function deletetimesheetProcess(TimesheetRequest $request)
{
DB::table('TIME')->where('Source', $request->get('TIMESHEETNUMBER'))->delete();
\Alert::success(trans('yay'))->flash();
return view('details/customershow',[]);
}
Here are the defined routes for the two functions:
Route::get('/time/deletetimesheet', 'Admin\TimeCrudController#deletetimesheetLoad');
Route::post('/time/deletetimesheet/process', 'Admin\TimeCrudController#deletetimesheetProcess');
Currently the blade template loads correctly, and does not throw an error on submit - just reloads the current page. What am I doing wrong?