I'll copy the relevant code and then i'll explain the problem.
routes.php
get('/movies/create', 'MovieController#create');
Moviecontroller.php
public function create()
{
return view('movies.create');
}
master.blade.php
<html>
<head>
<title>Laravel</title>
</head>
<body>
#yield('content')
</body>
</html>
edit.blade.php
#extends('master')
#section('content')
<h1>Edit {{ $movie->name }}</h1>
{!! Form::model($movie, ['url' => '/movies/' . $movie->id, 'method' => 'PATCH']) !!}
{!! Form::text('name') !!}
{!! Form::text('director') !!}
{!! Form::textarea('description') !!}
{!! Form::text('rating') !!}
{!! Form::submit('Update movie') !!}
{!! Form::close() !!}
#stop
show.blade.php
#extends('master')
#section('content')
<h1>{{ $movie->name }}</h1>
<h4>Director: {{ $movie->director }}</h4>
<h4>Rating: {{ $movie->rating }}</h4>
<p>Description: {{ $movie->description }}</p>
{!! link_to('/movies/' . $movie->id . '/edit', 'Edit') !!}
#stop
so i have this code, but when i go to /movies/create in the browser, it's trying to open show.blade.html, which, of course, will throw an exception ($movie does not exist). Why does that happen?
You probably have a conflicting route above the one you showed, something like this:
get('/movies/{movie}', 'MovieController#show');
get('/movies/create', 'MovieController#create');
So when you go to yoursite.com/movies/create in your browser, the first route is triggered, and the controller opens show.blade.php - but there is no movie for it to show yet.
If you move them the other way around, the create method will work as intended, and you'll still be able to show existing movies:
get('/movies/create', 'MovieController#create');
get('/movies/{movie}', 'MovieController#show');
best way:
php artisan make:controller MovieController
and define route
Route::resource('movies', 'MovieController');
//available routes
Verb Path
GET /movies
GET /movies/create
POST /movies
GET /movies/{id}
GET /movies/{id}/edit
PUT/PATCH /movies/{id}
DELETE /movies/{id}
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);
/* ... */
}
Good day to all, I am new to Laravel framework and as I read some articles about HTTP verbs which say that POST is universal to both PUT and DELETE (reference:
https://softwareengineering.stackexchange.com/questions/114156/why-are-there-are-no-put-and-delete-methods-on-html-forms
) and thus I wonder if it is possible to achieve Update and Delete functionality with POST and without using hidden() in the view itself. For example, if you look at the code below hidden method is used to clarify which HTTP verb should be used in this case PUT and what if I remove it and try to update the data will it be possible.
The code:
#extends('layouts.app')
#section('content')
<h1>Income</h1>
<br>
{!! Form::open(['action'=>['IncomeController#update',$income->id], 'method' =>'POST']) !!}
<div class="form-group">
{{Form::label('Title', 'Title')}}
{{Form::text('Title', $income->Title, ['class' =>'form-control', 'placeholder' =>'Enter title'])}}
</div>
**{{Form::hidden('_method', 'PUT')}}**
{{Form::submit('Create', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
#stop
suppose if you have Route like this
Route::put('income/update/{id}', 'IncomeController#update');
Route::delete('income/delete/{id}', 'IncomeController#delete');
Then you can replace your route with this
Route::post('income/update/{id}', 'IncomeController#update');
Route::post('income/delete/{id}', 'IncomeController#delete');
And then change your form like this (Just remove the hidden field)
#extends('layouts.app')
#section('content')
<h1>Income</h1>
<br>
{!! Form::open(['action'=>['IncomeController#update',$income->id], 'method' =>'POST']) !!}
<div class="form-group">
{{Form::label('Title', 'Title')}}
{{Form::text('Title', $income->Title, ['class' =>'form-control', 'placeholder' =>'Enter title'])}}
</div>
{{Form::submit('Create', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
#stop
I am new to laravel.I am trying to create a basic form in my form.blade.php page.But when i try to navigate to my form.blade.php page i am getting the following error:
FatalErrorException in 5fca28a34b9eeee32d99bfd5ad77d6a463cb98c9.php
line 23: syntax error, unexpected 'put' (T_STRING), expecting ')'
in my route.php i am calling the View::make() method to get to the form.blade.php page
Route::get('/',function(){
return View::make('form');
});
form.blade.php page::
<!DOCTYPE html>
<html>
<body>
{{Form::open(array('url'=>'thanks'))}}
{{Form::label('email','Email Address')}}
{{Form::text('email')}}
{{Form::label('os','operating system')}}
{{Form::select('os',array(
'linux'=>'Linux',
'mac'=>'Mac',
'windows'=>'windows'
))}}
{{Form::label('comment','Comment')}}
{{From::textarea('comment','',array('placeholder=>'put your comment here'))}}
{{Form::checkbox('agree','yes',false)}}
{{Form::label('agree','i agree with your terms and condition')}}
{{Form::submit('submit')}}
{{Form::close()}}
</body>
</html>
{!! Form::open(array('url'=>'thanks')) !!}
{!! Form::label('email','Email Address') !!}
{!! Form::text('email') !!}
{!! Form::label('os','operating system') !!}
{!! Form::select('os',array(
'linux'=>'Linux',
'mac'=>'Mac',
'windows'=>'windows'
)) !!}
{!! Form::label('comment','Comment') !!}
{!! Form::textarea('comment','',array('placeholder'=>'put your comment here')) !!}
{!! Form::checkbox('agree','yes',false) !!}
{!! Form::label('agree','i agree with your terms and condition') !!}
{!! Form::submit('submit') !!}
{!! Form::close() !!}
Above is the correct way to use form/html, here is the documentation laravelcollective/html
{{From::textarea('comment','',array('placeholder=>'put your comment here'))}}
should be
{{From::textarea('comment','',array('placeholder'=>'put your comment here'))}}
Seems like you are missing a single quote.
Im a beginner with laravel. Im following the laracasts tutorial and Im stuck at the part where you access another page with a form by:
{{ Form::open(['url' => 'created']) }}
for example.
Now that leads me to the right url but it gives me
Whoops, looks like something went wrong.
As soon as I type the link manually it works normally.
This is the code of the page where it directs to:
controller:
public function created()
{
return 'hello';
}
Routes:
Route::get('created', 'TestController#created');
View:
#extends('layout')
#section('content')
<h1> Test </h1>
#stop
This is the form of the 1st page:
#extends('layout')
#section('content')
<h1>Create New User</h1>
{{ Form::open(['url' => 'created']) }}
<div>
{{ Form::label('email', 'E-mail:')}}
{{ Form::text('email')}}
</div>
<div>
{{ Form::label('password', 'Password:')}}
{{ Form::password('password')}}
</div>
<div>
{{ Form::submit('Create')}}
</div>
{{ Form::close()}}
#stop
What is going wrong here?
Form open by default links to a post method so what you need is either a post route or a get method. Following should work:
{{ Form::open(['url' => 'created']) }}
// Insert your fields/codes here
{{ Form::close() }}
//Change route method to post
Route::post('created', 'TestController#created');
Please read the documentation here for more details.
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