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.
Related
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');
}
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.
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
I have in my View:
Edit</td>
But when I put 1-50 in my $property->user_id parameter, it leads to a property.
I have Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit'); in my web.php file.
Route File:
Route::get('/properties/', 'PropertyController#index');
Route::get('/properties/{property}', 'PropertyController#show');
Route::get('/agents/{agent}', 'AgentController#index');
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
Route::post('/agents/{agent}', 'AgentController#update')->name('agent.property.update');
This is my Controller code:
public function edit($id)
{
$property = Property::find($id);
return view('agents.edit', compact('property'));
}
I don't understand this behavior in Laravel, it's not what I intend and I just want to make the route work correctly.
Laravel gives both agent_id and property_id to the controller as parameter. You are using agent_id only and assuming it as property id.
public function edit($agent_id, $property_id)
{
$property = Property::find($property_id);
return view('agents.edit', compact('property'));
}
Based on your route file, I think you should put this route:
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
above this one:
Route::get('/agents/{agent}', 'AgentController#index');
I've constructed a select list in a dashboard view, which lists id and name of various components. The data is passed to a controller that makes a view using the passed id to pull up the correct component data for that id. Problem is that the controller is constructed to expect an object from which to get the id, so that when I submit the id from the list, I get a "Trying to get property of non-object" error. (It doesn't matter whether I submit to the route or directly to the controller; I get the same error.) Here's the code:
PagesController (that creates list array for the dashboard):
public function showDashboard()
{
$components = Component::lists('name','id');
return View::make('dashboard', array(
'components'=>$components, ...
));
}
Snippet of source code for the select list:
<form method="GET" action="https://..." accept-charset="UTF-8">
<select id="id" name="id"><option value="2">Component Name</option>...
Components Model:
class Component extends Eloquent {
protected $table = 'components'; ... }
ComponentsController:
public function show($id)
{
$component = $this->component->find($id);
return View::make('components.show', array(
'component'=>$component, ...
));
}
dashboard.blade.php:
{{ Form::open(array(
'action' => 'ComponentsController#show',
'method'=>'get'
)) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::close() }}
The same controller code is used for other purposes and works fine, for example, when I pass a specific id from a URL, it accepts that id without an error. So, I know this should be something simple involving the form opening, but I can't figure it out. How can I fix this? Thanks!
It will not work because using get method the url will be like this.
http://laravel/puvblic/show?id=2
and laravel will not accept it , rather it accepts a parameter for function in this way
http:/laravel/puvblic/show/2
A better way to do this would be to make your form method as 'POST' . It would be much safer and better this way. and modify your function as.
public function show()
Then , You can get the id in your controller as
Input::get('id')
EDIT:
To make it simple , try this one:
Route::get('{show?}', function()
{
$id = Input::get('id') ;
echo $id; //This will give you the id send via GET
die();
});
Simply follow the GET method , your form will send a GET request and it will come to this route here you can perform your desired function.
I finally figured out the problem, and the solution:
The problem is that the form should (optimally) be sent as POST (not GET), and therefore not changed from the default value provided by Blade. BUT then the route has to be registered correctly as POST, and that's what I wasn't doing before. So,
dashboard.blade.php:
{{ Form::model(null, array('route' => array('lookupsociety'))) }}
{{ Form::select('value', $societies) }} ...
Route:
Route::post('lookupsociety', array('as'=>'lookup society', 'uses'=>'SocietiesController#lookupsociety'));
SocietiesController#lookupsociety:
public function lookupsociety()
{
$id = Input::get('value'); ... // proceed to do whatever is needed with $id value passed from the select list
}
It works perfectly! The key was changing the method in the route to Route::post() instead of Route::get().
I knew it had to be simple -- I simply hadn't stumbled on the solution before :)