laravel passing parameters from view to route - laravel

Using a view with user input. I then want to pass to a route. This what I found so far:
href="{{URL::to('customers/single'$params')}}"
I want to pass the user input as the above $params to my route. This is sample of my route:
Route::get('customer/{id}', function($id) {
$customer = Customer::find($id);
return View::make('customers/single')
->with('customer', $customer);
As soon as I can pass the parameter I can do what I want with the route, which I know how.

Basically you can pass parameter to routes by doing:
Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
In your anchor tag, instead of doing href={{URL...}}, do something like:
{{ URL::to('user/$param') }}
For more information on routing, visit link

This is what I have and works:
<a <button type="button" class="buttonSmall" id="customerView" href="{{URL::to('customer',array('id'=>'abf'))}}" >View</button></a>
But I need the array value 'abf' to be the value of a textbox.

This worked for me in my view anchor tag
href="{{ URL::to('user/'.$param) }}"
instead of what was specified above
href="{{ URL::to('user/$param') }}"

You can user it in View as I used:
<a class="stocks_list" href="/profile/{{ Auth::user()->username }}">Profile</a>
Hope it helps you.

Related

Laravel dynamic routing for search

I would like to generate dynamic route when user clicks on Search button.
I know it can be done with following GET method
https://laravel.dev/search?q=parameter
https://laravel.dev/search?state=XYZ&category=Automobile
But instead I would like to do following
https://laravel.dev/search/state/XYZ/category/Automobile
So if I add an extra parameter in search form it will just add onto the URL.
The parameters may be optional so can not add a fix route in routes. User may supply state or search through all states.
https://laravel.dev/search/category/Automobile
Following is my search form code
<div class="jumbotron">
<!--Search Bar-->
{{ html()->form('GET',route('frontend.search'))->class('form-inline justify-content-center')->open() }}
{{ html()->select('category',$categories)->class('form-control mr-sm-2') }}
{{ html()->select('state',$states)->class('form-control mr-sm-2') }}
<!--More filter to add later-->
<button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
{{ html()->form()->close() }}
</div>
How can I achieve that?
Thank you
You can handle the logic with a catch-all route with regular expressions
https://laravel.com/docs/7.x/routing#parameters-regular-expression-constraints
//routes.php
Route::get('search/{search?}', 'SearchController#search')
->where('search', '(.*)');
//controller
class SearchController extends BaseController {
public function search($search = null)
{
if ($search != null){
dd($search);
}
}
}
Try to use Regex:
Route::get('search/{searchParams}', 'SearchController#search')
->where('searchParams', '[a-zA-Z0-9\/]+');
You can put anything on searchParams but you need to parse it.

How to append query string to pagination links using Laravel

Hello and thanks for your help. I've done some research and tried a few options but can't seem get this to work properly. I'm passing a URL with a query string into a function that loads the page via URL passed. However, I'm trying to find a way to paginate the results as well. Is there a way I can pass the query string url to Laravel's pagination links? Thanks.
My URL with query string
<a id="searchData"class="btn btn-primary ml-1 mr-5 text-light" title="Search" type="submit"
onclick="ajaxLoad('{{url('dma/data')}}?startDate='+$('#startDate').val()+'&endDate='+$('#endDate').val()
+ '&dmaNameFilter=' + encodeURI(dma_name) + '&memberNameFilter=' + encodeURI(member_name))">Search Results
</a>
I tried this for the links():
{{ $data->appends(request()->query())->links() }}
I have this in my Controller:
$data = Report::where('CallDate', '>=', $start_date)->where('CallDate', '<=', $end_date)->paginate(13)->appends(request()->query());
You can also add this:
$this->app->resolving(\Illuminate\Pagination\LengthAwarePaginator::class, function ($paginator) {
return $paginator->appends(Arr::except(request()->query(), $paginator->getPageName()));
});
To your AppServiceProvider
Try This
$data->appends(request()->input())->links()
You can pass any data to pagination by calling
{{ $paginator->links('view.name', ['foo' => 'bar']) }}
on your situation I think you want to pass query string to paginator; you may try
{{ $paginator->links('view.name', request()->getQueryString() ) }}
If you need to append querystrings for your ajax controller you'd better check https://github.com/spatie/laravel-query-builder
Since Laravel 8, You can simply use
$paginator->withQueryString();

can I use route() to make a DELETE request in laravel

I'm using laravel and trying to delete something. Is it possible to specify the DELETE method on laravel's route()??
e.g
route('dashboard-delete-user', ['id' => $use->id, 'method'=> 'delete'])
or something like that??
EDIT:
What I meant was could I specify that in a link or a button in my blade template. Similar to this:
href="{{ route('dashboard-delete-user') }}
Yes, you can do this:
Route::delete($uri, $callback);
https://laravel.com/docs/master/routing#basic-routing
Update
If for some reason you want to use route only (without a controller), you can use closure, something like:
Route::get('delete-user/{id}', function ($id) {
App\User::destroy($id);
return 'User '.$id.' deleted';
});
No or at least I haven't figure out how to.
The only way for this to work out of the box would be to build a form to handle it. At the very minimum, you would need...
<form action="{{ route('dashboard-delete-user') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" value="submit">Submit</button>
</form>
Or you can just create the get route which you are trying to link to and have it handle the logic. It doesn't need to be a route which only respondes to delete requests to delete a resource.
Yes you can, using a URL helper. https://laravel.com/docs/5.2/helpers#urls
There are several options to choose from.

Laravel: delete photo by photo id

I'm trying to delete a photo by it's id, but the routes do not work and I receive a MethodNotAllowedHttpException. What I do:
First I create a form (in my blade template):
{{ Form::open(array("action" => array("cms/albums/destroyphoto", $photo['id']), "method" => "DELETE")) }}
<button type="submit">Delete</button>
{{ Form::close() }}
Then i create my route:
Route::post('cms/albums/destroyphoto/{id}', 'AlbumsController#destroyphoto');
And create my function in the Albumscontroller:
public function destroyphoto($id)
{
dd('Welcome photo');
}
Any suggestions where the routing goes wrong?
Thanks in advance.
Ps. I did composer dump-autoload
When you open your form using "action" you should pass the controller class and action name. You also don't need to specify the method since you're using Route::post
Like this:
{{ Form::open(array("action" => array("AlbumsController#destroyphoto", $photo['id']))) }}
More information

Dropdownlist in a edit.blade.php form

please how can I put dropdown list in an edit form with the old selected value on default?
here is my example:
<div class="form-group">
{{ Form::label('Container', 'Container:') }}
{{ Form::select('Select_cont', $containers) }}
</div>
I don´t know where to put the code of the old new value selected in my view and what should it be.
Please don´t forget that when directing to edit.blade.php I wrote thisin the function of my controller
return View::make('audio.edit',array($container))
->with('containers', $containers)
thanks a lot for your help :)
Selected value is the 3rd param of Form::select, so:
{{ Form::select('Select_cont', $containers, $selectedPreviouslyKey) }}
How to get that key depends on your code and what's in the dropdown
Pass it to the view like this
return View::make('audio.edit',array('containers' => $containers, 'selectedPreviouslyKey' => $selectedKey));
or use compact() / with etc
The View::make call isn't entirely right. It should be:
return View::make('audio.edit')->with('containers', $containers);
or
return View::make('audio.edit')->withContainers($containers);
or
return View::make('audio.edit', array('containers' => $containers));
or
return View::make('audio.edit', compact('containers'));
Also: make sure $containers exists.
The documentation on the select tag. http://laravel.com/docs/html#drop-down-lists

Resources