Laravel 5 : Submit form doesnt work - php

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.

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

How to add Edit and Delete function in my FullCalendar laravel ph?

I'm doing a project in laravel, where I can show my list of events using fullcalendar. I already created/have a fullcalendar view with events and adding event, but I want to be able to click the date and edit it or delete it.
Sample event fullcalendar
I already search some solutions or guides for this, but I can't work it properly. I tried creating another one from scratch but it doesn't work.
Here are the links that I searched and doing it as my guide.
FullCalendar Events and Scheduling
github driftingruby/042-fullcalendar
This is my codes in my create.blade.php
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">Event Calendar</div>
<div class="panel-body">
{!! Form::open(array('route' => 'admin/events.create','method'=>'POST','files'=>'true')) !!}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
#if (Session::has('success'))
<div class="alert alert-success">{{ Session::get('success') }}</div>
#elseif (Session::has('warning'))
<div class="alert alert-danger">{{ Session::get('warning') }}</div>
#endif
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
{!! Form::label('event_name','Event Name:') !!}
<div class="">
{!! Form::text('event_name', null, ['class' => 'form-control']) !!}
<!-- {!! $errors->first('event_name', '<p class="alert alert-danger">:message</p>') !!} -->
</div>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
{!! Form::label('start_date','Start Date:') !!}
<div class="">
{!! Form::date('start_date', null, ['class' => 'form-control']) !!}
<!-- {!! $errors->first('start_date', '<p class="alert alert-danger">:message</p>') !!} -->
</div>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
{!! Form::label('end_date','End Date:') !!}
<div class="">
{!! Form::date('end_date', null, ['class' => 'form-control']) !!}
<!-- {!! $errors->first('end_date', '<p class="alert alert-danger">:message</p>') !!} -->
</div>
</div>
</div>
<div class="col-xs-1 col-sm-1 col-md-1 text-center"> <br/>
{!! Form::submit('Add Event',['class'=>'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Event Details</div>
<div class="panel-body">
{!! $calendar_details->calendar() !!}
</div>
</div>
</div>
<link rel="stylesheet" href="https://cdjns.cloudfare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.css"/>
<!-- Scripts -->
<script src="http://code.jquery.com/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.js"></script>
{!! $calendar_details->script() !!}
And this is my code for my AdminEventsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Events;
use App\Barangay;
use Illuminate\Support\Facades\Auth;
use Session;
use Calendar;
class AdminEventsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index() {
$events = Events::get();
$event_list = [];
foreach ($events as $key => $event) {
$event_list[] = Calendar::event(
$event->event_name,
true,
new \DateTime($event->start_date),
new \DateTime($event->end_date.' +1 day')
);
}
$calendar_details = Calendar::addEvents($event_list);
return view('admin.events.create', compact('calendar_details') );
}
public function addEvent(Request $request)
{
$mybrgyid = Auth::user()->brgyid;
$mybarangay = Barangay::select('brgyname')->where('id', $mybrgyid)->get();
session(['mybarangay' => $mybarangay]);
$this->validate($request,[
'event_name' => 'required',
'start_date' => 'required',
'end_date' => 'required'
]);
//$validator = validator::make($request->all(), [
//'event_name' => 'required',
//'start_date' => 'required',
//'end_date' => 'required'
//]);
//if ($validator->fails()) {
// \session::flash('warning', 'Please enter the valid details');
// return Redirect::to('/admin/events')->withErrors($validator);
//}
$event = new Events;
$event->brgyid = Auth::user()->id;
$event->event_name = $request['event_name'];
$event->start_date = $request['start_date'];
$event->end_date = $request['end_date'];
$event->save();
return redirect('/admin/events')->with('success', 'Event Created');
}
}
Somehow I'm being confuse as to where I would create a modal for editing and deleting an event in my fullcalendar.
Your help is much appreciated.

MethodNotAllowedHttpException form error

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']) !!}

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.

Categories