Missing argument 2 for App\Http\Controllers\UserController::store() - php

Controller
public function store( Request $request,$id)
{
$new = Car::find($id);
$new->status = $request ->input('field');
$new->save();
redirect('home');
}
View
#foreach($users as $user)
#foreach($user->cars as $users)
{!! Form::open( ['route' => 'user.store', 'method'=>'post','id'=> '$users->id']) !!}
<th scope="row">1</th>
<td>{!! Form::label($cars->name)!!}<td>
<td>{!! Form::label($cars->age)!!}<td>
<td>{{Form::select($cars->status, ['R' => 'Requested', 'C' => 'Coming', ['name' => 'field']])}} // name is worong but I dont know the alternative
<td>{!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}</td>
{{ Form::close() }}
Route
Route::Resource('user', 'UserController');
Problem
Trying to save the selected value from status in Car model. but I get an error about the parameters. Can someone show me where my mistake is? I'm new to Laravel.

You can't pass additional data to store action this way.
You can look at this route by using php artisan route:list command. As you can see, it doesn't expect and doesn't pass any data.
So, you need to pass ID in hidden input:
{!! Form::hidden('userId', $user->id) !!}
And get data in controller with $request->userId
Don't forget to remove $id from store() and $users->id from Form::open()
Also, correct syntax (with fixed typos) for Form::open() is:
{!! Form::open(['method' => 'post', 'route' => 'user.store']) !!}

Related

Laravel parse variable from Form to Controller method via Route

