Long link when I redirect to index - laravel

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.

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.

pass user request in form action route in laravel 8

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'));
}

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...

How to update specific row using modal Laravel 5.8

I am trying to update a specific row using modal. However I don't have any idea how to pass the value of the row id to the route parameter.
Here's the update form.
<form action="{{route('subcategory.update', 'idhere')}}" method="POST">
#method('PATCH')
#csrf
<div class="modal-body">
<label for="editname">New sub-category name:</label>
<input type="text" name="editname" id="editname" class="form-control">
<input type="text" name="editid" id="editid" value="" hidden>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="submit">Save changes</button>
</div>
</form>
Edit button
<button class="btn btn-secondary btn-sm" data-myid="{{$item->id}}"
data-mytitle ="{{$item->name}}"
data-target="#editsub" data-toggle="modal">Edit</button>
And here's the controller
public function update(Request $request, Subcategory $subcategory)
{
// return $request;
Subcategory::where('id',$request->editid)->update([
'name' => $request->editname
]);
return back();
}
When you pass an id here and if your model name is subcategory.php you will get the db record in your controller.
<form action="{{route('subcategory.update', $subcategory )}}" method="POST">
Or you can do following too
<form action="{{route('subcategory.update', $subcategory->id )}}" method="POST">
Your route should have {subcategory} in it for eager loading. After that you can do following in your code.
public function update(Request $request, Subcategory $subcategory)
{
//variable $subcategory has the db record
$subcategory->update(['name' => $request->only('editname')]);
return back();
}
If I understand it correctly, you can store the item id in
<input type="text" name="editid" id="editid" value="{$item->id}" hidden>
And since you are using POST request, you can easily get the item id from the request.
Like the one in your post:
$request->input('editid');
Please try first to pass id in input box hidden field

not getting checkbox element value from form

I have a small form in Laravel 5.4 which has a checkbox and a text box. The issue is that when I post the form, the checkbox value is not coming through the request. I have custom styling on the checkbox but surely it can't be that?
I've been looking at this for a while, and everything looks normal. My code is below:
<form method="post" action="{{ route('admin.settings.save') }}">
{{ csrf_field() }}
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label><b>Site Name</b></label>
<p>This is the name of your LaravelFileManager instance.</p>
<input name="siteName" id="siteName" class="form-control" value="{{ \App\Helpers\ConfigHelper::getValue('site_name') }}" />
</div>
<div class="form-group">
<label><b>Footer Message</b></label>
<p>You can customise the footer message for the application.</p>
<div class="checkbox">
<label>
<input type="checkbox" name="showFooter" id="showFooter" checked="{{ \App\Helpers\ConfigHelper::getValue('show_footer_message') }}"> Show footer message
</label>
</div>
</div>
<button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Save Changes</button>
</div>
</div>
</form>
My controller code is as such:
public function saveSettings(Request $request) {
$siteName = $request->input('siteName');
$showFooter = $request->input('showFooter');
ConfigHelper::setValue('site_name', $siteName);
ConfigHelper::setValue('show_footer_message', $showFooter);
return redirect()->route('admin.settings')->with('result', 'Settings saved.');
}
My route:
Route::post('settings/save', ['uses' => 'Admin\SettingsController#saveSettings'])->name('admin.settings.save');
I've also done a vardump on the $request variable and even that is missing the check box value:
array(2) {
["_token"]=> string(40) "sgyO7Kkz1ljsYEZ1G5nkj4uVbmFZqiTMbpK9P6Bi"
["siteName"]=> string(16) "File Manager 1.0"
}
It's missing the 'showFooter' variable.
Not quite sure where to go with this one. Any help appreciated.
So I got this working in the end. Using help from the comments:
public function saveSettings(Request $request) {
$siteName = $request->input('siteName');
$showFooter = $request->has('showFooter');
ConfigHelper::setValue('site_name', $siteName);
ConfigHelper::setValue('show_footer_message', $showFooter);
return redirect()->route('admin.settings')->with('result', 'Settings saved.');
}
For some reason, using $request->input('showFooter') wasn't working properly. $request->get('showFooter') brings a result when true, so adding the ternary makes it work every time.

Resources