MethodNotAllowedHttpException laravel 5.4 - laravel-5.4

try to make a simple insert but i get this error MethodNotAllowedHttpException in RouteCollection.php (line 251) at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php (line 238)
this is my form in the view
<form method="post" action="{{route('product.create')}}" class="form-horizontal" enctype="multipart/form-data">
{!! csrf_field() !!}
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label" for="name">Name</label>
<div class="col-md-9">
<input id="name" name="name" type="text" placeholder="Product name" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textarea">Description</label>
<div class="col-md-9">
<textarea class="form-control" id="textarea" name="description"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="size">Size</label>
<div class="col-md-9">
<select class="form-control" id="size">
<option selected>Choose size...</option>
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="larg">Larg</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="category_id">Category</label>
<div class="col-md-9">
<select class="form-control" id="category_id">
<option selected>Choose Categories...</option>
{{--<option value= "$categories"></option>--}}
<option value= "1"> men </option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="image">Image</label>
<div class="col-md-9">
<input id="file" name="image" class="input-file" type="file">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="submit"></label>
<div class="col-md-9">
<button id="submit" name="submit" class="btn btn-primary">Create</button>
</div>
</div>
</fieldset>
</form>
my route
Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
Route::get('/' ,function(){
return view('admin.index');
})->name('admin.index');
});
Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');
my controller
public function create()
{
$categories = Category::pluck('name','id');
return view('admin.product.create',compact('categories'));
}
public function store(Request $request)
{
$formInput = $request->except('image');
$image = $request->image;
if($image){
$imageName = $image->getClientOriginalName();
$image->move('images', $imageName);
$formInput['image']=$imageName;
}
Product::create($formInput);
return redirect()->route('admin.index');
}
any help will be appreciated

should be:
action="{{route('product.store')}}"
The route product.create expects GET header for showing a register form

i forget to add name to the form now after adding name i get alle field except image

I tried all above answers and still had this problem. So I found out that my model was requiring the "fillable" property:
protected $fillable = ['name', 'status', 'etc','etc2'];
This solved the issue and now I can post/put/delete.
Also, in my routes file instead of using one route to each action, I simply use, for example:
Route::resource('user', 'v1\UserController');

Related

Button type submit Vs Input type submit | laravel 8

