I'm using PHPstorm with Laravel 5.2.3, and currently trying to update a persons information by reusing a form from my create page. I've followed everything to the T from the laracast tutorials, but for some reason I'm getting the MethodNotAllowed error when I hit submit on an update.
Routes
Route::group(['middleware' => ['web']], function()
{
...
Route::get('create','ResourceController#create');
Route::post('create', 'ResourceController#store');
Route::resource('pages', 'ResourceController');
});
Controller
class ResourceController extends Controller
{
...
public function create()
{
return view('pages.create');
}
public function store(Requests\CreateNewContactRequest $request)
{
ContactPerson::create($request->all());
return redirect('resource');
}
public function edit($id)
{
$user = ContactPerson::findOrFail($id);
return view('pages.edit')->with(compact('user'));
}
public function update($id, Request $request)
{
$user = ContactPerson::findOrFail($id);
$user->update($request->all());
return redirect('pages.resource');
}
}
Edit View
#extends('app')
#section('content')
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['ResourceController#update', $user->id]]) !!}
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First_Name', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last_Name', null, ['class' => 'form']) !!}
</div>
...
{!! Form::close() !!}
View Source
It shows that it's a POST method with laravel spoofing it as a PATCH.
Error
It generates wrong action in <form> tag. Try using:
'route' => ['pages.update', $user->id]
instead of:
'action' => ['ResourceController#update', $user->id]
Is it works?.
Related
I'm new to Laravel. I've created a form and trying to delete one of the post. I just want to know how opening a form from Laravel collectives works.
Currently, this is how my form looks like in show.blade.php
{!! Form::open(['action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{!! Form::close() !!}
The above form gives me the error Action PostsController#destroy not defined.
But when I add 'url' => 'posts/' in Form i.e.
{!! Form::open(['url' => 'posts/', 'action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
The above error disappears.
Below is the destroy() function in PostsController
class PostsController extends Controller
{
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect('/posts')->with('success','Post removed');
}
}
web.php
// --- Posts Routing
Route::get('/posts', [App\Http\Controllers\PostsController::class, 'index'])->name('posts');
MY QUESTIONS
I'm redirecting in the destroy() function in PostsController, why url is necessary with action in Form from Laravel
collectives to avoid the above error?
When to use 'url' and when to use action?
I've laravel 4.2.10.
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');
I want to save some form data, and I'm Getting error.
The Error
Action App\Http\Controllers\Admin\ConcursoController#store not defined. (0)
My Form
{!! Form::open(['action'=>'Admin\ConcursoController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('company','Entidade')}}
{{Form::text('company','',['class' => 'form-control', 'placeholder' => 'Nome da entidade aquĆ..'])}}
</div>
{{Form::submit('submeter', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
My Route
$this->group(['middleware' => ['auth:admin'], 'namespace' => 'Admin', 'prefix' => 'admin'], function(){
$this->get('/', 'AdminController#index')->name('admin.home');
$this->resource('concursos', 'ConcursoController');
});
Controller index Method
public function index()
{
$concursos = Concurso::all();
$title = 'Concursos';
return view('admin.concursos.index',compact('title'))->with('concursos',$concursos);
}
Controller Create method
public function create()
{
return view('admin.concursos.create');
}
Controller Store Method
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
]);
//Criar concurso
$concurso = new Concurso;
$concurso->title = $request->input('title');
$concurso->body = $request->input('body');
$concurso->save();
return redirect('/admin/concursos')->with('Success', 'Concurso Adicionado');
}
Laravel version 5.7.14
Probably check this file: App\Http\Controllers\Admin\ConcursoController and see if you have a function/method called 'store'. The error is pretty straightforward that script can't find that function.
replace your form
['route' => ['concursos.store']
like
{!! Form::model($transactions, ['route' => ['transaction.store'], 'method' => 'POST','class'=>"form-horizontal"]) !!}
I'm trying to get the correct routes for the Laravel Contact form on a single page website but I'm not sure how to apply the routes so far, since i've done it with sites that aren't single page (parallax - like website/ scrollable).
These are the routes I am creating, so everything stays on the homepage(no redirects at all because it's a one page scrollable website)
Route::get('/', 'ContactUsController#create')->name('contact.create');
Route::post('/', 'ContactUsController#store')->name('contact.store');
My Controller looks like this: Please note that the create controller returns the view to my index which of course is routed like so ('/'),
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\ContactEmail;
class ContactUsController extends Controller
{
public function create()
{
return view('index');
}
public function store(Request $request)
{
$contact = [];
$contact['name'] = $request -> get('name');
$contact['phone'] = $request -> get('phone');
$contact['email'] = $request -> get('email');
$contact['subject'] = $request -> get('subject');
$contact['message'] = $request -> get('message');
//send mail logic here
Mail::to(config('mail.support.address'))->send(new ContactEmail($contact));
flash('Your Message has been sent!) -> success();
return redirect()-> route('/');
}
}
And below is my Contact Form:
{!! Form::open(['route' => 'contact.store', 'class' => 'text-light '])!!}
{!! Form::label('name', 'Your Name', ['class' => 'text-light'])!!}
{!! Form::text('name', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('phone', 'tel', ['class' => 'text-light'])!!}
{!! Form::text('phone', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('email', 'Email', ['class' => 'text-light'])!!}
{!! Form::text('email', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('subject', 'Subject', ['class' => 'text-light'])!!}
{!! Form::text('subject', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('message', 'Your Message Here..', ['class' => 'text-light'] )!!}
{!! Form::textarea('message', null, ['class' => 'form-control text-light'])!!}
{!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
#if($errors -> any())
<div class="alert alert-danger">
<ul>
#foreach($errors -> all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#include('flash::message')
Solved problem with solution :
//routes for contact form
Route::get('/contacts', 'ContactUsController#create')->name('contact.create');
Route::post('/contacts', 'ContactUsController#store')->name('contact.store');
Everything else same.
Thanks guys for all support.
//Create the routes in web.php
Route::get('contact',['as' => 'contact, 'uses' => 'ContactController#create']);
Route::post('contact',['as' => 'contact', 'uses' => 'ContactController#store']);
// Create the ContactController using the command
php artisan make:controller ContactController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use Mail;
class ContactController extends Controller
{
public function create()
{
return view('front.contact.index');
}
public function store(ContactRequest $request)
{
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('hello#outlined.co');
$message->to('poornima#outlined.co', 'outlined')->subject('New Contact
Request from outlined');
});
return back()->with('status', 'your message has been received');
}}
On successful form submission, I should be seeing 'in here' within the browser but I get re-directed to a page not found view:
Sorry, the page you are looking for could not be found.
My form control:
{!! Form::open(['url' => 'prize_draw_store', 'class' => 'wffm-form-module text-left']) !!}
{!! Form::label('fullname', 'Name:', array('class' => 'control-label')) !!}
{!! Form::text('fullname', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
{!! Form::label('email_address', 'Email Address:', array('class' => 'control-label')) !!}
{!! Form::text('email_address', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
<div class="form-submit-border">
{{ Form::submit('Submit', array('class' => 'btn btn-default')) }}
</div>
{!! Form::close() !!}
My routes are well-defined:
/routes/web.php
use App\Http\Controllers\PrizeDrawController;
use App\Http\Requests\PrizeDrawFormRequest;
Route::get('/',
['as' => 'prize_draw', 'uses' => 'PrizeDrawController#create']);
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
PrizeDrawController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\PrizeDrawFormRequest;
class PrizeDrawController extends Controller
{
public function create() {
return view('front.pages.home');
}
public function store(PrizeDrawFormRequest $request) {
var_dump('in here');
return redirect('thankyou')->with('status', 'Form submitted!');
}
}
PrizeDrawFormRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PrizeDrawFormRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'email_address' => 'required|email',
'fullname' => 'required'
];
}
}
I'm expecting the code to flow like thus:
1. http://aacomp.local/prize_draw_store -- POST form submission
2. App\Http\Controllers\PrizeDrawController#store
3. PrizeDrawController::store(PrizeDrawFormRequest $request)
4. Redirect a user to thankyou page
We don't seem to reach step 3 though? I must be missing something basic -
are you able to help?
Thanks in advance.
The problem lies with:
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
Changing this route to
Route::post('/foobar',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
and updating the form action:
{!! Form::open(['url' => 'foobar', ...
say - will get the desired results. Thanks to Darius. V for this.