I'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so it makes me more confused of what's wrong because i really don't know why.
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit');
Route::post('contact/update','ContactController#update');
Controller with edit and update function
use Illuminate\Http\Request;
use App\Contact;
use DB;
public function edit($kode_kontak){
$contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
return view('contact.edit',['contact' => $contact]);
}
public function update(Request $request){
DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Model
class Contact extends Model
{
public $timestamps = false;
protected $table = 'contact';
protected $fillable = [
'kode_kontak',
'kode_pegawai',
'email',
'telepon'
];
protected $primaryKey = 'kode_kontak';
}
View of edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="/contact/update" method="POST">
#csrf
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Your update route is using the wrong "verb" and url. If you take a look at Laravel's Resource Controllers you can see the different actions and route names available for editing, updating, deleting etc. when creating a "CRUD" controller.
You can see the route for an "update" action and its "verb".
Change your routes to
Route::get('contact/{contact}/edit', 'ContactController#edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController#update')->name('contact.update');
Or if you want to add a complete CRUD controller use the short form:
Route::resource('contact', 'ContactController');
This will create all required routes in one convenient line of code. Use php artisan route:list to check all routes.
HTTP forms only support GET and POST methods, Laravel uses #method() in blade to add the other verbs (put,patch,delete):
Edit:
your form uses disabled attributes on some <input>s. Those values will not be sent along with your request. Here's an updated edit.blade.php:
disabled attributes have been swapped with readonly.
The action uses Laravel's RouteModelBinding
Removed the #foreach since you only have one item to be edited
edit.blade.php:
<div id="contact">
<h2>Edit Contact</h2>
<form action="{{ route('contact.update', ['contact' => $contact]) }}" method="POST">
#csrf
#method('patch')
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $contact->kode_kontak}}" readonly>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $contact->kode_pegawai}}" readonly>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $contact->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $contact->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
</div>
Since it's using RouteModelBinding you can change your update() method to:
public function update(Request $request, Contact $contact)
{
$contact->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Laravel will know what Contact $contact is
you loop your contact to produce a form for each one of them with the same input name, id,... which is the wrong approach.
my suggestion to you :
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit');
Route::post('contact/{contact}/edit', 'ContactController#update');
Controller with edit and update function
public function edit(Contact $contact){
return view('contact.edit',compact('contact'));
}
public function update(Request $request,Contact $contact){
$contact->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
and of course, you will update edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="/contact/{{ $p->id }}/update" method="POST">
#csrf
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Related
I'm new with laravel and now i making some small project. I have a form, after the submit button pressed i got this error message "Sorry, the page you are looking for could not be found."
Is there anything wrong about my code?
Please help me to fix this issue, so i can continuing the project.
Thanks in advice
view blade, i named it index.blade.php
<div class="col m7 s12">
<form method="submit" action="post">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
controller, i named it LayoutController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class LayoutController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
return view('layouts/index');
}
public function submit(Request $request)
{
$name = $req->input('name');
$email = $req->input('email');
$phone = $req->input('phone');
$message = $req->input('message');
$data = array('name'=>$name,"email"=>$email,"phone"=>$phone,"message"=>$message);
$data->save();
return Redirect::to('/layouts/index');
}
routes web.php
Route::get('/', 'LayoutController#index');
Route::post('/submit', 'LayoutController#submit');
Your form method should be POST and action should be /submit
<form method="POST" action="/submit">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
Try this :
<form method="POST" action="{{ route('submit') }}">
The Error you're getting is because the wrong <form> tag attributes
action => 'The route or page or class method that'll process the form
information'
method => 'This the URI HTTP verb used to transport information, you
can either use POST(sending data as http payload) or GET(sending data
as query string)
changing the <form> tag like this will solve your issue
<form method="POST" action="{{ url('/submit') }}">
The form method should be POST and the action will be your route:
<form method="POST" action="{{ url('/submit') }}">
<div class="col m7 s12">
<form method="POST" action="{{url('/submit')}}">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
I'm creating a laravel application that allows users to create a blog post.
I have created a PostsController as a resource with store function like this:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
return 123;
}
Also, I added a route in web.php
Route::resource('posts', 'PostsController');
If I list the routes with php artisan php artisan show:routes, POST method is listed:
The HTML form looks like this:
<form action="PostsController#store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
When I submit the form, I get MethodNotAllowedHttpException:
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I used to use laravel collective for forms before. Haven't done any work in laravel for a while and now it seems to be deprecated (https://laravelcollective.com/), so I resorted to classic HTML form. How do I work around this?
Your action is incorrect within your form - you need to point the action to the URL of the route, and then the route will select the method, in this case, the 'store' method. Also add #csrf for more information CSRF Protection
<form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
add the name in textbox and textarea
form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
I made a function other than resource and I am trying to update my data and the update page is showing error.
I am solving this error I tried to resolve but i am unable to find way to pass second variable
Edit.blade.php
#extends('home')
#section('title', '| Edit User')
#section('dashboard')
<div class='col-lg-4 col-lg-offset-4'>
<h1><i class='fa fa-user-plus'></i> Edit {{$user->name}}</h1>
<hr>
<form action="{{ route('users.updated', ['id' => $user->id]) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="name" class="col-sm-2 col-form-label">Name *</label>
<input type="text" class="form-control col-sm-10" id="name" name="name"
value="{{$user->name}}" />
</div>
<div class="form-group">
<label for="email" class="col-sm-2 col-form-label">Email *</label>
<input type="email" class="form-control col-sm-10" id="email" name="email"
value="{{$user->email}}" />
</div>
<h5><b>Give Role</b></h5>
<div class='form-group'>
#foreach ($roles as $role)
<input class="form-check-input" type="checkbox" name="roles[]" value="{{ $role->id }}">
<label class="form-check-label" for="defaultCheck1">
{{$role->name}}
</label>
#endforeach
</div>
<div class="form-group">
<label for="password" class="col-sm-2 col-form-label">Password *</label>
<input type="password" class="form-control col-sm-10" id="password" name="password"
value="{{$user->password}}" />
</div>
<div class="form-group row" style="margin:5%;">
<button type="submit" class="btn btn-primary col-sm-3 col-sm-offset-3">Edit User</button>
</div>
</form>
</div>
#endsection
Web.php
Route::get('users/{id}/change' ,'UserController#admin_side_edit');
Route::post('users/savechanges/{id}', [
'as' => 'users.updated',
'uses' => 'UserController#admin_side_update'
]);
This is causing me the error if someone would please help me.
After changes
Actually you don't pass the id to the action, in the blade.
For you action in your form you can use this :
{{ route('users.updated', ['id' => $user->id]) }}
And don't forget to add params in the route
Route::post('users/savechanges/{id}', [
'as' => 'users.updated',
'uses' => 'UserController#admin_side_update'
]);
Hope this can help you.
Hi i try to save fields in db ot 2 steps but i have MethodNotAllowedHttpException
on step 1 in my controller i save the 2 fields in db
public function storeNumber(Request $request){
$number = new Number;
$number->user_id = $user = \Auth::user()->id;
$number->number = $this->getGeneratedNumber();
$number = new Number($request->all());
$number->save();
return redirect('numbers/{id}/details');
}
view
<form class="form-horizontal" method="POST" action="{{action('NumberController#storeNumber')}}">
{{ csrf_field() }}
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-primary btn-block">
Generate Numbers
</button>
</div>
</div>
model
class Number extends Model
{
/**
* #var array
*
*/
protected $fillable = [
'number'
];
}
on step 2 i want to save another field in same db with same controller this is my another store function for store another fields in same db . but when i try to save laravel say MethodNotAllowedHttpException.
public function store(Request $request, $id){
$number = Number::find($id);
$number = new Number($request->all());
$number->save();
return redirect('numbers');
}
this is my view
<form method="post" action="{{action('NumberController#store', $id)}}">
{{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
<div class="form-group">
<label for="number" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Number" name="number" value="{{$number->number}}">
</div>
<div class="form-group">
<label for="comment" class="control-label">Comment</label>
<textarea name="comment" class="form-control form-control-lg" cols="30" rows="10">{{$number->comment}}</textarea>
</div>
<div class="form-group">
<label for="accept" class="control-label">Accept</label>
<input type="radio" name="accept" value="1">Yes<br>
<input type="radio" name="accept" value="0">No<br>
</div>
#if($number->accept == 1)
<div class="form-group">
<label for="name" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Name" name="name" value="{{$number->name}}">
</div>
<div class="form-group">
<label for="city" class="control-label">City</label>
<input type="text" class="form-control form-control-lg" placeholder="City" name="city" value="{{$number->city}}">
</div>
<div class="form-group">
<label for="postcode" class="control-label">Postcode</label>
<input type="number" class="form-control form-control-lg" placeholder="Postcode" name="postcode" value="{{$number->postcode}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea name="address" class="form-control form-control-lg" cols="30" rows="10">{{$number->address}}</textarea>
</div>
#else
<p>TODO status for NO</p>
#endif
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-default">Finish</button>
</div>
</div>
</form>
You have a "_method" filed with "PATCH" value, so you have to change the route to "patch" instead of "post".
Please change blade content of update as follow
<form method="post" action="{{action('NumberController#store', $id)}}">
to
<form method="post" action="{{action('NumberController#update', $id)}}">
return redirect('numbers/{id}/details');
Here is incorrect, what is id?
I'm trying register users in a laravel 5 application using the restful controller.
The problem is that when I dump the data in my store function, I only get the csrf token, but not the values.
Here's what I tried so far:
Input::all();
Request::get();
Here's the code I'm executing:
Form
<form class="form-horizontal" method="post" action="/users">
<div class="form-group">
<label for="name" class="col-lg-2 control-label">
Name
</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name" value="a">
</div>
</div>
<div class="form-group">
<label for="email" class="col-lg-2 control-label">
Email
</label>
<div class="col-lg-10">
<input type="email" class="form-control" id="email" value="a#a.Fr">
</div>
</div>
<div class="form-group">
<label for="password" class="col-lg-2 control-label">
Pw
</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="password">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Controller
public function store()
{
$input = Input::only('name','email','password');
$user = new User;
$user->name = $input['name'];
$user->email = $input['email'];
$user->password = Hash::make($input['password']);
$user->save();
}
And Route is just
Route::resource('users','UsersController');
Your input fields are all missing a name attribute! For example
<input type="text" class="form-control" id="name" name="name" value="a">
<!-- ^^^^^^^^^^^ -->
You should put name attributes on the input fields, because html forms communicate with php with name attribute.
Example:
<input........... name="etc">
and in controller use the name you have given an input as a key :
$user->name = $input['etc'];
<input type="text" class="form-control" id="name" name="name" value="a">