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');
Related
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);
/* ... */
}
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 (identified by $modelesc). This form sends the data to a "orders" table.
I'm now getting the following error in orders.blade.php:
Parse error: syntax error, unexpected '<', expecting ']'
I don't understand why I'm getting '<'; I don't see this in the code!
This is my code so far:
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders($modelesc=null) {
$cars = DB::table('cars')->where('Model', '=', '$modelesc');
return view('orders', compact('cars'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'CarController#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', $car->Model) !!}
{!! 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 orders table. The *_id fields come from other tables, and I want to fill some values of the forms with the relevant entry (id, users_id, Model, Fabrication_date, Colour_id, Order_status_id).
Firstly you need to wrap the Form names within quotes, the Order Status ID was also missing a closing bracket:
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::hidden('Order_status_id', '1') !!}
Next, if $colours is a collection you can do the following in Laravel 5.4 (I'm unsure which version you're using)
{!! Form::select('Colour', $colours->pluck('Colour')) !!}
If you're on Laravel 5.1 or prior, you'll do the following:
{!! Form::select('Colour', $colours->lists('Colour')) !!}
This is because the lists method was removed in 5.2.
#pseudoanime is also correct with his answer, the database call needs the get method adding
try changing $cars = DB::table('cars')->where('Model', '=', '$modelesc');
$cars = DB::table('cars')->where('Model', '=', $modelesc)->get();
Your orders method in your CarController doesn't set $cars to what you'd think; without calling get(), it'll be an instance of Builder instead of an array! Furthermore, you're only fetching cars where the model equals the string "$modelesc" instead of the value of $modelesc.
Furthermore, in your orders.blade.php file, you seem to reference a $colours variable, but that doesn't get passed down in your CarController.
Change your orders method to the one below:
public function orders($modelesc = null)
{
$cars = DB::table('cars')
->where('Model', $modelesc)
->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
There is also an issue with your Form::select call in your orders.blade.php file; the Blade template directives (#foreach, #endforeach) are only for the "HTML" portion of the Blade file. Since we called pluck() on the Collection of colours in the CarController, we can simply pass in $colours for that method.
{{-- Incorrect --}}
{!! Form::select('Colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{{-- Correct --}}
{!! Form::select('Colour', $colours) !!}
You also appear to be using constants in your Form builders (instead of strings with quotes); these should be strings only.
{!! Form::open(['action' => 'index', 'method' => 'POST']) !!}
{!! Form::text('Model', $car->Model) !!}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::close() !!}
I have only two forms for the moment in my view, they are both post method I tried to solve it like this
route
Route::post('view', function(){
if(Input::has('form1')){
'NameController#method1';
} elseif (Input::has('form2')){
'NameController#method2';
}
});
view
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', $trans->text) !!}
{!! Form::submit('Submit', array('name' => 'form1' )) !!}
{!! Form::close() !!}
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', 'text') !!}
{!! Form::submit('Submit', array('name' => 'form2')) !!}
{!! Form::close() !!}
And it throw's out this error
syntax error, unexpected ''ConfigurationController#title' (T_CONSTANT_ENCAPSED_STRING)
it was error in coding I corrected it but it won't do what I wish it simply returns blank screen it doesn't cycle trough controllers
I modified the code(removed return and closed the route)
What you want to do is create a method someMethodName in the NameController and in there
public function someMethodName()
{
if(Input::has('form1')){
$this->method1();
} elseif (Input::has('form2')){
$this->method2();
}
}
then replace all the route stuff with
Route::post('view', 'NameController#someMethodName')
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 the following code:
In my view:
{{Form::open()}}
{{Form::hidden ('hiddentitle', 'title info')}}
{{Form::close()}}
In my controller:
class PController extends BaseController {
public function user(){
$hiddentitle = Input::get('hiddentitle');
dd($hiddentitle); //I get NULL values
return View::make('person.user');
}
}
Can anyone explain why I get NULL values for $hiddentitle? I know this seems very simple but it's a big problem. Thanks
How are you submitting the form?
I just tested this code and it works for me with Laravel 5.
routes.php
Route::resource('test', 'TestController');
Controller
public function create()
{
return view('test.create');
}
public function store()
{
dd(\Input::get('hiddentitle'));
}
test/create.blade.php
{!! Form::open() !!}
{!! Form::hidden ('hiddentitle', 'title info')!!}
{!! Form::submit('Go') !!}
{!! Form::close() !!}
results
"title info"
Edited per comments
I am unclear on exactly what you are trying to do. If you post your attempts I could answer your question better. I believe you would like to do something like this.
#foreach ($user->books as $book)
#if(!emtpy($book->title)
{!! Form::open() !!}
{!! Form::hidden ('hiddentitle', 'title info')!!}
{!! Form::submit('Go') !!}
{!! Form::close() !!}
#endif
#endforeach