pass user request in form action route in laravel 8 - laravel

here is my simple form
<form id="test-form" class="white-popup-block mfp-hide">
<div class="popup_box ">
<div class="popup_inner">
<h3>Make an Appointment</h3>
<form action="{{route('doctorwithdatepopup',['date'=>$date,'id'=>$doctorsid])}}" method="POST">
#csrf
<div class="row">
<div class="col-xl-6">
<input id="datepicker" placeholder="Pick date" name="date">
</div>
#php
$Department = DB::table('departments')->orderBy('id','desc')->get();
#endphp
<div class="col-xl-6">
<select class="form-select wide" id="departmentid" name="departmentid">
<option data-display="Select Department">Department</option>
#foreach ($Department as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
#endforeach
</select>
</div>
<div class="col-xl-12">
<select class="form-select wide" id="doctorsid" name="doctorsid" class="">
</select>
</div>
<br>
<br>
<div class="col-xl-12">
<button type="submit" class="boxed-btn3">Search</button>
</div>
</div>
</form>
</div>
</div>
</form>
inaction ['date'=>$date,'id'=>$doctorsid] these two data I want pass based on user selection how can I do that?
for example
$date = $request->date
this one can handle in the controller but in action URL how can I pass user enter values ?

Little tricky. Try to make 2 routes for that, first route to get user form requests. Second, your "doctorwithdatepopup" routes.
web.php
Route::post('url', [YourController::class, 'store'])->name('storeform');
Route::get('url2/{date}/{id}', [YourController::class, 'show'])->name('doctorwithdatepopup');
YourController.php
public function store(Request $request)
{
// Some Validation, Logic, etc
return redirect()->route('doctorwithdatepopup', ['date' => $request->date, 'id' => $doctorId]);
}
public function show($date, $id)
{
return view('your view', compact('date', 'id'));
}

Related

my livewire app is returning 404 not found when I selected a dropdown item

I am designing a laravel app, which I used module and livewire. I have a dropdown item which when I selected an item its returned an error 404 NOT FOUND
Here is my livewire component.
I am thinking may the wire:model="selectedSession" and wire:model="selectedTerm" is having problem
please i need your help
<?php
namespace Modules\Student\Http\Livewire;
use Livewire\Component;
use Modules\Student\Entities\Result;
use Modules\Student\Entities\Term;
use Modules\Student\Entities\Section;
class ResultDependance extends Component
{
public $selectedSession = null;
public $selectedTerm = null;
public $results;
public function mount($id)
{
$this->results = Result::with('student', 'section', 'term', 'subject')->where('student_id', $id)->get();
}
public function render()
{
return view('student::livewire.pages.resultdependance',
['terms' => Term::all()],
['sessions' => Section::all()]
);
}
}
my blade.php codes
<div class="section">
<div class="card">
<div class="card-body">
<div class="card-title">
<h4><strong>Result</strong></h4>
<div class="form">
<form action="" method="post">
{{csrf_field()}}
<div class="form-group">
<select name="session" id="" class="form-control form-select" wire:model="selectedSession">
<option selected>Select Session</option>
#foreach($sessions as $session)
<option value="{{$session->id}}">{{$session->section_title}}</option>
#endforeach
</select>
</div>
<br>
<div class="form-group">
<select name="term" id="" class="form-control form-select" wire:model="selectedTerm">
<option selected>Select Term</option>
#foreach($terms as $term)
<option value="{{$term->id}}">{{$term->term}}</option>
#endforeach
</select>
</div>
<input type="button" value="test" wire::loading.attr='disabled'>
<h1 wire:loading>please wait</h1>
</form>
<br>
</div>
</div>
</div>
</div>
</div>
I am designing a laravel app, which I used module and livewire. I have a dropdown item which when I selected an item its returned an error 404 NOT FOUND
Here is my livewire component.
I am thinking may the wire:model="selectedSession" and wire:model="selectedTerm" is having problem
please i need your help
It's missing the logic to filter the results based on the selected session and term.
you can do it by using livewire hooks or by adding this
public function filterResults()
{
$this->results = Result::with('student', 'section', 'term', 'subject')->where('student_id', $id)->where('section_id', $this->selectedSession)->where('term_id', $this->selectedTerm)->get();
}
and in your selectedSession
<select name="session" id="" class="form-control form-select" wire:model.lazy="selectedSession" wire:change="filterResults">
Now when the user changes the selected session or term, the component will filter the results and update the view accordingly.

