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
Related
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
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.
I had developed a system that use maddhatter/laravel-fullcalendar.It works and i had no problem to display the calendar with the events.However, there is the problem that the calendar show all events include from different users.Can someone help me to solve this?
Controller
//show the events in the calendar
$events = Schedule::get();
$events->user()->id;
$event_list = [];
foreach ($events as $key => $event) {
$event_list[] = Calendar::event(
$event->event_name,
false,
new \DateTime($event->start_date),
new \DateTime($event->end_date),
null,
// Add color and link on event
[
'color' => '#05B06C',
//'url' => 'http://full-calendar.io',
]);
}
//Display Fullcalendar
$calendar_details = Calendar::addEvents($event_list)
->setOptions([ //set fullcalendar options
'firstDay'=> 1,
'editable'=> true,
'navLinks'=> true,
'selectable' => true,
'durationeditable' => true,
]);
return view('front.teacher.schedule.index', compact('calendar_details','events') );
}
Model
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Schedule extends Model
{
protected $fillable=[
'event_name','start_date','end_date'
];
}
View
{!! Form::open(array('route' =>'schedule:addEvents','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','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-sm-3">
<div class="form-group">
{!! Form::label('start_date','Start Date:')!!}
<div class="">
{!! Form::input('datetime-local','start_date',\Carbon\Carbon::now(),['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::input('datetime-local','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 cold-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">My Event Details</div>
<div class="panel-body">
{!! $calendar_details->calendar() !!}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('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() !!}
Schedule Table
Table
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']) !!}
I encountered an issue with the route as follows.
Route [forgotpassword/reset] not defined.
Login forgotpassword url
<div class="forgot-password">Forgot password?</div>
Route code
Route::group(['namespace' => 'Auth'], function() {
Route::get('/forgotpassword/reset', 'ForgotPasswordController#showLinkRequestForm');
Route::post('/forgotpassword/email', 'ForgotPasswordController#sendResetLinkEmail');
Route::get('/password/reset/{token}', 'ResetPasswordController#showResetForm');
Route::post('/password/reset', 'ResetPasswordController#reset');
});
ForgotPasswordController Code
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Base\BaseController;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
class ForgotPasswordController extends BaseController
{
use SendsPasswordResetEmails;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
public function showLinkRequestForm()
{
return view('password.forgotpassword');
}
}
I overwrite function ShowLink to return view into my forgotPassword blade.
and here's my forgotPassword blade
#extends('layout.master')
#section('content')
<div id="content" class="content">
#include('partials.breadcrumbs')
<h1 class="page-header">Change <small>password</small></h1>
#include('partials.message')
<div class="row">
<div class="col-md-12">
<div class="panel panel-inverse" data-sortable-id="ui-general-1">
<div class="panel-heading">
<h2 class="panel-title">Change Password</h2>
</div>
<div class="panel-body">
{!! Form::open(['method' => 'forgotpassword','route' => ['forgotpassword/reset']]) !!}
<div class="forgot-password-form">
#if (Session::has('message'))
<div class="col-sm-12">
<div class="alert alert-success">
{{ Session::get('message') }}
</div>
</div>
#endif
{!! csrf_field() !!}
<div class="row ct-no-margin">
<div class="col-sm-4 col-xs-12">
<div class="form-group">
<div class="{{ $errors->has('email') ? ' has-error' : '' }}">
<div class="row ct-no-margin">
<div class="col-sm-12 ct-no-padding">
<label for="">Email address</label>
</div>
<div class="col-sm-12 ct-no-padding">
<input type="email" name="email" class="form-control" required/>
</div>
#if ($errors->has('email'))
<span class="help-block error-email">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn fp-submit-btn">Submit</button>
Cancel
</div>
</div>
</div>
</form>
</div>
</div>
</section>
</div>
#endsection
Any help in this regard will be appreciated
Change your route to named route like:
Route::get('/forgotpassword/reset', [ 'as' => 'forgotpassword/reset', 'uses' => 'ForgotPasswordController#showLinkRequestForm']);
And as of your view file I found this:
{!! Form::open(['method' => 'forgotpassword','route' => ['forgotpassword/reset']]) !!}
Use 'method' => 'get'