Laravel - pass array variable to route [duplicate] - php

I need to pass array to route and controller from view.
I get the error:
Missing required parameters for [Route: actBook] [URI: bookfromindex/actBook/{id}/{array}].
I have my route defined as:
Route::get('/bookfromindex/actBook/{id}/{array}', 'BookController#actBook')->name('actBook');
My controller function is defined as:
public function actBook(Request $request, $id, $array){
And I call this route in my view using:
დაჯავშნა
How do I prevent this error?

Just change -
დაჯავშნა
to -
დაჯავშნა

First, you need to serialize your array then you can pass into the parameter
Example :
{{ $serializeArray = serialize($array) }}
<a href="{{ route('actBook', $room->id, $serializeArray) }}" class="btn btn-default">
Controller :
public function actBook(Request $request, $id, $array){
Route :
Route::get('/bookfromindex/actBook/{id}/{array}', 'BookController#actBook')->name('actBook');
Hope this will help you.

Just use serialize($array);
Then pass this array to the route.

Related

How to delete data from database in Laravel 8.*

I'm trying to delete city from my db but i really got no clue what to do next.
Since I didnt find anything about deleteing data from db for Laravel 8.* I think this might be usefull for other stackoverflowers too.
My error says clearly that I'm missing some arguments,
Too few arguments to function App\Http\Controllers\CityController::delete(), 0 passed in C:\xampp\htdocs\Laravel1\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
While my controller says that he expected type 'object' but only found array
Exoected type 'object'. Found 'array'. intelephense(1006)
#var array $todelete
Here is Route from web.php
Route::delete('cities', [App\Http\Controllers\CityController::class, 'delete'])->name('cities.delete');
And this is form I'm using to delete
<form class="float-right m-0" method="post" action="{{ route('cities.delete') }}">
#method('delete')
#csrf
<div class="form-row">
<input type="hidden" name="cityId" value="{{ $city->id }}">
<button> <i class="fa fa-trash-alt"></i></button>
</div>
</form>
Edit
After solving previous problems, I'm stuck on another error:
Call to a member function delete() on array
Here is how my "CityController"s function looks like after updates
public function delete(Request $request)
{
$cityId = $request->get('cityId', false);
// TODO: Check for validation
$todelte = DB::select('select '.$cityId.' from cities');
$todelte -> delete();
return redirect('frontend/city/index');
}
Correct query to delete stuff:
DB::table('cities')->where('id', $cityId)-> delete();
The issue with not getting the $id parameter passed has to do with your route not accepting any parameters.
Your call is DELETE /cities, which does have any parameters.
If you changed that to DELETE /cities/5, than you could get a parameter passed down the line.
In order to get the parameter to your controller you should change your route as follows:
Route::delete('cities/{id}', [App\Http\Controllers\CityController::class, 'delete'])->name('cities.delete');
This way the {id} part will be passed along to the function specified in the callback. To make your form work with that new route you should change the action-part of you form to direct to the new URL.
I suggest using the 2nd parameter as well so you get:
route('cities.delete', ['id' => $city->id])
Then you can remove the hidden input.
If you do want to keep the existing form you need to use the Request as a parameter in your delete-function. That would look like this:
public function delete(\Illuminate\Http\Request $request)
{
$cityId = $request->get('cityId', false);
// TODO: Check for validation
$todelte = DB::select('select '.$cityId.' from cities');
$todelte -> delete();
return redirect('frontend/city/index');
}

How to build a URL from a set of LARAVEL subcategories?

Please explain how to correctly build a URL from a variety of subcategories?
I have a page - all categories (general menu).
// Route
Route::get('/category', 'CategoryController#index')->name('category.index');
// Controller
public function index()
from this page, I have to go to ru/category/clothes/womens, for this I have:
// Route
Route::get('/category/{alias?}', 'CategoryController#one')->name('category.one');
// Controller
public function one($alias, Request $request)
// Link
<a title="{{ $v['name'] }}" href="{{ route('category.one', $v['url']) }}">
but I can't pass the parameter (clothes/womens). 404 was not found , maybe because $v ['url'] is not a laravel object.
What should I do then?
Route::get('/category/{alias?}', 'CategoryController#one')->where('alias', '(.*)')->name('category.one');

How to pass two different ID's in a route laravel?

I have a simple app, I need to pass two different ID's id and code_id in a route, here is my solution I have tried so far
view
{{ __('Code') }}
Here is route config
Route::get('settings/code/{id}/{code_id}', ['as' => 'settings.code', 'uses' => 'SettingController#code']);
Here is my function in a controller
public function code($code_id, $id)
{
$settings = Setting::find($code_id, $id);
dd($settings);
return view('pages.settings.code', compact('settings'));
}
Here is the error I get
Missing required parameters for [Route: settings.code] [URI: settings/code/{id}/{code_id}]. (0)
What is wrong with my code?
First you should pass an array as 2nd argument to route() method:
{{ route('settings.code', ['id' => $settings->id, 'code_id' => $settings->code_id]) }}
And note that:
Route parameters are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter.
So you should swap the arguments of your controller's method:
public function code($id, $code_id)
{
//...
}
I got my solution like this:
Route::match(["get", "post"], "/patient-member-orders/**{patient_id}/{member_id}**", [PathologyController::class, "patientMemberOrders"])->name("pathology-patient-member-orders");
After that we need to pass that name in our route method's second argument as an array in the Key Value pair.
**route("pathology-patient-member-orders", ["patient_id" => $member->patient_id, "member_id" => $member->id])**
Please correct me if I am wrong.
Chinmay Mishra

Pass multiple arguments to route/controller in laravel

I'm trying to pass two variables/arguments from my view through a link
<a href="{{ route('shop.order.test', $id,$form['grouping']) }}"
and call the route
Route::get('ordering/test', 'Shop\OrderingController#testing')
->name('shop.order.test');
And call this function with those two arguments
public function testing($id,$grouping){
}
It doesn't seem to be working though. Is my error in my route or my link call?
If you want to have parameters to be passed into controller's method, you need to define route parameters like this
Route::get('ordering/test/{id}/{grouping}', 'Shop\OrderingController#testing');
then you can have it in controller method:
public function testing($id, $grouping)
To generate route for above definition, the second parameter is the array of params to pass. So it will become
{{ route('shop.order.test', ['id' => $id, 'grouping' => $form['grouping']) }}
To pass parameters in a route use an array with the paramter names as keys:
{{ route('shop.order.test', ['id' => $id, 'grouping' => $form['grouping']]) }}
Laravel Doc

routes laravel with a variable

Hello this is my controller:
public function getValues(Request $request){
$typ=$request->get('typ');
$stellentyp=$request->get('stellentyp');
$bereich=$request->get('bereich');
$view = 'user.'.$stellentyp;
return view($view,['typ' => $typ, 'stellentyp', $stellentyp, 'bereich', $bereich]);
}
I want that the user can select a "stellentyp" and then the view with that "stellentyp" should be shown.
But I have a problem with my routes, they do not know the variable "stellentyp".
How can I connect my controller with my routes?
I tried this
Route::post('user/{$stellentyp}', 'StartController#getValues')->name('user.{$stellentyp}');
but it does not work :(. The error is:
Missing required parameters for [Route: user] [URI:
user/{$stellentyp}]. (View:
C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)
Route::post('user/{stellentyp}', 'StartController#getValues')->name('user.values');
In order to add a variable inside the route path you don't have to use the dollar signal to declare it. Check above code. Also the name of the route does not have to be dynamically assigned.
in laravel routes you don't need $ sign for variable
, remove $ sign and test your route again, it should be work...
Route::post('user/{stellentyp}', 'StartController#getValues')->name('user.stellentyp');
and add $stellentype to your controller method input like that:
public function getValues(Request $request,$stellentype)
Route parameter totaly different from POST parameters.
when you send a post parameter, it should be in your form as a input or selectbox etc.. then you can catch them in controller with get() method from $request (instance of Request class)
public function getValues(Request $request){
$request->get("post")
}
But route parameter not send in a form, we send them with below way
in blade file
<form action="{{route("user.values",$user_id)}}">
in route file
Route::post('user/{id}', 'StartController#getValues')->name('user.{$stellentyp}');
and catch them in controller as a function parameter
public function getValues(Request $request,$user_id){
dd($user_id);
}
i get it :D
<form action="{{ action('StartController#getValues') }}" method="post" id="postData">
{{ csrf_field() }}
<select name="stellentyp">
<option value="praktika">Praktika</option>
<option value="fwd">Freiwilligendienste</option>
<option value="jobs">Ferien- und/oder Nebenjobs</option>
</select>
<button type="submit">Jetzt finden</button>
this is my blade.php
and my controller is the same and my routes looks like this
Route::resource('user/start', 'StartController');
Route::post('user/angebote', 'StartController#getValues');
Route : -
Route::post('user/{stellentyp}', 'StartController#getValues');
Controller : -
public function getValues(Request $request,$stellentyp=''){
$typ=$request->get('typ');
$stellentyp=$request->get('stellentyp');
$bereich=$request->get('bereich');
$view = 'user.'.$stellentyp;
return view($view,['typ' => $typ, 'stellentyp', $stellentyp, 'bereich', $bereich]);
}
Use this code to get the expected answer.

Categories