I'm trying to update an edit to a post without using the resource. I tried parsing the variable from my Form to my route using {id} but it gets ignored. This is the form I'm trying to post from.
{!! Form:: open(['action'=> ['ManageBooksController#updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('_method', PUT)}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
This is my route
Route::put('manageBooks', 'ManageBooksController#updateBook');
This is my method in my controller
public function updateBook(Request $request, $id)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::find($id);
$books->Book_NAME =$request->input('Book_NAME');
$books->save();
return redirect('/manageBook')->with('success', 'Book Edited');
}
Your route is expecting a PATCH. Try updating your route to:
Route::post('/manageBooks/{id}', 'ManageBooksController#updateBook');
Or include the Laravel's #method('PATCH') within your form.
Also, your controller names don't match :)
Consider changing sequence of the arguments to the function:
public function updateBook($id, Request $request) // Notice the sequence of the arguments
{
......
}
If you want to use Route, you have to specifics like this
{!! Form:: open(['route'=> ['manage_book', $book->id], 'method' => 'POST']) !!}
In your route, you may need to name it properly
Route::post('/manageBooks/{id}', array('as'=>'manage_book','uses'=>'ManageBooksController#updateBook'));
Hope it helps.
Update without using resource :
your route :
Route::get('/manageBooks', 'ManageBooksController#whateverer')->name('manageBooks');
Route::post('/manageBooks/{id}/edit', 'ManageBooksController#updateBook')->name('updateBook');
your blade:
{!! Form:: open(['route'=> ['updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('id', $book->id)}} //hidden field is not required
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
your controller:
public function updateBook(Request $request, $id)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::where('id',$id)->update(['Book_NAME'=>$request->Book_NAME]);
return redirect()->route('manageBooks')->with('success', 'Book Edited');
}
In the end I added another hidden field where i parse the ID through of the post I'm editing. I also changed the find method to take the request variable pointing to the ID.
My Form:
{{Form::hidden('Book_ID', $book->Book_ID)}}
{{Form::hidden('_method', PUT)}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
My function:
public function updateBook(Request $request)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::find($request->Book_ID);
$books->Book_NAME =$request->input('Book_NAME');
$books->save();
return redirect('/manageBook')->with('success', 'Book Edited');
}
Change your method POST to PUT in your from first,
{!! Form:: open(['action'=> ['ManageBooksController#updateBook', $book->id],'method' => 'PUT']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
Then You Have to Pass a parameter in your route, as your method expecting $id
Route::put('manageBooks/{id}/update', 'ManageBooksController#updateBook');

2 button accessing different method in the same controller with the same route using Laravel

I have 2 buttons in my blade. I want to update the same database column with them, when I click button 1, it updates the column to 1, when I click the second button it updates the column to 2. I am using the same route to do this because I have to pass the "id" from the view to the controller.
buttons in the view:
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
Routes
Route::get('/notification_list/notification_application/{notification_id}', 'AdminController#approveNotification')->name('show.approve_notification_application');
Route::get('/notification_list/notification_application/{notification_id}', 'AdminController#sendNotificationToSuperAdmin')->name('show.approve_notification_application');
Controller
public function approveNotification($id){
$notification = Notification::find($id);
$notification->approved = '2';
$notification->save();
return redirect()->route('admin.notification_list');
}
public function sendNotificationToSuperAdmin($id){
$notification = Notification::find($id);
$notification->approved = '1';
$notification->save();
return redirect()->route('admin.notification_list');
}
I don't know how to do this. When I click any button, only the second route seems to work which means irrespective of which button I click, it always updates the table to with the value 1.
That's because you can't set two or more routes with same URL and method type. you can use same URL with other type like Route:get('hi') and Route::post('hi').
Back to your problem you can do something like this:
Buttons
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 0) !!}
{!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 1) !!}
{!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
Controller
public function approveNotification(Request $request, $id){
$notification = Notification::find($id);
$notification->approved = $request->input('type') == 1 ? 1 : 2;
$notification->save();
return redirect()->route('admin.notification_list');
}
don't forget to insert use Illuminate\Http\Request in the top of the file after namespace.
Routes
keep the first and delete the second one.
The reason to your problem :
in the routes file - you called 2 methods with the same name - thats why it arrive to the 2nd route ( the seconed name overwrite the first one );
How to solve it ?
First - delete one of the routes.
Then - add an hidden field in your form, so you can know later which button was clicked
After that - you will need to add an IF in your controller - according to $id
something like this :
if ($yourHiddenField == 1) {
... your code here...
} elseif ($yourHiddenField == 2 ) {
... your code here ...
}
( you will need to get the value of the hidden field first )

"View not defined" error in Laravel

I'm learning Laravel, and I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car, which is identified with the modelesc variable. This form sends the data to a "orders" table. But I keep getting this error "Action App\Http\Controllers\orders not defined." on catalog.blade.php
This is the code that I have made.
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders($modelesc) {
$cars = DB::select('select * from cars where Model = modelesc');
return view('orders', compact('cars'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
{!! Form::hidden(modelesc, $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
{!! Form::open(array('action' => 'index', 'method' => 'POST')) !!}
{!! Form::text(Model, $modelesc) !!}
{!! Form::hidden(users_id, Auth::user()->id) !!}
{!! Form::hidden(Fabrication_date, date(Y-m-d)) !!}
{!! Form::select('colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{!! Form::hidden(Order_status_id, '1' !!}
{!! Form::close() !!}
This is the structure of the table 'orders'. The _id come from other tables, and I want to fill some values of the forms with this id's.
id,
users_id,
Model,
Fabrication_date,
Colour_id,
Order_status_id
Laravel does not know where to find the action orders, because you did not specify a controller (see the thrown exception: it is looking for some class named orders in the namespace App\Http\Controllers\). To do so, you just need to replace
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
with
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
If you want to use a route instead (e.g. you have defined a route named orders which links to a Controller action) you need to replace action with route in the Form::open method:
{!! Form::open(array('route' => 'orders', 'method' => 'GET')) !!}.

How to solve Missing argument 2 for App\Http\Controllers\UserController::destroy() in laravel 5.3?

My view is like this :
#foreach($users as $user)
<tr>
<td>{!! $user->id !!}</td>
<td>{!! $user->username !!}</td>
<td>{!! $user->phone !!}</td>
<td>{!! $user->address !!}</td>
<td>
{!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!}
<div class='btn-group'>
</i>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
#endforeach
My routes\web.php is like this :
Route::get('users/destroy/{year}', 'UserController#destroy')->name('users.destroy.year');
Route::resource('users', 'UserController');
My controller is like this :
public function destroy($id, $year)
{
$user = $this->userRepository->findWithoutFail($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('users.index'));
}
$this->userRepository->delete($id);
Flash::success('User deleted successfully.');
// return redirect(route('users.index'));
return redirect(route('users.index.year', ['year' => $year]));
}
There is exist error like this :
ErrorException in UserController.php line 205: Missing argument 2 for App\Http\Controllers\UserController::destroy()
And the url looks like this : http://localhost/mysystem/public/users/10
When click button delete, I want the url looks like this : http://localhost/mysystem/public/users/index/2020
Is there any people who can help me?
Try this
$year = 2020;
{!! Form::open(['route' => ['users.destroy', $user->id, $year], 'method' => 'delete']) !!}
Maybe you can try :
In routes/web.php just declare the route resource
Route::resource('users', 'UserController');
In you form add an input hidden with the year value
{!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!}
<input type="hidden" name="year" value="2020">
And update your controller like that
public function destroy(Request $request, $id)
{
$user = $this->userRepository->findWithoutFail($id);
$year = $request->year;
if (empty($user)) {
Flash::error('User not found');
return redirect(route('users.index'));
}
$this->userRepository->delete($id);
Flash::success('User deleted successfully.');
// return redirect(route('users.index'));
return redirect('users/index/'.$year);
}
You are using the wrong route name in your open method, it should be users.destroy.year.
So your form will look as:
{!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!}

Passing PHP Variable from GET to current form if isset laravel 5

Ok, I've searched the last couple of days and haven't been able to find an answer that made sense. I'm positive it's doable but not sure if I'm in over my head. I'm new to Laravel 5 and also pretty new to PHP. I'm trying to send a inputted string from the GET['quote'] part of my (laravel collective) form to the same form that is updated with a new view below it, that utilizes the GET variable. Basically keeping whatever someone types in the searchbox after refresh.
Normally I would just echo out the needed variable if it's "set" then leave it empty if it isn't in the 'value' = "" section of the textbox. In Laravel with this form I'm not able to do that due to the rendering of the child view in the parent view(parent first, then child), if I understand correctly. Is there any work around? I'm about ready to just do the old echo within the form if I can't find a solution.
Thanks in advance! This site has helped me with a lot of my questions over the last few months.
Current Form on a Parent View.
{{ Form::open(array('url' => 'search', 'method' => 'get')) }}
{!! Form::text('query', '', [
'class' => "form-table",
'placeholder' => "Search Keywords",
'value' => "{{ $query }}"
]) !!}</td>
{!! Form::submit('Submit') !!}
{{ Form::close() }}
Code below form is to pass the textbox string to another view and show it below this form. I don't know any JS so I'm kind of fumbling about here probably trying to make something work that just wont work.
#yield('child')
Code below here is from my controller.
public function getSearch(){
$title = "Page Title";
$query = "some string"; //This was set to $_GET['query'] but was failing.
return view('pages.search')->with("title", $title)->with("query", $query);
Working Code: For anyone that runs into this issue
Form Code: value = "" was in the wrong place on my original form as well so I've placed the $query into the proper form array below.
{{ Form::open(array('url' => 'search', 'method' => 'get')) }}
{{ Form::text('query', $query, [
'class' => "form-table",
'placeholder' => "Search Keywords",
])
}}
{{ Form::submit('Submit') }}
{{ Form::close() }}
Pages Controller.
Added to the top of my controller below namespace.
use Illuminate\Http\Request;
Controller:
public function getSearch(Request $request){
$title = "Search";
$query = $request->input("query", "");
return view('pages.search', ["title" => $title, "query" => $query]);
}
You were close, but missing a couple of key parts. First you need to pass in the Request to the controller to access, well, the request.
I would highly recommend reading up on this part of the docs before you get stuck into Laravel too much. Responses and requests are the heart of all things back end:
https://laravel.com/docs/5.1/requests
https://laravel.com/docs/5.1/responses
Controller
use Illuminate\Http\Request;
public function getSearch(Request $request)
{
$title = "Page Title";
$query = $request->input("query", ""); // get the 'query' string, or default to an empty string
return view('pages.search', [
'title' => $title,
'query' => $query,
]);
}
View
{!! Form::open(['url' => 'search', 'method' => 'get']); !!}
{!! Form::text('query', $query, [
'class' => "form-table",
'placeholder' => "Search Keywords"
]);
!!}
{!! Form::submit('Submit'); !!}
{!! Form::close(); !!}
First check if query has any value. If not it will return an empty string as the second argument of input field is set to ''. Then it will search through the database for the given keyword.
Controller:
public function getSearch(Request $request)
{
$title = "Page Title";
if(Input::has('query')){
$query = Input::get('query');
}
$data = DB::table('table_name')->where('column_name','like','%'.$query.'%');
// or if you use model you can use this
$data = ModalName::all()->where('column_name','like','%'.$query.'%');
return view('pages.search', compact('title','data'));
}
Blade:
{!! Form::open(array('url' => 'search', 'method' => 'get')) !!}
{!! Form::text('query', '', [
'class' => "form-table",
'placeholder' => "Search Keywords",
]) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
// To view data add the table below search form
<table>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
</tr>
#foreach($data as $d)
<tr>
<td>{{ $d->column_one }}</td>
<td>{{ $d->column_two }}</td>
<td>{{ $d->column_three }}</td>
</tr>
#endforeach
</table>

Categories