Laravel MethodNotAllowedHttpException when i try to save fields? - php

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?

Related

Old data not displayed in edit.blade.php

This is my edit method:
public function edit(User $user)
{
// ...
return view('admin.biodata.edit',
[
'title' => 'Edit Biodata',
'active' => 'biodata',
'majors' => Major::all(),
'biodata' => $user
]
);
}
edit.blade.php
#extends('admin.adminDashboard.layouts.main')
#section('container')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2"></div>
<h1>Edit Student</h1>
<div class="container">
<form action="{{url('admin/biodata')}}" method="POST">
#csrf
<input type="hidden" name="id" value="{{ $biodata->id }}">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" value="{{$biodata->name}}">
</div>
<div class="form-group">
<label for="nrp">NRP</label>
<input type="text" class="form-control" id="nrp" name="nrp" placeholder="Enter NRP" value="{{$biodata->nrp}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" value="{{$biodata->email}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="Enter address" value="{{$biodata->address}}">
</div>
<div class="form-group">
<label for="generation">Generation</label>
<input type="text" class="form-control" id="generation" name="generation" placeholder="Enter generation" value="{{$biodata->generation}}">
</div>
<div class="form-group">
<label for="major">Major</label>
<select class="form-control" id="major" name="major_id">
#foreach ($majors as $major)
<option value="{{$major->id}}">{{$major->nama_jurusan}}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
#endsection
When I press the edit button from the index view, the old data is not displayed in the view admin.biodata.edit. I thought I have already passed the user data through the biodata variable.
Replace the value in the template with
value="{{old('name',$biodata->name)}}"

Nothing happened after updating data in Laravel

There is nothing changed when I'm updating my record. Before this I don't put $id in the function but after make some research, I need to put it and also put in the route. Still, I do not have any idea know where is my mistakes.
web.php
Route::get('/administrator','AdminController#index');
Route::post('/admin/create','AdminController#create');
Route::get('/admin/{id}/edit', 'AdminController#edit');
Route::post('/admin/{id}/update', 'AdminController#update');
Route::post('/admin/delete', 'AdminController#destroy');
AdminController
public function edit($id) {
$admin = Admin::find($id);
return view('admins.edit',['admins'=>$admin]);
}
public function update(Request $request, $id) {
$admins = Admin::find($id);
$admins->name = $request->name;
$admins->branch = $request->branch;
$admins->email = $request->email;
if ($request->hasFile('avatar')){
$filename = $request->avatar->getClientOriginalName();
$request->avatar->storeAs('images/'.$request->email,$filename,'public');
$admins->avatar = $request->email.'/'.$filename;
}
else
{
$admins->avatar='default.png';
}
$admins->save();
return redirect('/administrator')->with('success','Your details are updated!');
profile.blade.php
<h4 class="heading"><b><center>Admin's Details</center></b></h4>
<ul class="list-unstyled list-justify"><center>
<li><b>ID: </b>{{$admins->id}}</li>
<li><b>Name: </b>{{$admins->name}}</li>
<li><b>Branch: </b>{{$admins->branch}}</li>
<li><b>Email: </b>{{$admins->email}}</li></center>
</ul>
<div class="text-center">Edit Profile</div>
edit.blade.php
<div class="panel-body">
<form action="/admin/{id}/update" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<div class="form-group hidden">
<label for="id">id</label>
<input type="text" class="form-control" name="id" value="{{$admins->id}}"/>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{$admins->name}}"/>
</div>
<div class="form-group">
<label for="address">Branch</label>
<input type="text" class="form-control" name="branch" value="{{$admins->branch}}" />
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" name="email" value="{{$admins->email}}"/>
</div>
<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" name="avatar" class="form-control-file" value="{{$admins->avatar}}">
</div>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
I hope someone can help me solve this problem.
add name() to route then call in view
Route::post('/admin/{id}/update', 'AdminController#update')->name('admin.update');
in blade
<form action="{{route('admin.update',$admins->id)}}" method="post" enctype="multipart/form-data">
web.php
Route:: resources ('admin', 'AdminController');
edit.blade.php
<div class="panel-body">
<form action="{{route('admin.update',$id)}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<div class="form-group hidden">
<label for="id">id</label>
<input type="text" class="form-control" name="id" value="{{$admins->id}}"/>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{$admins->name}}"/>
</div>
<div class="form-group">
<label for="address">Branch</label>
<input type="text" class="form-control" name="branch" value="{{$admins->branch}}" />
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" name="email" value="{{$admins->email}}"/>
</div>
<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" name="avatar" class="form-control-file" value="{{$admins->avatar}}">
</div>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
AdminController
public function update(Admin $admin, Request $request)
{
$admin->update([$this->requestValidate($request)]);
$this->storeImage($admin);
return redirect(route('admin.index'));
}
public function requestValidate($request)
{
return $request->validator([
'name' => 'string | required',
'branch' => 'string |required',
'email' => 'string | required | email'
]);
}
public function storeImage($request)
{
if ($request->hasFile('avatar')) {
$filename = $request->avatar-
>getClientOriginalName();
$request->avatar->storeAs('images/' . $request-
>email, $filename, 'public');
$request->avatar = $request->email . '/' . $filename;
} else {
$request->avatar = 'default.png';
}
}
Use this stardant with validate maybes it helps you...

Unable to update data with Laravel does not show any error

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>

After submitting a form i get an error "Sorry, the page you are looking for could not be found."

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>

Passing data from form to controller Laravel 5

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">

Categories