I am using Laravel framework as a backend API and a few blade PHP files for the front end, specifically for the authentication and the admin panel from the /admin route.
In /admin, I display a list of all registered users and buttons next to them. (This page is only visible for users that have their value in Admin column set as true). I want to toggle the Admin status of a user, either promoting or demoting them by clicking the button next to the user name.
For this, I tried to use a form submit with get method.
I have a method defined inside UserController like this:
public function setAdmin($id) {
$user = User::find($id);
$user->admin = !$user->admin;
if($user->save()) {
echo "Changed";
}
else {
echo "Could not be changed";
}
}
I want to call this method from the view on the click of a button.
I tried using a Form to send a request by specifying the action, but it gave an error saying the values passed are less than the expected number of parameters.
{!! Form::open(['action' => ['UserController#setAdmin', $user->id], 'method' => 'POST']) !!}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
I have a route set up explicitly to call this action
Route::post('/admin/users/setAdmin', 'UserController#setAdmin')
Although I am not sure if I have to set an explicit route for this action or if it's possible to call a controller function directly from a view without defining the route.
I have iterated through User Model to display all users:
#if(count($users) > 0)
#foreach($users as $user)
<div class="card">
{{ $user }}
</div>
{!! Form::open(['action' => ['UserController#setAdmin', $user->id], 'method' => 'POST']) !!}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
#endforeach
#else
<h2>No users found!</h2>
#endif
EDIT: Added the foreach section of the blade file. Also I modified the 'action' part of the Form::open() parameters, it was a mistype, the parameters error is still there.
Can someone explain how this can be done?
You are trying to pass a parameter to your route but there is any within its declaration. You need to add it in your route path:
Route::post('/admin/users/setAdmin/{id}', 'UserController#setAdmin')
If you don't want to have an URL like this, you should add a hidden input to your form containing your ID:
{!! Form::open(['action' => ['UserController#setAdmin'], 'method' => 'POST']) !!}
{{ Form::hidden('id', $user->id) }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
And in your controller's method:
use Request;
/* ... */
public function setAdmin(Request $request) {
$user = User::find($request->id);
/* ... */
}
Related
I'm trying to call a controller method from a form object, to increment a given item.
The problem is that when adding the parameter, the form action will add a question mark, instead of a slash.
<form method="POST" action="http://localhost/admin/pages?1">
How am I to define the parameter?
{!! Form::open([
'action'=>['Admin\\PagesController#increment', $item->id],
'style' => 'display:inline'
]) !!}
{!! Form::submit('Move Up', ['class' => 'btn btn-danger btn-xs']) !!}
{!! Form::close() !!}
In you code sample, you are sending the item id as a HTTP GET parameter. You can access the item id in your controller by giving a name to the parameter as follows.
{!! Form::open([
'action'=>['Admin\\PagesController#increment','itemId='.$item->id],
'style' => 'display:inline'
]) !!}
Then access the item id in your controller by
Input:get('itemId')
Your function shhould looks like this
public function increment($id)
{
//your code;
}
And your route should have id in it with post request
Route::post('increment/{id}','PagesController#increment');
I am building an application where users can like/unlike each others projects. I have build a system that allow users to like the work and every like gets stored in my database with a unique id, the project_id and the user_id.
Now I am building the Unlike part and get an error when hitting the Unlike button.
The error:
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 161:
My routes:
Route::post('projects/{id}', 'LikesController#store');
Route::get('projects/{id}','LikesController#destroy');
My Controller
public function store(Request $request)
{
$input = Request::all();
$like = new Like;
$like->user_id = Auth::user()->id;
$like->project_id = $input['project_id'];
$like->save();
return redirect('projects/'.$input['project_id']);
}
public function destroy($id)
{
$input = Request::all();
Like::find($id)->delete();
return redirect('projects/'.$input['project_id']);
}
My form
#if (Auth::check())
#if ($isLikedUser)
{!! Form::open(array('url'=>'projects/'.$project->id.'/deletelike','method'=>'POST')) !!}
{!! Form::hidden('project_id', $project->id) !!}
{!! Form::Submit('Unlike', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
#else
{!! Form::open(array('url'=>'projects/'.$project->id,'method'=>'POST', 'id'=>'likeform')) !!}
{!! Form::hidden('project_id', $project->id) !!}
{!! Form::Submit('Like', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
#endif
#else
<p>Log in to like.</p>
#endif
Your Route states that delete is done when the method id GET. But in your unlike button your form method is POST. Change that to GET. And also the action url you are giving for the delete is 'projects/'.$project->id.'/deletelike' But in your routes you dont have a route structured like this. Also if you post this using GET, then hidden project_id is not required since it is already passed in the url. So your delete form should look like this
{!! Form::open(array('url'=>'projects/'.$project->id,'method'=>'GET')) !!}
{!! Form::Submit('Unlike', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
Edit 1
Better change your Routes like this, and use post for both like and unlike:
Route::post('projects/like/{id}', 'LikesController#store');
Route::post('projects/unlike/{id}','LikesController#destroy');
and in your form
{!! Form::open(array('url'=>'projects/like'.$project->id,'method'=>'POST')) !!}
and for unlike form
{!! Form::open(array('url'=>'projects/unlike/'.$project->id,'method'=>'POST')) !!}
and your delete method should be
public function destroy($id)
{
$input = Request::all();
Like::whereProjectId($id)->whereUserId(Auth::user()->id)->delete();
return redirect('projects/'.$input['project_id']);
}
I have created a Form in a blade template. This form sends a POST request to search.postQuery, so I can get the search query and then do something with it and return a View.
This is the route I have defined:
Route::post('/search/{query}', ['as' => 'search.postQuery', 'uses' => 'SearchController#postQuery'])->where('query', '[a-zA-Z0-9]+');
My form looks like this:
{{ Form::open(array('method' => 'POST', 'route' => array('search.postQuery')) }}
{{ Form::text('searchQuery') }}
{{ Form::submit('Zoeken!') }}
{{ Form::close() }}
This is the method the route calls on POST:
public function postQuery($query)
{
var_dump("Landed here");
}
And finally, the error Laravel poses me with is a NotFoundHttpException.
I also discovered that Laravel is constructing a rather strange URL when I press submit: http://homestead.app/search/%7Bquery%7D
What am I doing wrong? As to my knowledge, I'm not doing something very strange?
This is your error
{{ Form::open(array('route'=>'search.postQuery','method' => 'POST')) }}
{{ Form::text('searchQuery') }}
{{ Form::submit('Zoeken!') }}
{{ Form::close() }}
I encounter a error:
Some mandatory parameters are missing ("users") to generate a URL for route "users.update".
I have this set on my view:
{{ Form::open( array('action' => array('UsersController#update')) ) }}
<div> {{ Form::label('username', 'Username:') }}
{{ Form::text('username', $user->username , array('class' => 'form-control')) }}</div>
<div> {{ Form::label('email', 'Email Address:') }}
{{ Form::text('email', $user->email , array('class' => 'form-control')) }}</div>
<div> {{ Form::label('new_password', 'New Password:') }}
{{ Form::text('new_password', '', array('class' => 'form-control')) }} </div>
<div> {{ Form::label('old_password', 'Old Password:') }}
{{ Form::text('password', '', array('class' => 'form-control')) }} </div>
{{ Form::submit() }}
{{ Form::close() }}
I also have a function in my controller linked to update:
public function update() {
return 'This is an update';
}
And finally, when I check all the routes available in Artisan command, I found that the update has a route to: users/{users}
What's wrong with my codes? I'm trying to update a user and it throws this error.
Your route is defined in a way to expect a variable $users to be passed. because of the: {users}
Instead, you should define it like:
Route::post('users/update', 'UsersController#update');
and then in your function update() get the post variable by:
$users_data = Input::get();
OR
if you want to keep the parameter, redefine the form by passing additional parameter:
{{ Form::open( array('action' => array('UsersController#update', $id)) ) }}
The way you are opening the FORM, it is needs a route paramenter. If you dont want to pass parameters, just use the following:
{{ Form::open(array('action' => 'UsersController#update')) }}
Instead of:
{{ Form::open( array('action' => array('UsersController#update')) ) }}
Even when you're setting a action, you may still need a route for it. I STRONGLY recommend you to always use CLEARED DEFINED routes to your controllers. See if the Resource Controllers helps you, in case you don't wanna to define every god damm route (I DON'T).
And, finally answering your question: I think a
{{ Form::open(array('action' => 'UsersController#update')) }}
...may solve your problem. Hope it helps. Sorry for my bad english! :D
I've been reading about this feature: http://laravel.com/docs/html#form-model-binding
And it looks really neat, but there are couple of things that I'm not certain about.
Do I need to put any code in the controller action to process this form? What does that look like?
The model (User) I want to bind in my form has a separate table for addresses. So I want to be able to fill out the User model's fields, but also the fields for the related Address model. Can I do that with form-model-binding, or do I have to handle the form manually?
Or, failing that, can I use form model binding for the user fields, but manually handle the address fields?
You don't need any different code in your controller to process this form. All your (named) form variables will be in Input::all().
The model ($user) you pass in
Form::model($user, array('route' => array('user.update', $user->id)))
Is just any record you need to, if you have more than one table involved, you'll have to do something like
$user = User::where('id',$userID)
->leftJoin('users_addresses', 'users_addresses.user_id', '=', 'users.id')
->first();
And pass this composed model to your Form::model().
How you name your inputs is entirely up to you, because you'll have to write the logic to process your form. But, in my opinion users_address[street] for the address inputs is good, because you'll end up with an array of addresses columns that you can pass right away to your UserAddress model.
<html>
<head>
<title></title>
</head>
<body>
{{ Form::model($user, array('route' => array('user.update', $user->id))) }}
{{ Form::label('first_name', 'First Name:', array('class' => 'address')) }}
{{ Form::text('first_name') }}
{{ Form::label('last_name', 'Last Name:', array('class' => 'address')) }}
{{ Form::text('last_name') }}
{{ Form::label('email', 'E-Mail Address', array('class' => 'address')) }}
{{ Form::text('email') }}
{{ Form::label('address[street1]', 'Address (Street 1)', array('class' => 'address')) }}
{{ Form::text('address[street1]') }}
{{ Form::label('address[street2]', 'Address (Street 2)', array('class' => 'address')) }}
{{ Form::text('address[street2]') }}
{{ Form::label('ddress[city]', 'City', array('class' => 'address')) }}
{{ Form::text('address[city]') }}
{{ Form::label('address[state]', 'State', array('class' => 'address')) }}
{{ Form::text('address[state]') }}
{{ Form::label('address[zip]', 'Zip Code', array('class' => 'address')) }}
{{ Form::text('address[zip]') }}
{{ Form::submit('Send this form!') }}
{{ Form::close() }}
</body>
</html>
And if you do dd( Input::all() ) in your controller, you'll get something like this:
This result is provided by Kint's dd(): https://github.com/raveren/kint. Really helpful.
If your form just have fields from a single Model, your update method can be very simple and look something like:
public function update($id)
{
$user = User::find($id);
if (!$user->update(Input::all())) {
return Redirect::back()
->with('message', 'Something wrong happened while saving your model')
->withInput();
}
return Redirect::route('user.saved')
->with('message', 'User updated.');
}
On forms a little bit more complex, coders will have to add more logic to their controllers, in you case with a little bit more of research I think you can make this happen:
public function update($id)
{
$user = User::find($id);
$inputs = Input::all();
if (!$user->update($inputs)) {
$address = new UserAddress($inputs['address']);
$user->address()->save($address);
...
}
...
}
In Laravel 5.1 for relation model binding you just need to eager load relation table(s):
$user = User::with(['address'])->find($id);
And in view set fields names as array:
{!! Form::model($user, ['route' => ['user.update', $user->id]]) !!}
{!! Form::text('address[street]') !!}
{!! Form::text('address[number]') !!}
{!! Form::close() !!}