I have a form to register a student.
The form submits when i use the following code
<div class="text-xs-right">
<i class="ti-save-alt"></i> <input type="submit" class="btn btn-info btn-rounded mb-5" value="Register">
</div>
But it just refreshes if i use the Button type Submit (below code)
<button type="submit" value="Submit" class="btn btn-rounded btn-primary btn-outline">
<i class="ti-save-alt"></i> Register
</button>
Can someone please help me understand what am i doing wrong here?
I tried to check if there is any jquery/Js Conflicts by commenting out the scripts. Nothing works.
I need to have the button working, as i am using fa icons to style them.
Thank in advance.
My full form code
<form class="form" method="post" action="{{ route('learner.store') }}">
#csrf
<div class="box-body">
<h4 class="box-title text-info"><i class="ti-user mr-15"></i> Personal Info</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>CPR Number</label>
<input type="text" name="cprnumber" class="form-control" placeholder="CPR Number" required data-validation-required-message="This field is required">
</div>
#error('cprnumber')
<span class="text-info">{{ $message }}</span>
#enderror
</div>
<div class="col-md-4">
<div class="form-group">
<label>First Name</label>
<input type="text" name="firstname" class="form-control" placeholder="First Name">
</div>
#error('firstname')
<span class="text-info">{{ $message }}</span>
#enderror
</div>
<div class="col-md-4">
<div class="form-group">
<label>Middle Name</label>
<input type="text" name="middlename" class="form-control" placeholder="Middle Name">
</div>
#error('middlename')
<span class="text-info">{{ $message }}</span>
#enderror
</div>
<div class="col-md-4">
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastname" class="form-control" placeholder="Last Name">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Gender</label>
<select name="learnergender" id="select" required class="form-control">
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Date of Birth</label>
<input type="date" name="learnerdob" class="form-control" required data-validation-required-message="This field is required">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Nationality</label>
<select name="learnernationality" id="select" required class="form-control">
<option value="">Select</option>
<option value="Bahraini">Bahraini</option>
<option value="Non-Bahraini">Non Bahraini</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Marital Status</label>
<select name="learnermaritalstatus" id="select" required class="form-control">
<option value="">Select</option>
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="Divorced">Divorced</option>
<option value="Widowed">Widowed</option>
</select>
</div>
</div>
</div>
<h4 class="box-title text-info"><i class="ti-save mr-15"></i> Contact Details</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>E-mail</label>
<input type="email" name="learneremail" class="form-control" placeholder="E-mail">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Mobile Number</label>
<input type="text" name="learnermobile" class="form-control" placeholder="Phone">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Building Number</label>
<input type="text" name="buildingnumber" class="form-control" placeholder="Building Number">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Road Number</label>
<input type="text" name="roadnumber" class="form-control" placeholder="Road Number">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Block Number</label>
<input type="text" name="blocknumber" class="form-control" placeholder="Block Number">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Area</label>
<input type="text" name="area" class="form-control" placeholder="Area">
</div>
</div>
</div>
<h4 class="box-title text-info"><i class="ti-save mr-15"></i> Education Details</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label>Education Level</label>
<select name="educationlevel" id="select" required class="form-control">
<option value="">Select</option>
<option value="Primary">Primary</option>
<option value="Preparatory">Preparatory</option>
<option value="Secondary">Secondary</option>
<option value="Diploma">Diploma</option>
<option value="Bachelor">Bachelor</option>
<option value="Masters/PHD">Masters/PHD</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Grade</label>
<input type="text" name="learnergrade" class="form-control" placeholder="Grade">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Graduation Date</label>
<input type="date" name="graduationdate" class="form-control" required data-validation-required-message="This field is required">
</div>
</div>
</div>
<h4 class="box-title text-info"><i class="ti-save mr-15"></i> Organisation Details</h4>
<hr class="my-15">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Organisation</label>
<input type="text" name="organisation" class="form-control" placeholder="Organisation">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Office E-mail</label>
<input type="email" name="officeemail" class="form-control" placeholder="Office E-mail">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Office Number</label>
<input type="text" name="officenumber" class="form-control" placeholder="Office Phone">
</div>
</div>
</div>
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label>Position</label>
<input type="text" name="position" class="form-control" placeholder="Position">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Department</label>
<input type="text" name="department" class="form-control" placeholder="Department">
</div>
</div>
</div>
<div class="form-group">
<label>Description</label>
<textarea rows="5" name="description" class="form-control" placeholder="Description/Remarks/Notes"></textarea>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<!-- <button type="button" class="btn btn-rounded btn-warning btn-outline mr-1">
<i class="ti-trash"></i> Cancel
</button>
<button type="submit" value="Submit" class="btn btn-rounded btn-primary btn-outline">
<i class="ti-save-alt"></i> Register
</button> -->
<div class="text-xs-right">
<input type="submit" class="btn btn-info btn-rounded mb-5" value="Register">
</div>
</div>
</form>
Store method
public function LearnerManagementStore(Request $request){
$validatedData =$request->validate([
'cprnumber' => 'required|unique:learner_management',
'firstname' => 'required',
'lastname' => 'required',
'learnerdob' => 'required',
'learneremail' => 'required',
'learnermobile' => 'required',
]);
$data = new LearnerManagement();
$data->cprnumber = $request->cprnumber;
$data->firstname = $request->firstname;
$data->middlename = $request->middlename;
$data->lastname = $request->lastname;
$data->learnergender = $request->learnergender;
$data->learnerdob = $request->learnerdob;
$data->learnernationality = $request->learnernationality;
$data->learnermaritalstatus = $request->learnermaritalstatus;
$data->learneremail = $request->learneremail;
$data->learnermobile = $request->learnermobile;
$data->buildingnumber = $request->buildingnumber;
$data->roadnumber = $request->roadnumber;
$data->blocknumber = $request->blocknumber;
$data->area = $request->area;
$data->educationlevel = $request->educationlevel;
$data->learnergrade = $request->learnergrade;
$data->graduationdate = $request->graduationdate;
$data->organisation = $request->organisation;
$data->officeemail = $request->officeemail;
$data->officenumber = $request->officenumber;
$data->position = $request->position;
$data->department = $request->department;
$data->description = $request->description;
$data->save();
$notification = array (
'message' => 'Learner Added successfully',
'alert-type' => 'success'
);
return redirect()->route('learners.viewall')->with($notification);
}
Route
// Learner Management
Route::prefix('learner')->group(function(){
Route::get('/viewall', [LearnerController::class, 'LearnerManagementViewall'])-> name('learners.viewall');
Route::get('/view/{id}', [LearnerController::class, 'LearnerManagementView'])-> name('learner.view');
Route::get('/add', [LearnerController::class, 'LearnerManagementAdd'])-> name('learner.add');
Route::post('/store', [LearnerController::class, 'LearnerManagementStore'])-> name('learner.store');
Route::get('/edit/{id}', [LearnerController::class, 'LearnerManagementEdit'])-> name('learner.edit');
Route::post('/update/{id}', [LearnerController::class, 'LearnerManagementUpdate'])-> name('learner.update');
Route::get('/delete/{id}', [LearnerController::class, 'LearnerManagementDelete'])-> name('learner.delete');
});