Long link when I redirect to index

I have the following form:
<form action="/compras" method="POST">
#csrf
<div class="container">
<div class="row">
<div class="col-sm-6">
<select class="form-control" id="productor" name="productor">
<option value="0">Selecciona Productor</option>
#foreach ($productores as $productor)
<option value="{{$productor->id}}">{{$productor->nombre}}</option>
#endforeach
</select>
</div>
<div class="col-sm 6">
<select class="form-control" id="proveedor" name="proveedor">
<option value="0">Selecciona Proveedor</option>
#foreach ($proveedores as $proveedor)
<option value="{{$proveedor->id}}">{{$proveedor->nombre}}</option>
#endforeach
</select>
</div>
<button class="btn btn-secondary mr-2" name="action" value="search" type="submit">Buscar</button>
<button class="btn btn-secondary" name="action" value="create" type="submit">Crear</button>
</div>
</div>
</form>
I don't actually need to save data to the database in this controller, however I used the store function because of the post route that it includes. The idea is that if you press the Buscar button it will redirect to index filtering the table with the parameters, if you press Crear button it will redirect to a different controller and do something different
public function store(Request $request)
{
switch($request->input('action')){
case 'create':
return Redirect::action('ComprandoController#index', array('productor'=> $request->productor, 'proveedor'=>$request->proveedor));
break;
case 'search':
return Redirect::action('ComprasController#index', $request);
break;
}
}
The code is working as intended but when I redirect to index I have a problem in the link.
The link before filtering is: 127.0.0.1:4545/compras but after the filter it's something like
http://127.0.0.1:4545/compras?POST%20/compras%20HTTP/1.1%0D%0AAccept:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8%0D%0AAccept-Encoding:%20%20%20%20%20%20%20%20%20%20%20gzip,%20deflate%0D%0AAccept-Language:%20%20%20%20%20%20%20%20%20%20%20es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3%0D%0AConnection:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20keep-alive%0D%0AContent-Length:%20%20%20%20%20%20%20%20%20%20%20%2085%0D%0AContent-Type:%20%20%20%20%20%20%20%20%20%20%20%20%20%20application/x-www-form-urlencoded%0D%0ACookie:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20XSRF-TOKEN=eyJpdiI6IlhvQkVGa2libnlydUcyRDNQVElRdlE9PSIsInZhbHVlIjoiZWh0d091RUphUE9BV0t0dUtjY2w0akw4VEx2TjkxaVIySXFKWmpJbkVueVU5cHpHZXgvQ3l0TGc0U28zNFAwNSIsIm1hYyI6IjEzMDk3NTI5MWRjOTI2Nzg3YjA4ZWNiYTVlODZjN2ZjMGJjMWFjNWRkNjRjNjcwNDYwOWI5YjBjODE3NmRkMWUifQ%3D%3D;%20laravel_session=eyJpdiI6IkFCcGR2d01xZmwwbmh4dGprMGNYZHc9PSIsInZhbHVlIjoiVHI1aGUzWjVTdlgyVVE3WmFGV01PSjhTb01ZTWxnVllEL2Q2SmVxQ1BpVW5qY24yZjlLc01pdUVRNjNoRDEyTiIsIm1hYyI6ImFkNDI3NzBjZmUwZmRmZmE5ZDM2NjM1YTBkMWQyZjM2ZTUwOTQ4NjcwNzlhNGJkNzJhYjFlNTI1NjE5OTZjOWIifQ%3D%3D%0D%0AHost:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20127.0.0.1:4545%0D%0AOrigin:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545%0D%0AReferer:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545/compras?POST%20/compras%20HTTP/1.1%0D%0AAccept:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8%0D%0AAccept-Encoding:%20%20%20%20%20%20%20%20%20%20%20gzip,%20deflate%0D%0AAccept-Language:%20%20%20%20%20%20%20%20%20%20%20es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3%0D%0AConnection:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20keep-alive%0D%0AContent-Length:%20%20%20%20%20%20%20%20%20%20%20%2085%0D%0AContent-Type:%20%20%20%20%20%20%20%20%20%20%20%20%20%20application/x-www-form-urlencoded%0D%0ACookie:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20XSRF-TOKEN=eyJpdiI6IkpNVTA3dDk1cDZlSUhYNUorRndkamc9PSIsInZhbHVlIjoiK1JXTVFyN2dFcHJLaC9uK1QzMjhjQ1FTa2ZuQzFCM1FabnpiY3lXbzZPMzdVbGE2c0w2b3dWSlp5YnpMdVc0VyIsIm1hYyI6IjRhOTdjNTJiMTA1ODI2ODRmMDVhOTgyMDg1NTMxMzY1YzhiNTI2MGJhMzk1YzZjMzBhNTg5YmJhNDY0N2YzZDYifQ%3D%3D;%20laravel_session=eyJpdiI6ImU4bUUxenZUR0RCaTBJZis4WTg4TGc9PSIsInZhbHVlIjoiYkxucWdXNi81YmlvNzRUbjlqSEpxdUt3ZnBiazVxYURCaVBXSFYwbXZPU0hxTk1Sa2RFSDJVTkIyZUdHVTNGeSIsIm1hYyI6ImFmYWVjMjM1NmMwYTljMjkzNGQ4MmFlYjIwY2Q3MzJhMzI1OTY4MjUwM2ZkZmRhZmIyNTBjNmJiZjYxZDQ1NWQifQ%3D%3D%0D%0AHost:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20127.0.0.1:4545%0D%0AOrigin:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545%0D%0AReferer:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545/%0D%0AUpgrade-Insecure-Requests:%201%0D%0AUser-Agent:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Mozilla/5.0%20%28Windows%20NT%2010.0;%20Win64;%20x64;%20rv:80.0%29%20Gecko/20100101%20Firefox/80.0%0D%0ACookie:%20XSRF-TOKEN=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5;%20laravel_session=eV6iavmA0PGGAkisR1egs2mLHqbW6w9j58maOxDC%0D%0A%0D%0A_token=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5&productor=0&proveedor=0&action=search%0D%0AUpgrade-Insecure-Requests:%201%0D%0AUser-Agent:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Mozilla/5.0%20%28Windows%20NT%2010.0;%20Win64;%20x64;%20rv:80.0%29%20Gecko/20100101%20Firefox/80.0%0D%0ACookie:%20XSRF-TOKEN=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5;%20laravel_session=eV6iavmA0PGGAkisR1egs2mLHqbW6w9j58maOxDC%0D%0A%0D%0A_token=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5&productor=1&proveedor=3&action=search
How could I get rid of the grotesque big link without ruining the working code?
Can you add your route file ?
I prefer use return redirect()->route('route.name', ['id' => 1]); to do redirect from controller.
https://laravel.com/docs/7.x/redirects#redirecting-named-routes
Also, you can use $request->all() or $request->only(['productor', 'proveedor']).
PS : the link is long, because Laravel serialize all datas from $request.

