I have a website I am currently editing tht was built with laravel. I have have a page that displays a "details of shipped package"
I added a form to page to update the current location of the shipped package on the details page.
<div class="row mb-30">
<div class="col-lg-12 mt-2">
<div class="card border--dark">
<h5 class="card-header bg--dark">#lang('Courier Location')</h5>
<div class="card-body">
<form action="{{route('....')}}" method="POST">
#csrf
<div class="modal-body">
<div class="form-group">
<label for="current_location" class="form-control-label font-weight-bold">#lang('Current Location')</label>
<input type="text" class="form-control form-control-lg" name="current_location" value="{{__($courierInfo->current_location)}}" required="">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn--primary"><i class="fa fa-fw fa-paper-plane"></i>#lang('Update')</button>
</div>
</form>
</div>
</div>
</div>
I have also added the update function in the controller
public function courierUpdate(Request $request, $id)
{
$request->validate([
'current_location' => 'required',
]);
$courierInfoUpdate =CourierInfo::findOrFail($id);
$courierInfoUpdate->current_location = $request->current_location;
$courierInfoUpdate->save();
$notify[] = ['success', 'Courier location info has been updated'];
return back()->withNotify($notify);
}
I am having problem with the laravel route to call that should be added as form action.
Declare a route on the web.php
Route::post('/courier-Update/{id}','App\Http\Controllers\YourControllerName#courierUpdate')->name('courier.Update');
and now just call this route in your form and also pass the id of that courier
like this:
route('courier.Update',$courier->id)
You can add a new route in routes/web.php
//import your controller at Beginning of the file
use App\Http\Controllers\YourController;
Route::post('update_location/{id}', [YourController::class, 'courierUpdate'])->name('updateLocation');
//or
Route::post('update_location/{id}', 'YourController#courierUpdate')->name('updateLocation');
And then in your blade view
<form action="{{ route('updateLocation', [ 'id' => $id]) }}" method="POST">
#csrf
</form>
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I've an update function for my Gamecontroller but it gets completely ignored when I call it and it just redirects to show view instead of executing the function despite calling it, am I missing something obvious?
Things I know and tried:
Every other function works in the GameController
I tried calling other functions like game.create and game.delete from that same view so I doubt it has to do with my view
I tried making the validate fail which got ignored because the function somehow doesn't get called
I tried just commenting the entire function and it did nothing didn't even give an error like it should
Checked to see if there was somehow a double function (there wasn't)
My update function in GameController class
public function update(Request $request, $id)
{
$validated = $request->validate([
'naam' => 'required|max:1',
'img' => 'required',
'formaat' => 'required',
'datum' => 'required',
'locatie' => 'required',
]);
if($validated->fails()){
return redirect()->back()->withErrors($validated);
}
DB::table('games')
->where('id', $id)
->update([
'naam' => $request->naam,
'img' => $request->img,
'formaat' => $request->formaat,
'datum' => $request->datum,
'locatie' => $request->locatie,
]);
return Redirect::to('games')
->with('success','Great! game updated successfully.');
}
My view:
<form action="{{ route('games.update',$data->id) }}" method="PUT" name="edit_games">
#csrf
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>Naam</strong>
<input type="text" name="naam" class="form-control" value= "{{ $data->naam }}" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>Pad van de afbeelding</strong>
<input type="text" name="img" class="form-control" value="{{$data->img}}" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>Formaat</strong>
<input type="text" class="form-control" name="formaat" value="{{$data->formaat}}" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>Datum</strong>
<input type="date" class="form-control" name="datum" value={{$data->datum}}/>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<strong>Locatie</strong>
<input type="text" class="form-control" name="locatie" value="{{$data->locatie}}"/>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
The route in web
Route::resource('/games', 'GameController');
HTML Forms can only have GET or POST method used. You have defined the method attribute as PUT: method="PUT"; this will end up using the GET method, which would end you up at your show route. Change your method attribute to POST then add a hidden field named _method with the desired HTTP method (PUT) in your case. (Form method spoofing)
<form method="POST" ....>
{{ method_field('PUT') }}
method_field('PUT') will end up putting a hidden input into your form:
<input type="hidden" name="_method" value="PUT">
Laravel 6.x Docs - Routing - Form Method Spoofing
unable to call post URL from form tag. When I click on the submit button it is going to a different URL name viewstudentmarks.
<form method="post" style="padding-top:30px;" action="{{ route('updatestudentmark',['id' => $stuid]) }}">
{{csrf_field()}}
<input type="hidden" name="type" value="{{$examtype}}">
<div class="row text-center">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
</div>
</div>
</div>
</div>
</div>
#foreach($extra as $detail)
<a class="btn btn-success" href='{{ url("viewstudentmarks/{$detail->class_id}") }}'>Back</a>
#endforeach
<input type="submit" class="btn btn-primary">
</form>
and web.php
Route::post('updatestudentmark/{id}','MarksRecordController#update')->name('updatestudentmark');
Route::get('viewhealthdetails/{id}','HealthDetailsController#viewstudents')->name('viewhealthdetails');
In form tag add {{ route('') }} name:
<form method="post" style="padding-top:30px;" action="{{ route('updatestudentmark',['id' => $stuid]) }}">
{{csrf_field()}}
</form>
Also, check your controller code. In the controller, Have you call the redirect method or not.
Thanks
it's much better to use name routes like:
Route::post('updatestudentmark/{id}','MarksRecordController#update')->name('test.route');
and in blade do something like this in action form:
{{route('test.route',['id' => $stuid])}}
I have some cotroller like this:
Route::get('/article/create', 'ArticlesController#create');
and here's my ArticlesController#create:
public function create(){
return view('articles.create',
[
'title'=>'Add Artikel',
'username'=>'Whatever Myname',
'status' => 'Offline'
]
);
}
when i trying to access blog.dev/article/create i got this strange errors:
"Trying to get property of non-object (View: E:\xampp\htdocs\blog\resources\views\articles\single.blade.php)"
how come i can get this kind of error when my view is pointing at articles.create but the error is at single.blade.php which it suppose for ArticlesController#view?
this is what in create.blade.php:
#extends('admin.layout')
#section('content')
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/articles">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input class="form-control" id="title" name="title" placeholder="Judul Artikel">
</div>
</div>
<div class="form-group">
<label for="content" class="col-sm-2 control-label">Content</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="content" name="content"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
#endsection
and here's in my single.blade.php :
#extends('admin.layout')
#section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
<hr>
#foreach($article->comments as $comment)
<blockquote>
<p>{{ $comment->comment }}</p>
<small>{{ $comment->created_at->diffForHumans() }}</small>
</blockquote>
#endforeach
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/article/{{ $article->id }}/comment">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="comment" name="comment"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
#endsection
and here's my router:
Route::get('/articles', 'ArticlesController#index')->name('home');
Route::post('/articles', 'ArticlesController#save');
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
Route::post('/article/{article}/comment', 'CommentsController#save');
Route::get('/register', 'RegistrationController#create');
Route::post('/register', 'RegistrationController#store');
I try to change the create function by pointing into another view, but still it showing same error and pointing at single.blade view.
I delete all code at single.blade and write 'test' text, i don't get any errors. but i'm pointing my controller for viewing into create.blade not sigle.blade
After seeing you routes the problem is in this two routes :
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
In this case Laravel will consider the create in your path blog.dev/article/create as an id parameter of the view route here => /article/{id}.
So as a solution you should simply inverse the two routes :
Route::get('/article/create', 'ArticlesController#create');
Route::get('/article/{id}', 'ArticlesController#view');
You need to find what's in the file by reading more than just that line. Maybe you're including that file in the articles.create? Maybe you're accessing a variable that doesn't exist whenever you load the page. Edit your answer with more of the error and what's inside both blades and we can pinpoint what's wrong.
Im having trouble posting my form to my database using laravel. When i click submit, it shows me the error MethodNotAllowedHttpException in RouteCollection.php line 218. My HTML code is shown below. I have defined the routes as shown below and I have also pasted my PostController which contains the store function.
<div class="blog-page blog-content-2">
<div class="row">
<div class="col-lg-9">
<div class="blog-single-content bordered blog-container">
<div class="blog-comments">
<h3 class="sbold blog-comments-title">Leave A Comment</h3>
<form method="post" action="store">
<div class="form-group">
<input name="title" type="text" placeholder="Your Name" class="form-control c-square"> </div>
<div class="form-group">
<textarea name="body" rows="8" name="message" placeholder="Write comment here ..." class="form-control c-square"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn blue uppercase btn-md sbold btn-block">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
This is my route page
Route::resource('posts', 'PostController');
This is the PostController which contains the store function which is suppose to store the data into the database.
public function store(Request $request)
{
//Validate the data
$this->Validate($request, array(
'title'=>'required|max:255',
'body'=>'required'
));
//Store the data into the database
$post = new Post;
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
//redirect to another page
return redirect()->route('posts.show', $post->id);
}
The problem is here:
<form method="post" action="store">
You should put posts here:
<form method="post" action="posts">
You can see all routes created with Route::resource() by using php artisan route:list command. Here, you need to look at URI created for posts.store route.
Also, you need to add CSRF token to your form:
<form method="post" action="posts">
{{ csrf_field() }}
<form method="post" action="store"> will send you to the path store which you do not have, your form should post to the same url, like this:
<form method="post" action=".">
use
<form method="post" action="posts">
I am making a signup page and continuously I am getting this
MethodNotAllowedHttpException in RouteCollection.php line 218:
when I try to submit data.
what it should do, is to just submit it to the database. but it's not doing it.
welcome blade:
#extends('layouts.master')
#section('title')
Welcome!!
#endsection
#section('content')
<div class="row">
<div class="col-md-6">
<h2>Sign Up</h2>
<form action="{{route('signup')}}" method="post">
<div class="form-group">
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" type="text" name="name" id="name">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
<div class="col-md-6">
<h2>Sign In</h2>
<form action="#" method="post">
<div class="form-group">
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
#endsection
User controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class UserController extends Controller
{
public function postSignUp(Request $request)
{
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']);
$user = new User();
$user -> email = $email;
$user -> name = $name;
$user -> password = $password;
$user->save();
return redirect()->back();
}
routes.php:
Route::group(['middleware' => ['web']], function(){
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup',[
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
});
Header blade:
<header>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-bsexample-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"></a>
</div>
</div><!-- /.container-fluid -->
master blade:
<!DOCTYPE html>
<html>
<head>
<title>#yield('title')</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
#include('includes.header')
<div class="container">
<div>
#yield('content')
</div>
</div>
</body>
</html>
You forgot to specify route name
route('signup')
Route::post('/signup', 'UserController#postSignUp')->name('signup');
If your route like this then action should be '/signup' like this
Route::post('/signup',[
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
This Exectiption you are facing
either because you dont have that signup route in routes.php
OR
you are doing post but you have written route::get('signup')
So you need to write route::post there.
Since you are using PATCH in your form, you have to add a method field helper to spoof the PUT HTTP verb.
Add this in your form or use post as the verb in routes and form.
{{ method_field('PUT') }}
We have following HTTP methods - get, post, put, patch etc.
When you are trying to insert a record in the database table, we need to use POST method instead of the PATCH method in HTML form.