error when updating table (crud) in laravel

When I get ready to edit the table I get the following error:
"The POST method is not supported for this route. Supported methods:
GET, HEAD."
<?php
Route::get('/crear',[
'uses'=>'CarController#mostrar',
'as'=>'cars.create'
]
);
Route::post('/crear',[
'uses'=>'CarController#crear',
'as'=>'cars.crear'
]);
Route::get('/', 'CarController#casa' );
Route::post('cars/{id?}/editar', 'CarController#edit')->name('editarcar');
Route::post('cars/{id?}/editar', 'CarController#update');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('cars', 'CarController');
In View
#extends('layouts.app')
#section('title', 'Contact')
#section('content')
<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
<form class="form-horizontal" method="post">
#foreach ($errors->all() as $error)
<div class="alert alert-danger">{{ $error }}</div>
#endforeach
#if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
{!! csrf_field() !!}
<fieldset>
<legend>Editar </legend>
<div class="form-group">
<label for="patente" class="col-lg-label">patente</label>
<div class="col-lg-10">
<input type="text" name="patente"size="6" maxlength="6" class="form-control"required>
</div>
</div>
<div class="form-group">
<label for="marca" class="col-lg-label">marca</label>
<div class="col-lg-10">
<input type="text" name="marca" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="modelo" class="col-lg-label">modelo</label>
<div class="col-lg-10">
<input type="text" name="modelo" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="color" class="col-lg-label">color</label>
<div class="col-lg-10">
<input type="text" name="color" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancelar</button>
<button type="submit" class="ntm btn-primary">Actualizar</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
#endsection
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
namespace App\Http\Controllers;
use App\Car;
use Illuminate\Http\Request;
public function edit($id)
{
$car = car::whereid($id)->firstOrFail();
return view('edit', compact('cars'));
}
public function update(Request $request, $id)
{
$car = car::whereid($id)->firstOrFail();
$car->patente = $request->post('patente');
$car->marca = $request->post('marca');
$car->modelo = $request->post('modelo');
$car->color = $request->post('color');
$car->save();
return redirect(action('CarsController#edit', $car->id))->with('status', 'El car ' . $id . ' ha sido actualizado');
}
Update your controller > edit action with correct variable compact.
public function edit($id)
{
$car = car::whereid($id)->firstOrFail();
return view('edit', compact('car'));
}
Add a form action like {{ route("cars.update", ['car' => $car->id]) }} so your view looks like:
#extends('layouts.app')
#section('title', 'Contact')
#section('content')
<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
<form class="form-horizontal" action="{{ route("cars.update") }}" method="post">
#foreach ($errors->all() as $error)
<div class="alert alert-danger">{{ $error }}</div>
#endforeach
#if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
{!! csrf_field() !!}
<fieldset>
<legend>Editar </legend>
<div class="form-group">
<label for="patente" class="col-lg-label">patente</label>
<div class="col-lg-10">
<input type="text" name="patente"size="6" maxlength="6" class="form-control"required>
</div>
</div>
<div class="form-group">
<label for="marca" class="col-lg-label">marca</label>
<div class="col-lg-10">
<input type="text" name="marca" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="modelo" class="col-lg-label">modelo</label>
<div class="col-lg-10">
<input type="text" name="modelo" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="color" class="col-lg-label">color</label>
<div class="col-lg-10">
<input type="text" name="color" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancelar</button>
<button type="submit" class="ntm btn-primary">Actualizar</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
#endsection

data not updating on laravel 5.4?

