I am new in Laravel and trying to learn forms. Currently I am trying to do a file uploading form and my create page looks like this:
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
{!! Form::open(['action' => 'MovieController#create', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{{Form::label('name', 'Name')}}
<br>
{{Form::text('name')}}
</div>
<div class="form-group">
{{Form::label('description', 'Description')}}
<br>
{{Form::textarea('description')}}
</div>
<div class="form-group">
{{Form::label('release_date', 'Release Date')}}
<br>
{Form::date('release_date')}}
</div>
<div class="form-group">
{{Form::label('country', 'Country')}}
<br>
{{Form::text('country')}}
</div>
<div class="form-group">
{{Form::label('poster_name', 'Poster Image')}}
{{Form::file('poster_name')}}
</div>
<div class="form-group">
{{Form::label('file_name', 'Movie File')}}
{{Form::file('file_name')}}
</div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
</body>
</html>
As you can see I am trying to make an action of 'MovieController#create'. Now let's see MovieController file:
namespace
App\Http\Controllers;
use Illuminate\Http\Request;
use App\Movie;
class MovieController extends Controller
{
public function create(Request $request){
$this->validate($request,[
'poster_name' => 'required|image'
]);
//Handle poster upload
$imageName = $request->file('poster_name')->getClientOriginalName();
$request->file('poster_name')->storeAs('public/images',$imageName);
$videoName = $request->file('file_name')->getClientOriginalName();
$request->file('file_name')->storeAs('public/videos',$videoName);
$movie = new Movie;
$movie->name = $request->name;
$movie->description = $request->description;
$movie->release_date = $request->release_date;
$movie->country = $request->country;
$movie->poster_name = $imageName;
$movie->file_name = $videoName;
$movie->save();
$movies = Movie::all();
return view('home',['movies' => $movies]);
}
}
At the beginning everything was working but then I made a few changes in create file (only css and visual changes) and now when I try to go to that page, it gives the following error:
ErrorException
Action App\Http\Controllers\MovieController#create not defined. (View: C:\xampp\htdocs\MagicMovie\resources\views\movies\create.blade.php)
Any suggestions?
Try doing something like this,
On you form
{!! Form::open([ 'action' => route('create_movie'),
'enctype' => 'multipart/form-data', 'method' => 'POST']) !!}
or
{!! Form::open([ 'action' => url('movie/create'),
'enctype' => 'multipart/form-data' , 'method' => 'POST' ]) !!}
And on your routes (web.php)
Route::post('movie/create', ['uses' => 'MovieController#create', 'as' => 'create_movie']);
You can also check some basic Laravel routing here.
I think you can try this :
Route::post('/storeMovie', 'MovieController#create')->name('storeMovie');
in form action
{!! Form::open(['route' => 'storeMovie', 'enctype' => 'multipart/form-data']) !!}
Hope this work for you !!!
You are binding controller method to form directly, you can't do that.
You should write a Route for it.
in web.php
Route::post('/add', 'MovieController#create')->name('create_movie');
in form action
'action' => 'create_movie'
Related
I am trying to add an event on a calendar I have created, however I am getting the following error
The POST method is not supported for this route. Supported methods:
GET, HEAD
I have used the methods #csrf and {{ method_field('PUT') }} to no avail. I have also cleared route cache which did not help. Any help is much appreciated.
Routes:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function(){
Route::middleware('can:manage-users')->group(function(){
Route::resource('/users', 'UsersController', ['except' => ['show']]);
Route::resource('/courses', 'CoursesController', ['except' => ['show']]);
});
Route::middleware('can:manage-calendar')->group(function(){
Route::get('events', 'EventsController#index')->name('events.index');
Route::post('/addEvents', 'EventsController#addEvent')->name('events.add');
});
})
index.blade.php
#extends('layouts.app')
#section ('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-14">
<div class="card">
<div class="card-header">Calendar</div>
<div class="card-body">
{!! Form::open(array('route' => 'admin.events.index', 'method' => 'POST', 'files' => 'true'))!!}
{{-- {{method_field('PUT') }}
#csrf --}}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12"></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>
#Collin, I have added the image below in relation to your question
The error actually explains the problem. The method POST is not supported for the route you're using. You are trying to post to the route: admin.events.index but you actually want to post to the route events.add.
Route::post('/addEvents', 'EventsController#addEvent')->name('events.add');
{!! Form::open(array('route' => 'admin.events.add', 'method' => 'POST', 'files' => 'true'))!!}
{{-- #csrf --}}
Adding to this awnser is a possible solution for the validator exception the OP has mentioned in the comments.
The validator not found error can possibly come from the following:
When adding the the following code:
public function addEvent(Request $request)
{
$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.index')->withInput()->withErrors($validator);
Try adding:
use Illuminate\Support\Facades\Validator;
Just check your form action url route. You have to pass 'route('admin.events.add)' rather than 'route('admin.events.index')' and also dont use 'PUT' it will accept 'POST' as well.
I'm having a problem for the first time when i submit a form.
When i submit the form it doesn't go to post route and i don't know why.
my post route is that:
Route::post('/app/add-new-db', function()
{
$rules = array(
'name' => 'required|min:4',
'file' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('app/add-new-db')
->withErrors($validator);
}
else
{
$name = Input::get('name');
$fname = pathinfo(Input::file('file')->getClientOriginalName(), PATHINFO_FILENAME);
$fext = Input::file('file')->getClientOriginalExtension();
echo $fname.$fext;
//Input::file('file')->move(base_path() . '/public/dbs/', $fname.$fext);
/*
DB::connection('mysql')->insert('insert into databases (name, logotipo) values (?, ?)',
[$name, $fname.$fext]);
*/
//return Redirect::to('app/settings/');
}
});
And my html:
<div class="mdl-cell mdl-cell--12-col content">
{!! Form::open(['url'=>'app/add-new-db', 'files'=>true]) !!}
<div class="mdl-grid no-verticall">
<div class="mdl-cell mdl-cell--12-col">
<h4>Adicionar Base de Dados</h4>
<div class="divider"></div>
</div>
<div class="mdl-cell mdl-cell--6-col">
<div class="form-group">
{!! Form::label('name', 'Nome: ') !!}
{!! Form::text('name', null, ['id'=> 'name', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Logotipo:') !!}
{!! Form::file('image', ['class'=>'form-control']) !!}
#if ($errors->has('image'))
<span class="error">{{ $errors->first('image') }}</span>
#endIf
</div>
<div class="form-group">
{!! Form::submit('Adicionar', ['id'=>'add-new-db', 'class'=>'btn btn-default']) !!}
<p class="text-success"><?php echo Session::get('success'); ?></p>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
I'm loading this from from a jquery get:
function addDB()
{
$( "#settings-new-db" ).click(function(e)
{
$.get('/app/add-new-db', function(response)
{
$('.content-settings').html("");
$('.content-settings').html(response);
componentHandler.upgradeDom();
});
e.preventDefault();
});
}
When i try to submit the form i'm getting 302 found in network console from chrome.
I'm doing forms at this way and it is happens for the first time. Anyone can help me?
Thanks
Fix the name attribute for your image upload field. You refer it as image in the form but you are trying to fetch it as file in your controller.
This is my route
Route::resource('admin/reports', 'ReportController');
This is controller function
public function store(Request $request)
{
return "Thank you";
}
This is my html code
{!! Form::open([ 'url' => 'admin/reports/store', 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'dropzone', 'id' => 'reportfile' ]) !!}
{!! csrf_field() !!}
<div class="col-md-12">
<h3 style="text-align : center">Select File</h3>
</div>
<div class="col-md-12" style="text-align: center; padding: 10px">
<button type="submit" class="btn btn-primary">Upload Report</button>
</div>
{!! Form::close() !!}
When I submit the form, it show me MethodNotAllowedHttpException in RouteCollection.php line 218:
Any help is much appreciated. Thanks
Your form action should just be admin/reports.
Currently it will assume you are trying to post to the route admin/reports/{id}. That endpoint is used with GET, PUT and DELETE.
Check the docs including a table giving you routes https://laravel.com/docs/5.1/controllers#restful-resource-controllers if I were you I'd use the route helper to generate your urls for you
I'm new to laravel and trying to learn using it. I created a little project for threads. Nothing special. The thing is, the function to edit something doesn't work and I cant see why. Maybe someone of you see the mistake?
thats my Routes:
Route::get('/index', 'Test\\TestController#index');
Route::get('/add', 'Test\\TestController#add');
Route::post('/test', 'Test\\TestController#store');
Route::get('/show/{id}', 'Test\\TestController#show');
Route::get('/show/{id}/edit', ['as' => 'edit', 'uses' => 'Test\\TestController#edit']);
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController#update']);
thats the important parts of the edit method:
public function edit($id) {
$thread = Thread::query()->findOrFail($id);
return view('test.edit', [
'thread' => $thread
]);
}
public function update($id, StoreRequest $request) {
$thread = Thread::query()->findOrFail($id);
$thread->fill($request->all());
$thread->save();
return redirect(action('Test\\TestController#show', [$thread->id]));
}
}
show.blade
#extends('master')
#section('content')
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
{{ $thread->thread }}
</div>
</div>
<div class="form-body">
<div class="form-group"><br>
<ul>
{{$thread->content }}<br><br>
{{$thread->created_at->format('d.m.Y H:i:s')}}
</ul>
</div>
</div>
<div class="panel-footer">
<div class="btn btn-primary">Thread bearbeiten</div>
</div>
</div>
#stop
edit blade ( formular where the text I want to add is in the textbox and the save button )
#extends('master')
#section('content')
{!! Former::horizontal_open()->method('PUT')->action(action("Test\\TestController#update", [$thread->id])) !!}
{!! Former::populate($thread) !!}
{!! Former::text('thread')->label('Thread') !!}
{!! Former::text("content")->label("Content") !!}
{!! Former::large_primary_submit('Save!') !!}
{!! Former::close() !!}
#stop
model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
ERROR MESSAGE :
No query results for model [App\Models\Thread\Thread].
Form:
#section('form')
<div class="col-sm-4">
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', 'Enter your name', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('contact', 'Contact Number:') !!}
{!! Form::text('contact', 'Enter your contact number', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('location', 'Location:') !!}
{!! Form::select('location', $locations, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('service', 'Service Required:') !!}
{!! Form::select('service', $services, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Just go Befikr >>', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
routes.php:
<?php
Route::get('/', 'PagesController#index');
Route::post('/', 'PagesController#store');
PagesController.php
<?php namespace App\Http\Controllers;
use App\service_location;
use App\service_type;
use App\service_request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
class PagesController extends Controller {
public function index()
{
$locations = service_location::lists('location_area', 'location_id');
$services = service_type::lists('service_desc', 'service_id');
return view('home.index', compact('locations','services'));
}
public function store()
{
$input = Request::all();
return $input;
}
}
Unfortunately, the code neither seems to return $input on the screen (implying that its not executing the store() function at all), nor does it throw any error/exception.
Can anyone please let me know why the post method is not resulting in appropriate results here?
Edit 1
I attempted to directly return via the route's inline function, but even that didn't work. Hence, now I'm pretty confident that the post method is not getting triggered at all. This is what I did to verify it:
Route::post('/', function()
{
return 'Hello';
});
Well, I figured the answer to my own question here.
Turns out, that in order to be able to execute a post request, you need to explicitly deploy a PHP server as:
php -s localhost:<AnyFreePort> -t <YourRootDirectory>
Whereas when I attempted to run it the first time through XAMPP, I was attempting to run the application out of sheer laziness, directly as:
http://localhost/myproject/public
which worked fine for a get request.
Even I had same issue, but your solution wasn't helpful for me since I was using Apache server instead of built in PHP server
then I found this solution which says just enter space to form action, weird issue and solution but it worked for me...
Eg.
{!! Form::open(array('url' => ' ', 'method' => 'post')) !!}