I am developing Laravel application and I need add comment form to My each task file regarding to each project.
this is comments/form.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', $project->id) }}">
<div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
<textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
#if ($errors->has('comments'))
<span class="help-block">{{ $errors->first('comments') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Comment</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
I am going to include this form file to show.blade.php file in tasks folder in view file.
this is show.blade.php
<h2>{{ $tasks->project->project_name }}</h2>
<hr>
{{$tasks->task_name}}
<hr>
{!!$tasks->body!!}
<hr>
#include('comments.form')
commentController.php
public function postNewComment(Request $request, $id, Comment $comment)
{
$this->validate($request, [
'comments' => 'required|min:5',
]);
$comment->comments = $request->input('comments');
$comment->project_id = $id;
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect()->back()->with('info', 'Comment posted successfully');
}
routes.php
Route::post('projects/{projects}/comments', [
'uses' => 'CommentsController#postNewComment',
'as' => 'projects.comments.create',
'middleware' => ['auth']
]);
but finally got this error massage
Undefined variable: project (View: C:\Users\Nalaka\Desktop\acxian\resources\views\comments\form.blade.php)
how can fix this problem?
You have not defined $project anywhere but you have $tasks from which you are getting project name already in show.blade.php so if you have project id also there in $tasks->project data then you can use this variable in view change form tag in comments/form.blade.php as below:
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', $tasks->project->id) }}">
I'm trying to make login form in Laravel 4.2 + Sentry . The problem is that when I submit the form I got the error that method is not allowed.
When I check my form in source it has method="POST" and also in the route I've wrote post. What can be the problem?
MethodNotAllowedHttpException
but can't see why? This is the form
{{ Form::open(array('route' => 'check-auth')) }}
<div class="body bg-gray">
{{-- Display flash message --}}
#if (Session::has('flash_message'))
<span style="margin-left:18%; color: #ff0000">{{ Session::get('flash_message') }}</span>
#endif
<div class="form-group">
<input type="text" name="email" class="form-control" placeholder="User email"/>
#if($errors->has('login_errors')) <span class="has-error">{{ $errors->first('email') }}</span> #endif
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="User password"/>
</div>
<button type="submit" name="submitbtn" class="btn bg-olive btn-block">Sign me in</button>
</div>
{{ Form::close() }}
Route
Route::post('user-login', ['as'=>'check-auth', 'uses'=>'AuthenticationController#login']);
and controller
public function login()
{
try{
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
$user = Sentry::authenticate($credentials, false);
if($user){
return Redirect::to('dashboard');
}
return Redirect::to('/')->with('title','Login errors');
}
catch(Exception $e){
echo $e->getMessage();
Session::flash('flash_message', 'No access!');
return Redirect::to('/')->with('title','Login errors');
}
}
UPDATE: Error
production.ERROR: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in /var/www/html/time/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210
You're route is correct, the only thing I could suggest would be to append the type to the opening of the form:
{{ Form::open(['url' => 'check-auth', 'method' => 'post']) }}
{{ Form::open(array('route' => 'check-auth')) }}
See, You are using route check-auth and in routes file you defined a different route i.e user-login
Route::post('user-login', ['as'=>'check-auth', 'uses'=>'AuthenticationController#login']);
Correct route and try again this will work
Instead of post use get (it can still post things but your are able to retrieve too)
I am trying to pass a form through. I am using request method to get variables. here is my blade of a form:
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
Routes involved:
Route::get('/admin/gallery', 'GalleryController#manageGallery')->name('manageGallery');
Route::post('/admin/gallery', 'GalleryController#postPhoto')->name('postPhoto');
And this is my controller for it:
class GalleryController extends Controller
{
public function manageGallery() {
return view('home.manageGallery');
}
public function postPhoto(Request $request) {
die("works");
}
}
It does not throw error at me. It just does nothing. So my question is: am I using this method wrong or do I need something more? Thanks in advance.
Firstly make sure that the form you are using is using the correct method for your route
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}" method="post">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
In your controller, put the following in the postPhoto function
public function postPhoto(Request $request)
{
dd($request);
}
You should now get a Request object output to the screen when you submit the form
You may wanna use Blade Forms in order to make Forms in a more natural way for Laravel
{{ Form::open(['route' => '/admin/gallery', 'method' => 'post', 'files' => true]) }}
{{ Form::text('title') }}
{{ Form::label('title', 'Name :') }}
{{ Form::file('file') }}
{{ Form::label('file', 'File :') }}
{{ Form::submit('Add') }}
{{ Form::close() }}
It reduces the overhead of adding the token by yourself as it is automatically added when using the Form facade.
And then, in your controller, you would do something like that to debug when sending the form :
<?php
use Request; /* do not forget this line */
class GalleryController extends Controller
{
public function postPhoto(Request $request)
{
dd($request->toArray());
}
}
My Laravel blog project the get method of postcontroller is working but post method is not working. I redirect The Post Method. But Accordind to my my code it should return to my Admin page.My Controller Code is
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Category;
class PostController extends Controller
{
public function getBlogIndex() {
return view('frontend.blog.index');
}
public function getSinglePost($post_id,$end='frontend') {
return view($end . '.blog.single');
}
public function getCreatePost() {
return view('admin.blog.create_post');
}
public function postCreatePost(Request $request ) {
$this->validate($request, [
'title' => 'required|max:120|unique:posts',
'author' => 'required|max:80',
'body' => 'required'
]);
$post = new Post();
$post->title = $request['title'];
$post->author = $request['author'];
$post->body = $request['body'];
$post->save();
return redirect()->route('admin.index')->with(['success' => 'Post Successfully Created']);
}
}
My Routes file
<?php
Route::group(['middleware' => ['web']], function () {
Route::group([
'prefix' =>'/admin'
], function() {
Route::get('/', [
'uses' => 'AdminController#getIndex',
'as' => 'admin.index'
]);
Route::get('/blog/posts/create', [
'uses' => 'PostController#getCreatePost',
'as' => 'admin.blog.create_post'
]);
Route::get('/blog/post/create', [
'uses' => 'PostController#postCreatePost',
'as' => 'admin.blog.post.create'
]);
});
});
My form is
#extends('layouts.admin-master')
#section('styles')
{!! Html::style('src/css/form.css') !!}
#endsection
#section('content')
<div class="container">
#include('includes.info-box')
<form action="{{ route('admin.blog.post.create') }}" method="post">
<div class="input-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" {{ $errors->has('title') ? 'class=has-error' : '' }} value="{{ Request::old('title') }}">
</div>
<div class="input-group">
<label for="author">Author</label>
<input type="text" name="author" id="author" {{ $errors->has('author') ? 'class=has-error' : '' }} value="{{ Request::old('author') }}">
</div>
<div class="input-group">
<label for="category_select">Add Category</label>
<select name="category_select" id="category_select">
<option value="Dummy Category ID">Dummy Category</option>
</select>
<button type="button" class="btn">Add Category</button>
<div class="added-categories">
<ul></ul>
</div>
<input type="hidden" name="categories" id="categories">
</div>
<div class="input-group">
<label for="body">Body</label>
<textarea name="body" id="body" rows="12" {{ $errors->has('body') ? 'class=has-error' : '' }} >{{ Request::old('body') }}</textarea>
</div>
<button type="submit" class="btn">Create Post</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
#endsection
#section('scripts')
{!! Html::script('src/js/posts.js') !!}
#endsection
When I submit My post I shows this
I dont find where the problem is. Plz Help Me
This is an extremely common error and a good one to look out for. Whenever you see:
MethodNotAllowedHttpException in RouteCollection.php
The very first thing you should check is your routes file to make sure you have Route::get or Route::post correctly based on what you are trying to do.
Your issue is that your form sends the data as POST, but your route is GET.
<form action="{{ route('admin.blog.post.create') }}" method="post">
and
Route::get('/blog/post/create', [
'uses' => 'PostController#postCreatePost',
'as' => 'admin.blog.post.create'
]);
Change that to Route::post for it to function correctly.
I have a link
<a class="trashButton" href="{{ URL::route('user.destroy',$members['id'][$i]) }}" style="cursor: pointer;"><i class="fa fa-trash-o"></i></a>
this link is supposed to direct to the destroy method of the Usercontroller , this is my route Route::resource('/user', 'BackEnd\UsersController');
UserController is a Resource Controller. But at this moment it is directing me to the show method rather than directing to the destroy method
You need to send a DELETE request instead of a GET request. You can't do that with a link, so you have to use an AJAX request or a form.
Here is the generic form method:
<form action="{{ URL::route('user.destroy', $members['id'][$i]) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button>Delete User</button>
</form>
If you're using Laravel 5.1 or later then you can use Laravel's built-in helpers to shorten your code:
<form action="{{ route('user.destroy', $members['id'][$i]) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button>Delete User</button>
</form>
If you're using Laravel 5.6 or later then you can use the new Blade directives to shorten your code even further:
<form action="{{ route('user.destroy', $members['id'][$i]) }}" method="POST">
#method('DELETE')
#csrf
<button>Delete User</button>
</form>
You can read more about method spoofing in Laravel here.
This is because you are requesting the resources via GET method instead DELETE method. Look:
DELETE /photo/{photo} destroy photo.destroy
GET /photo/{photo} show photo.show
Both routes have the same URL, but the header verb identifies which to call. Looks the RESTful table. For example, via ajax you can send a DELETE request:
$.ajax({
url: '/user/4',
type: 'DELETE', // user.destroy
success: function(result) {
// Do something with the result
}
});
I use this template 'resources/views/utils/delete.blade.php'
<form action="{{ $url or Request::url() }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type='submit' class="{{ $class or 'btn btn-danger' }}" value="{{ $value or 'delete' }}">{!! $text or 'delete' !!}</button>
</form>
Called as this:
#include('utils.delete',array( 'url' => URL::route('user.destroy',$id),'text' => '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> delete me'))
If you're looking to do this via a regular link instead of through AJAX or another type of form request you can set up a special route that will respond to a normal GET request:
In your routes, define this in addition to the resource:
Route::get('user/{site}/delete', ['as' => 'user.delete', 'uses' => 'UserController#destroy']);
In your view:
Delete this user
In your controller:
public function destroy(User $user)
{
$user->delete();
return redirect()->route('users.index');
}
If we need to use an anchor to trigger the destroy route, and we don't want to use ajax, we can put a form inside our link, and submit the form using the onclick attribute:
<a href="javascript:void(0);" onclick="$(this).find('form').submit();" >
<form action="{{ url('/resource/to/delete') }}" method="post">
<input type="hidden" name="_method" value="DELETE">
</form>
</a>
If you really want to visit the destroy action on delete route by HTML, then there is an approach to use HTTP Method Spoofing which means that you could visit a delete HTTP method by adding a hidden input named _method with the value of `"DELETE". Same way can be used for "PUT" and "PATCH" HTTP method.
Below is a sample for DELETE method.
<form action="/tasks/5" method="POST">
<input type="hidden" name="_method" value="DELETE">
</form>
will get the route
DELETE /tasks/{id} destroy tasks.destroy
if you use laravel collective, you can write this way in your views.
{!! Form::open(['url' => '/tasks/'.$cat->id, 'method' => 'delete']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
In case someone came here to find how to replace standard laravel form for delete, from button in it to link, you can just replace:
{!! Form::open(['method' => 'DELETE', 'route' => ['tasks.destroy', $task->id],'onsubmit' => 'return confirm("Are you sure?")', 'id'=>'himan']) !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
TO
{!! Form::open(['method' => 'DELETE', 'route' => ['tasks.destroy', $task->id],'onsubmit' => 'return confirm("Are you sure?")', 'id'=>'himan']) !!}
Delete
{!! Form::close() !!}
Just replace button with simple <a href="#"... but with onclick attribute to submit the form!
If you want to use a link, you can use a library I have created that lets people make links that behave like POST, DELETE... calls.
https://github.com/Patroklo/improved-links
GET and DELETE Both routes have the same URL, but the header verb identifies which to call.
Here are my code snippets for edit and delete. I use bootstrap modal confirmation for delete action
<div class="btn-group">
<a href="{{ route('locations.edit', $location->id) }}"
class="btn btn-default btn-sm">
<i class="fa fa-pencil"></i>
</a>
<span class="btn btn-danger btn-sm formConfirm"
data-form="#frmDelete-{{$location->id}}"
data-title="Delete Location"
data-message="Are you sure you want to delete this Location ?">
<i class="fa fa-times"></i>
</span>
<form method="POST"
style="display: none"
id="frmDelete-{{$location->id}}"
action="{{ route('locations.destroy' , $location->id) }}">
{!! csrf_field() !!}
{{ method_field('DELETE') }}
<input type="submit">
</form>
BootStrap Modal
<div class="modal fade" id="formConfirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
class="sr-only">Close</span></button>
<h4 class="modal-title" id="frm_title">Delete</h4>
</div>
<div class="modal-body" id="frm_body"></div>
<div class="modal-footer">
<button style='margin-left:10px;' type="button" class="btn btn-primary col-sm-2 pull-right"
id="frm_submit">Yes
</button>
<button type="button" class="btn btn-danger col-sm-2 pull-right" data-dismiss="modal" id="frm_cancel">
No
</button>
</div>
</div>
</div>
And Finally JS code
$('.formConfirm').on('click', function (e) {
e.preventDefault();
var el = $(this);
var title = el.attr('data-title');
var msg = el.attr('data-message');
var dataForm = el.attr('data-form');
$('#formConfirm')
.find('#frm_body').html(msg)
.end().find('#frm_title').html(title)
.end().modal('show');
$('#formConfirm').find('#frm_submit').attr('data-form', dataForm);
});
$('#formConfirm').on('click', '#frm_submit', function (e) {
var id = $(this).attr('data-form');
$(id).submit();
});
My, non-ajax version. I use it in dropdowns (bootstrap) in resource list (datatables as well). Very short and universal.
Global jQuery method:
$('.submit-previous-form').click(function (e) {
e.preventDefault();
$($(this)).prev('form').submit();
});
And then we can use everywhere something like this:
{{ Form::open(['route' => ['user.destroy', $user], 'method' => 'delete']) }} {{ Form::close() }}
<i class="icon-trash"></i> Delete him
Recommend: It's easy to integrate with confirms scripts for example swal.
you can try this: (you can pass your id)
<form action="{{ route('tasks.destroy', $dummy->id) }}" method="post">
#csrf
#method('DELETE')
<a href="#" class="btn btn-danger" title="Delete" data-toggle="tooltip" onclick="this.closest('form').submit();return false;">
<i class="bi bi-trash-fill" style="color:white"></i>
</a>
</form>
requires route like:
Route::get('/tasks/delete/{id}', 'TasksController#destroy')
->name('tasks.destroy');
your controller:
public function destroy($id)
{
$task = Task::find($id);
$task->delete();
return redirect('/home')->with('success','Task Deleted Successfully');
}
or you can try this
{!! Form::open(['method' => 'DELETE','route' => ['reports.destroy', $dummy->id],'class'=>'']) !!}
{{ Form::button('<i class="bi bi-trash-fill" style="color:white"></i>', ['type' => 'submit', 'class' => 'delete get-started-btn-two'] ) }}
{!! Form::close() !!}