Here is my edit.blade file.Please notice on From tag action attribute.Is this Ok?
#extends('admin_theme.master')
#section('title','Add Category')
#section('content')
#if(Auth::check())
<script>
function validselect(){
var ind=document.getElementById('my_select').selectedIndex;
if(ind==0){
alert("please select an Valid Option");
}
}
</script>
<div class="forms">
<div class=" form-grids form-grids-right">
<div class="widget-shadow " data-example-id="basic-forms">
<div class="form-title">
<h4>Ready To ADD:</h4>
</div>
<div class="form-body">
<form class="form-horizontal" action={{route('category.update',$singledata->id)}} method="put">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Category Name</label>
<div class="col-sm-9">
<input class="form-control" id="inputEmail3" placeholder="Name" type="text" value="{{$singledata->name}}" name="name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Status</label>
<div class="col-sm-9">
<Select class="form-control" id="my_select"onchange="validselect()" name="status">
<option>Select availability</option>
<option value="1" #if($singledata->status==1){{"selected"}} #endif >Active</option>
<option value="0" #if($singledata->status==0){{"selected"}} #endif>DeActive</option>
</Select>
</div>
</div>
<div class="form-group">
</div>
<div class="col-sm-offset-2"> <button type="submit" class="btn btn-default" >Update</button>
</div>
</form>
</div>
</div>
</div>
</div>
#endif
#endsection
Here is my update method in controller and here the inputted data from edit page not found in $request object.I changed the html method like as PUT or PATCH but no luck :)
public function update(Request $request, $id)
{
$allinput=$request->all();
// dd($allinput);
// dd($allinput);
$data=Category::findorfail($id);
$data->update($allinput);
return view('admin_theme.dynamic_files.category.allCategory');
}
here is my route
<?php
Route::get('/', function () {
return view('my_theme.index');
});
Auth::routes();
Route::get('/admin', 'HomeController#index');
Route::resource('category','CategoryController');
In before (doing CRUD) the store() method works fine the problem is when i update data...i am stuck on this,please help me on this.Thanks in advance
Atlast solved
adding method="POST" in form tag and also add below line after {{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
but why this line need ?dont know.If anyone make me understand i will be thankfull :)

How to get json and display it by user id

I want to display the data of the selected user in angular 2. I make a service to get the data from API
getUserById(id: string): Promise<User> {
const url ='api/user/${id}';
return this.httpClient.get(url)
.toPromise()
.then(response => response.json() as User)
.catch(this.handleError); }
and this is my controller
user: User;
constructor(private userService: UserService) {
userService.getUserById('45835708-f55a-40fb-878f-33b9c920e196')
.then(hasil => this.user = hasil)
.catch(this.handleError);
}
and here is my template
<div class="form-group">
<label class="col-sm-2 control-label">FULL NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">USER NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">PASSWORD</label>
<div class="col-sm-6">
<input class="form-control" type="password" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">DATE OF BIRTH</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
How to display the json to my template? Do I make correct service and controller?
You can choose to use template driven forms or reactive driven forms.
this.user object can be a model to that form, and when you input something into html input elements, object will update automatically:
<div class="form-group">
<label class="col-sm-2 control-label">FULL NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" [(ngModel)]="user.fullName" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">USER NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" [(ngModel)]="user.username" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">PASSWORD</label>
<div class="col-sm-6">
<input class="form-control" type="password" value="" [(ngModel)]="user.password" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">DATE OF BIRTH</label>
<div class="col-sm-6">
<input class="form-control" type="date" value="" [(ngModel)]="user.dateOfBirth" disabled>
</div>
</div>
If you want to add some validator, you should go with Reactive forms:
https://angular.io/docs/ts/latest/guide/reactive-forms.html
The principle here is to create a form group and define individual properties with their initial values and validator.
First make sure you have FormsModule imported in your module. Then... remember to use ngModel to bind the user to the form.
But, forms do not care about ngModel unless there is also a name attribute on each field:
<input class="form-control" name="fullName" [ngModel]="user.fullName">
OR
ngModelOptions in each field:
<input class="form-control" [ngModelOptions]="{standalone: true}" [ngModel]="user.fullName">
Here's a demo for you with usage of the name attribute:
Plunker

HTML form not submitted via phone

Good day, I'm wondering why my button cannot do a submit inside phone browsers. Please take alook at my form
<form class="form-horizontal" method="POST" action="#" id ='form_id'>
<div class="form-group">
<label class="col-sm-3 control-label">Tipe</label>
<div class="col-sm-5">
<select onchange = 'by_brand();' name = 'tipenya' id="tipe" class="form-control select2">
<option value="">---</option>
<option value="AR-1">Naughty</option>
<option value="AR-6">Les Femmes</option>
<option value="AR-10">Matahari</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Area</label>
<div class="col-sm-5">
<select onchange="brands();" name ='area' id="areanya" class="form-control select2">
<option value="">All</option>
<?php foreach ($area as $areanya) {?>
<option value="<?=$areanya->AreaCode;?>"><?=$areanya->Description;?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Outlet</label>
<div class="col-sm-5">
<select name='outlet' id="hsl" class="form-control select2">
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Date</label>
<div class="col-sm-5">
<input type="text" class="form-control pull-right" readonly="readonly" placeholder="Tanggal" name="date_search" id="reservation" />
</div>
</div>
<div class="box-footer pull-right">
<input type="submit" class="btn btn-submit" />
</div>
</form>
It's working fine when i use any laptop. Can anyone help me?
Here is my full html code
http://pastebin.com/KCEQzW3e
First of all try using this to test if the sumbit actually works
<form action="javascript:alert("sumbit button works!");" id='form_id'>
if this doesnt work then there is something wrong with your form.
if this works. there is something wrong with the jquery function you used.
try using this to call a function if you click submit:
<form class="form-horizontal" method="POST" action="#" id ='form_id' onsubmit="myFunction()">
I hope this helps.