Laravel search functionality

I've got a searchbar on my homepage which looks like this:
<form action="{{ route('search', $query) }}" method="GET" role="search">
<div class="input-group mb-4 search_bar">
<input type="search" name="search" placeholder="Search..." aria-describedby="button-addon5" class="form-control search_input">
<div class="input-group-append">
<button id="button-addon5" type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
My route looks like this:
Route::get('all-{query}-posts', 'SearchController#index')->where('query', '[A-Za-z0-9-]+')->name('search');
So basically I want the input of the searchbar in my url like this: /all-stuff-posts instead of the usual: ?search=stuff
It seems to automatically add the ?search=stuff to the end even though I didn't add that anywhere so that's the first problem.
The second problem is that I can only retrieve the query in the controller but that gives me an error in web.php because the query is still not set. Is there a different way of doing this that does work?
Change your form action method below like this
<form action="{{url('')}}/all-{{$query}}-posts" ...>
Used Post method.
<form action="{{ route('search', $query) }}" method="POST" role="search">
If you want to develop a search engine in your app, I will suggest the following. (One of the simplest ways).
Learn about POST, GET, PUT first.\
Then, My model like this
public function scopeSearchByKeyword($query, $keyword,$location)
{
if ($keyword!='' and $location!='') {
$query->where(function ($query) use ($keyword,$location) {
$query->where("title", "LIKE","%$keyword%")
->where("location_id", "$location")
->where("status", "1");
});
}
else
{
$query->where(function ($query) use ($keyword) {
$query->where("title", "LIKE","%$keyword%")
->where("status", "1");
});
}
return $query;
}
Here I am searching by title and location. It can output even if only a keyword is entered.
Then my controller like this:
public function search_kasblar(Request $request)
{
$inputs = $request->all();
$keyword = $inputs['search_keyword'];
$location = $inputs['location'];
$jobs= JobBoards::SearchByKeyword($keyword,$location)->get();
$total_res=count($jobs);
return view('jobs.search',compact('jobs','total_res','keyword'));
}
Here we can search for incoming data in the input.
So my view blade like this:
<div class="finderform">
{!! Form::open(array('url' => 'listings/search','class'=>'','id'=>'search','role'=>'form')) !!}
<div class="col-md-5 col-sm-5 no-padding"> <i class="fa fa-search local-search-ic"></i>
<input type="text" class="form-control" name="search_keyword" id="input-search-term" title="Search for..." placeholder="Search anything here" value="" autocomplete="off">
</div>
<div class="form-group col-md-5 col-sm-5 no-padding"> <i class="fa fa-map-marker local-search-ic ic-map-location"></i>
<div class="">
<select id="location" name="location" class="form-control">
<option value="">Select Location</option>
#foreach(\App\Location::orderBy('location_name')->get() as $location)
<option value="{{$location->id}}">{{$location->location_name}}</option>
#endforeach
</select>
</div>
</div>
<button type="submit" class="btn tp-btn-default tp-btn-lg">Search</button>
{!! Form::close() !!}
You can use like this.
And use POST in your router...

