MethodNotAllowedHttpException form error - php

I have created a form in Laravel so here are the following files:
The form that someone should submit some details,
contact.blade.php:
#extends('layouts.layout')
#section('content')
<main role="main">
<section class="jumbotron text-center">
<div class="container">
<h1 class="jumbotron-heading">Laravel demo</h1>
<p class="lead text-muted">Please fill the form</p>
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
</div>
</section>
<div class="album text-muted">
<div class="container">
{!! Form::open(['url' => 'contact/submit']) !!}
{!! csrf_field() !!}
<div class="form-group">
{{Form::label('name', 'Name') }}
{{Form::text('name', 'Enter Name', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('email', 'E-Mail Address') }}
{{Form::text('email', 'example#gmail.com', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('message', 'Enter Message') }}
{{Form::textarea('message', 'Enter Message', ['class'=> 'form-control'])}}
</div>
<div>
{{Form::submit('Submit', ['class'=> 'btn btn-primary'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</main>
#endsection
Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MessageController extends Controller
{
public function submit(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required'
]);
return 'SUCCESS';
}
}
*
In the Routes web.php file i have included the method as post:
Route::get('/', function () {
return view('home');
});
Route::get('/contact', function () {
return view('contact');
});
Route::post('/contact/submit', 'MessageController#submit');
The error message is " RouteCollection.php (line 251)" .After searching for similar occassions here the problem ussually occurs when in the Routes you use a different method to the specified route method. I'm using POST method for submiting details, I still cannot understand why I get this.
Any help would be appreciated.

Instead of this {!! Form::open(['url' => 'contact/submit']) !!}
Try this.
{!! Form::open(['method'=>'POST','action'=>'MessageController#submit']) !!}

Add the back slash to the url of the form like so :
{!! Form::open(['url' => '/contact/submit']) !!}

Related

cover image must be an image in laravel 8

I'm new with Laravel 8, I have been trying uploading a form with a file field but I keep getting an error "image must be an image", even though I have enctype='multipart/form-data' included in a form.
Blade Template
#extends('layouts.app')
#section('contents')
<div class="container">
<h1> Create a Posts</h1>
{!! Form::open(['action' =>['App\Http\Controllers\PostController#store','enctype'=>'multipart/form-data', 'method'=>'POST']]) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title','',['class'=>'form-control','placeholder'=>'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body','',['class'=>'form-control','id'=>'article-ckeditor','placeholder'=>'Bodytext'])}}
</div>
<br>
<div class="form-group">
<div class="image">
{{Form::file('cover_image')}}
</div>
</div>
<br>
{{Form::submit('Submit',['class'=>'btn btn-primary']);}}
{!! Form::close() !!}
</div>
#endsection
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\User;
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => ['required', 'unique:posts', 'max:500000'],
'body' => ['required'],
'cover_image'=>['image','nullable','max:19999']
]);
//handle file uploads
if($request->hasfile('cover_image')){
return '123';
}
}
Route
Route::resource('/post', PostController::class);
You need to allow file uploads when you open the form :
#extends('layouts.app')
#section('contents')
<div class="container">
<h1> Create a Posts</h1>
{!! Form::open(['action' =>['App\Http\Controllers\PostController#store','enctype'=>'multipart/form-data', 'method'=>'POST',
// add files true in the form open
'files' => true]]) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title','',['class'=>'form-control','placeholder'=>'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body','',['class'=>'form-control','id'=>'article-ckeditor','placeholder'=>'Bodytext'])}}
</div>
<br>
<div class="form-group">
<div class="image">
{{Form::file('cover_image')}}
</div>
</div>
<br>
{{Form::submit('Submit',['class'=>'btn btn-primary']);}}
{!! Form::close() !!}
</div>
#endsection
see https://laravelcollective.com/docs/6.x/html#opening-a-form

Blade Form not showing flash message and is not performing validation on form fields

O. I am in need of help with this problem. I have a contact form within a blade.php file, I have a route set up in my web.php file and I have a controller set up which is routed from the web.php file and is to perform validation on the fields and display a flash message on the page when the form is submitted. Right now the form is properly being submitted to my database so it is working but if I submit with a blank form, the validation is not working as it should (laravel) and also the flash message does not show upon successful form submission:
CODE:
Web.php
<?php
Route::get('/', 'HomeController#index')->name('home');
Route::post('/contact/submit','MessagesController#submit');
MessagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Message;
class MessagesController extends Controller
{
public function submit(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|min:2',
'email' => 'required|max:255',
'phonenumber' => 'required|min:10|max:10',
'message' => 'required|min:5',
]);
Message::create($validatedData);
return redirect('/')->with('success', 'Message has been sent');
}
}
contact.blade.php
{{--CONTACT FORM--}}
<section id="contact">
<div class="container-fluid padding">
<div class="row text-center padding">
<div class="col-12">
<h2>Contact PDMA</h2>
</div>
<div class="col-12 padding">
{!! Form::open(['url' => 'contact/submit']) !!}
#csrf
<div class="form-group">
{{Form::label("name", 'Name')}}
{{Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'Enter name'])}}
</div>
<div class="form-group">
{{Form::label("email", 'E-Mail Address')}}
{{Form::text('email', '', ['class' => 'form-control', 'placeholder' => 'Enter email'])}}
</div>
<div class="form-group">
{{Form::label("phonenumber", 'Phone Number')}}
{{Form::text('phonenumber', '', ['class' => 'form-control', 'placeholder' => 'Enter phone number'])}}
</div>
<div class="form-group">
{{Form::label("message", 'Message')}}
{{Form::textarea('message', '', ['class' => 'form-control', 'placeholder' => 'Enter message'])}}
</div>
<div>
{{Form::submit('Submit Form', ['class' => 'btn btn-success'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</section>
Just use Session:
First Import Session class in your controller
use Session;
Message::create($validatedData);
Session::flash('success', 'Message has been sent');
return redirect('/')
Then Create a blade file in view folder, you can call it whatever you want, e.g: notify.blade.php
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="bottom:10px; position: fixed; left:2%; z-index:100">
×
<h4 class="alert-heading">Well done!</h4>
<p>{{ Session::get('success') }}</p>
</div>
#endif
#if (Session::has('danger'))
<div class="alert alert-danger" role="alert" style="bottom:10px; position: fixed; left:2%; z-index:100">
×
<h4 class="alert-heading">Error!</h4>
<p>{{ Session::get('danger') }}</p>
</div>
#endif
Finaly, include this file in any view.
include('notify')
As liverson suggested create the blade file for the session
And the other thing you can also is catch the error and change the input style using another blade something like error.blade.php and include it in your form
#if($errors->any())
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
For the Form you can add {{$errors->has('name') ? 'is-danger' : ''}} to your div class
For Example
<div class="form-row text-left">
<label for="name" class="col-md-3">Name</label>
<div class="col-md-9">
<input type="text" name="name" class="input {{$errors->has('name') ? 'is-danger' : ''}}" required
value= #if(isset($user))"{{$user->name}}"#else "{{old('name')}}"#endif>
</div>
</div>
https://laracasts.com/series/laravel-from-scratch-2018/episodes/15

Laravel 5.4 update user profile not working

I am trying to update the profile of the user who is logged in. I like to create a function that the user that has logged in when he/she wants to update or edit his/her own profile. So I got no errors. But the problem is it wont update and it just refresh my browser but nothing happens.
Here is the code on my controller
public function edit($id)
{
// $applicant = $this->applicantRepository->findWithoutFail($id);
$applicant = Applicant::where('id',$id)->get()->last();
if (empty($applicant)) {
Flash::error('Applicant');
return redirect(route('applicant.home'));
}
return view('applicant-dashboard.edit')->with('applicant', $applicant);
}
public function update($id, UpdateApplicantRequest $request)
{
$applicant = $this->applicantRepository->findWithoutFail($id);
if (empty($applicant)) {
Flash::error('Applicant not found');
return redirect(route('applicant.home'));
}
$input = $request->all();
$applicant = $this->applicantRepository->update([
'name' => $input['name'],
'address' => $input['address'],
'cellphone_no' => $input['cellphone_no']], $id);
Flash::success('Profile updated successfully.');
return redirect(route('applicant.edit'));
}
Here is the code in my routes:
Route::get('/edit/{id}', 'HomeController#edit')->name('applicant.edit');
Route::post('/update', 'HomeController#update')->name('applicant.update');
Here is the code in my views:
edit.blade.php
#extends('applicant-dashboard.layouts.app')
#section('content')
<section class="content-header">
<h1>
Applicant Profile
</h1>
</section>
<div class="content">
{{-- #include('adminlte-templates::common.errors') --}}
<div class="box box-primary">
<div class="box-body">
<div class="row" style="padding-left: 20px">
{!! Form::model($applicant, ['route' => ['applicant.update', $applicant->id], 'method' => 'post']) !!}
#include('applicant-dashboard.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
and in fields.blade.php
{!! Form::hidden('id', null, ['class' => 'form-control input-sm','required']) !!}
<!-- Name Field -->
<div class="row" style="padding-right: 15px">
<div class="form-group col-sm-4">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control input-sm','required']) !!}
</div>
</div>
<div class="row" style="padding-right: 15px">
<!-- Address Field -->
<div class="form-group col-sm-4">
{!! Form::label('address', 'Address:') !!}
{!! Form::text('address', null, ['class' => 'form-control input-sm','required']) !!}
</div>
</div>
<!-- Cellphone Field -->
<div class="row" style="padding-right: 15px">
<div class="form-group col-sm-4">
{!! Form::label('cellphone_no', 'Cellphone:') !!}
{!! Form::text('cellphone_no', null, ['class' => 'form-control input-sm','required']) !!}
</div>
</div>
<div class="row" style="padding-right: 15px">
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
</div>
code in my model for validation:
public static $rules = [
'name' => 'required',
'email' => 'required|unique:applicants',
'password' => 'required',
'password_confirmation' => 'required|same:password'
];
and the UpdateApplicantRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\Applicant;
class UpdateApplicantRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return Applicant::$rules;
}
}
I am looking for help.
Thanks in advance.
There can be several options. Firstly you need to make sure that right url mapping is created, secondly if the validation is not failing and thirdly if any other middleware is redirecting, for example user is not logged in.
Change the beginning of update method to be
public function update($id, Request $request) {
dd($request->all());
If you do not see dd() output then mapping is not right. If you see then validation fails.
Check php artisan route:list and see what middleware is assigned to this route.
Right now it looks like email and password are missing from the fields. Usually you have different validation rules when creating and when updating.
Also I advise to have displaying validation errors in your template.

Cannot Access $errors in laravel 5.2

I am having a issue with Laravel 5.2 accessing the $errors variable in my partial.
routes.php
I have wrapped my routes in the middleware web.
and in my auth.blade.php
<div class="col-md-4">
<div class="panel panel-{{ $errors->all() ? 'danger' : 'default'}}">
<div class="panel-heading">
`enter code here`<h2 class="panel-title">#yield('heading')</h2>
</div>
<div class="panel-body">
#if($errors->all())
<div class="alert alert-danger">
<strong>We found some errors</strong>
<ul>
#foreach($errors->all() as $errors)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#yield('content')
</div>
</div>
</div>
here is the code for my login partial
#extends('layouts.auth')
#section('title','Login')
#section('heading','Welcome Please login')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Login', ['class' => 'btn btn-primary']) !!}
Forgot password?
{!! Form::close() !!}
#endsection
when i click login it just redirects to the same page buit not flashing the errors?
When i click login it shows this error
enter image description here
Remove web middleware, this may cause empty $errors. Since 5.2.27, web middleware aplies automatically to all routes and you shouldn't add it again manually.

Laravel 5 : Submit form doesnt work

I can't submit a form with laravel. Nothing even happens, no errors is shown. Page just stays the same as it was.
This is my route file:
Route::resource('/', 'WebsiteController');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
This is file with form:
<div class="col-lg-12">
{!! Form::open(['url' => '/']) !!}
<div class="row">
<div class="col-md-6">
<div class="form-group wow fadeInLeft">
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Your name *','id'=>'name']) !!}
</div>
<div class="form-group wow fadeInLeft">
{!! Form::text('email',null,['class'=>'form-control','placeholder'=>'Your email *','id'=>'email']) !!}
</div>
<div class="form-group wow fadeInLeft">
{!! Form::text('phone',null,['class'=>'form-control','placeholder'=>'Your phone *','id'=>'phone']) !!}
</div>
</div>
<div class="col-md-6">
<div class="form-group wow fadeInRight">
{!! Form::textarea('message',null,['class'=>'form-control','placeholder'=>'Your message *','id'=>'message']) !!}
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center wow bounceIn">
{!! Form::submit('Click Me!') !!}
</div>
</div>
{!! Form::close() !!}
#if ($errors->any())
<ul class='alert alert-danger' style='list-style: none;'>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
</div>
This is my CreateContactRequest file:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateContactRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|min:3',
'phone' => 'required',
'email' => 'required|email',
'message' => 'required'
];
}
}
And finally, this is my store method in WebsiteController:
public function store(CreateContactRequest $request)
{
$this->createContact($request);
}
Included files on top of the WebsiteController file:
use App\Contact;
use App\Http\Requests;
use App\Http\Requests\CreateContactRequest;
use Illuminate\HttpResponse;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Request;
Any help is welcomed and appreciated.

Categories