I want to make validation for same user assigned using laravel

I am a beginner and I am trying to make validation for the user assigned when I assign the same user again when I submit so the error message should be shown, this user is already assigned please help me how to do this? thanks.
Controller
public function adduseraction(REQUEST $request) {
// Users_permissions::where('user_id',$request->userid)->get()-
// >pluck('user_Access_id');
$useradd=$request->get('userid');
$checkid=$request->get('multiusersid');
$array = array();
foreach($checkid as $id){
$array[] = array('user_id'=>$useradd,'user_Access_id'=>$id);
}
$user=Users_permissions::insert($array);
return redirect('admin')->with('success', 'Users has been assigned');
}
html view
<form action="{{route('adduseraction')}}" method="post">
{{ csrf_field() }}
<div class="col-sm-4">
<label>Select User</label>
<select name="userid" class="form-control" id="typeofworkday">
<option>Select User</option>
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name}}</option>
#endforeach
</select>
</div>
<br> <br> <br>
<div class="card-body">
<label>Select Muliple User or One</label>
<!-- Minimal style -->
<div class="row">
#foreach($users as $user)
<div class="col-sm-2">
<div class="form-check">
<input type="checkbox" id="check" name="multiusersid[]" value="
{{$user->id}}" class="form-check-input" >
<h5 style="position:relative;left:10px;">{{$user->name}}</h5>
</div>
<!-- checkbox -->
</div>
#endforeach
</div>
<!-- /.card-body -->
</div>
<br><br><br><br>
<div class="card-footer">
<button type="submit" name="btnsubmit" class="btn btn-primary col-
md-2 center">Submit</button>
</div>
</form>
Ok you can do something like below
First check does user already exists in the table and if exists return redirect with the error message or else insert the data
public function adduseraction(Request $request) {
$isUserAvailable = Users_permissions::where('user_id',$request->userid)->get()- >pluck('user_Access_id');
//If user already exists
if($isUserAvailable) {
return redirect('admin')->with('error', 'Users has been assigned');
}else{
$useradd=$request->get('userid');
$checkid=$request->get('multiusersid');
$array = array();
foreach($checkid as $id){
$array[] = array('user_id'=>$useradd,'user_Access_id'=>$id);
}
$user=Users_permissions::insert($array);
return redirect('admin')->with('success', 'Users has been assigned');
}
}

middleware conflicts with controller __construct middleware (request validation error not working)

I am using middleware for user roles.
But when I am using middleware in Controller __construct method, request validation does not work properly. It doesnt show any errors, session error bag returns null. I am not able to see any errors when form submit. But when I have disabled middleware in construct I can see request validation errors.
web.php middleware + controller _construct middleware = request validation doesnt works.
web.php middleware + without _construct middleware = works fine.
without web.php middleware + _construct middleware = works fine.
I showed the details in my codes.
I tried every method for a week but I couldn't solve it.
I look forward to your help sincerely.
web.php
Route::group(['middleware' => ['client.role:paying']], function () {
Route::get('/pay_section', 'HomepageController#showPaySection');
Route::get('/pay_success', 'HomepageController#showPaySuccess');
Route::get('/pay_error', 'HomepageController#showPayError');
Route::post('/pay_section', 'HomepageController#doPaySection');
});
HomepageController (like this my form request validation doesnt works because of middleware)
public function __construct()
{
$this->middleware(function ($request, $next) {
$client = auth()->guard('client');
if ($client->check()){
$request->session()->put('client_id', $client->user()->id);
}else{
$request->session()->put('client_id', -1);
}
$this->_cid = $request->session()->get('client_id'); // client
View::share(['cid' => $this->_cid]);
return $next($request);
});
}
HomepageController (like this my codes works perfect. I am able to see request validation errors there is no problem.)
public function __construct()
{
$this->_cid = 2; // client
View::share(['cid' => $this->_cid]);
}
Middleware ClientRole.php
public function handle($request, Closure $next, ...$roles)
{
$currentRole = array();
$client = auth()->guard('client');
if ($client->check()){
$currentRole[] = 'client';
}else{
$currentRole[] = 'guest';
}
if (session()->has('shop_cart')) {
$currentRole[] = 'shopping';
}
if (session()->has('order')) {
$currentRole[] = 'paying';
}
$currentRole[] = 'paying';
foreach($roles as $role) {
if(in_array($role, $currentRole))
return $next($request);
}
return redirect('/');
}
HomepageController form action
public function doPaySection(CreditcardRequest $request)
{
$validated = $request->validated();
// it doesnt show any errors when form empty. But it should be.
// without middleware it shows error on my view when form empty.
}
View
<div class="messages">
#if ($errors->any())
<div class="row mt-3">
<div class="col-md-12">
<div class="alert alert-warning alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="alert-heading font-size-h4 font-w400">Error!</h3>
#foreach ($errors->all() as $error)
<p class="mb-0">{{ $error }}</p>
#endforeach
</div>
</div>
</div>
#endif
</div>
<form action="{{ action('HomepageController#doPaySection') }}" method="post"
class="needs-validation" novalidate>
#csrf
<div class="row">
<div class="col-md-6 mb-3">
<label for="ccname">Name on card</label>
<input type="text" class="form-control" name="cc_name" id="ccname" placeholder="" value="" required>
<small class="text-muted">Full name as displayed on card</small>
<div class="invalid-feedback">
Name on card is required
</div>
</div>
<div class="col-md-6 mb-3">
<label for="ccnumber">Credit card number</label>
<input type="text" class="form-control" name="cc_number" id="ccnumber" placeholder="" value="" >
<div class="invalid-feedback">
Credit card number is required
</div>
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<label for="ccexp">Expiration</label>
<input type="text" class="form-control" name="cc_exp" id="ccexp" placeholder="" value="1209" required>
<div class="invalid-feedback">
Expiration date required
</div>
</div>
<div class="col-md-3 mb-3">
<label for="cccvv">CVV</label>
<input type="text" class="form-control" name="cc_cvv" id="cccvv" placeholder="" value="333" required>
<div class="invalid-feedback">
Security code required
</div>
</div>
</div>
<hr class="mb-4">
<hr class="mb-4">
<button class="btn btn-primary btn-lg btn-block" type="submit">
<i class="fa fa-check"></i> Submit
</button>
</form>
You may set SESSION_DRIVER=file in you .env file
Then run php artisan config:clear
Seems related

Resources