Laravel form not sending any data - php

I am trying to submit form to the laravel. However, I am facing strange issue that the form is being submitted without any data. When I tried to print_r on the cotroller it displays:
Array
(
)
My form looks like this:
<form method="POST" action="{{route('amenityFront.index')}}" id="buildingByProperty">
#csrf
<select class="form-control" name="property" id="propertySelect">
#foreach($properties as $pk => $pv)
<option value="{{$pv->id}}" {{ ($pv->id == $selectedProperty)?'selected':'' }}>{{$pv->property_name}}</option>
#endforeach
</select>
<button type="submit">Submit</button>
</form>
And this is my route. Is it because of that match route? I am using this match route for first time.
Route:
Route::match(['get', 'post'],'/', ['as' => 'amenityFront.index', 'uses' => 'AmenityController#index']);
Update:
I have updated form to :
<form method="POST" action="{{route('amenityFront.postIndex')}}" id="buildingByProperty">
#csrf
<select class="form-control" name="property" id="propertySelect">
#foreach($properties as $pk => $pv)
<option value="{{$pv->id}}" {{ ($pv->id == $selectedProperty)?'selected':'' }}>{{$pv->property_name}}</option>
#endforeach
</select>
<button type="submit">Submit</button>
</form>
and updated route to:
Route::get('/', ['as' => 'amenityFront.index', 'uses' => 'AmenityController#index']);
Route::post('/', ['as' => 'amenityFront.postIndex', 'uses' => 'AmenityController#postIndex']);
However, instead of redirecting to postIndex I am still being posted to #index
My route:list

Related

Route [admin.distroyDish] not defined. (View: /home/abdalrazag/MyResutrant/resources/views/admin/manager.blade.php)

I am using Laravel 7 to build Restaurant system
I made form that go to specific route
and I get name route but it is display error as the title
manager view
<form method="post" action="{{route('admin.distroyDish')}}" >
#csrf
<div class="form-group">
<select class="form-control" name="dish">
#for ($i = 0 ; $i < count($InetialData['dish']); $i++))
<option value="{{ $InetialData['dish'][$i]->id }}">
{{ $InetialData['dish'][$i]->name }}
</option>
#endfor
</select>
</div>
<input type="submit" name="delete_dish" value="Delete" class="btn btn-danger">
<br>
</form>
web Route file
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/manager', 'ManagerController#dashboard');
Route::post('/manager', 'ManagerController#addItem');
Route::get('/manager/{id}', 'ManagerController#deleteTable')->name('admin.deleteTable');
Route::post('/manager', 'ManagerController#destroyDish')->name('admin.distroyDish');
Route::post('/manager', 'ManagerController#addIngrediant')->name('admin.addIngrediant');
Route::get('/kitchen', 'KitchenController#index')->name('kitchen.home');
Route::get('/kitchen/{id}', 'KitchenController#submitDish')->name('kitchen.submit.dish');
Route::post('/kitchen', 'KitchenController#addIngrediant')->name('kitchen.addIngrediant');
ManagerController
private function initData()
{
$InetialData = array(
'category' => DB::table('Category')->get(),
'dish' => DB::table('Items')->get(),
'users' => DB::table('users')->get(),
'Ingrediant' => DB::table('Ingrediant')->get(),
'IngrediantHistory' => DB::table('IngrediantHistory')->get()
);
return $InetialData;
}
public function destroyDish(Request $request)
{
DB::table('Items')
->where('id', '=', $request->dish)
->delete();
return redirect('/manager')->with('InetialData' , $this->initData());
}
other route like kitchen is working
why this route is not working ??
You've got duplicate routes:
Route::post('/manager', 'ManagerController#addItem');
Route::post('/manager', 'ManagerController#destroyDish')->name('admin.distroyDish');
Route::post('/manager', 'ManagerController#addIngrediant')->name('admin.addIngrediant');
Change the endpoint structure.
As I can see you have more routes calling POST /manager. Try to use routes like resources:
GET /entities list
GET /entities/{entity} view
POST /entities create
PUT /entities/{entity} update
DELETE /entities/{entity} delete
Elegant and easy to mantain and read.

Parameter goes into array as the name of the field instead of the value

What am I trying to achieve?
I have "tasks" and each task can have multiple "notes", so when you select a Task and click notes, it takes you to a page with all the notes for the task in which you clicked.
Each note has a field called "task_id", so my problem is passing this task_id to the note.
I'm trying to pass it like this on the notes form:
<form method="POST" action="{{route('notes.store',$task)}}">
#include('notes.form')
</form>
And it goes into my controller
public function store(Request $r)
{
$validatedData = $r->validate([
'note' => 'required',
]);
$r['created_by'] = Auth::user()->user_id;
return $r;
/*
$note = Note::create($r->all());
return redirect('/notes')->with('store');
*/
}
But I return it to see how its going and I get this:
{"_token":"OmGrbYeQDl35oRnmewrVraCT0SHMC16wE4gD56nl","note":"363","created_by":4,"8":null}
That 8 at the end is actually the correct task id, but it appears as the name instead of the value.
What may be causing this?
This is my form view:
#csrf
<div class="col">
<div class="form-group">
<input type="text" class="form-control" name="note">
</div>
</div>
<div class="col-10">
<div class="form-group">
<button class="btn btn-success" type="submit">Add note</button>
<br><br>
</div>
</div>
These are my routes:
Route::get('/tasks/{task}/notes', ['as' => 'tasks.notes', 'uses' => 'NoteController#index']);
Route::get('/projects/{project}/tasks', ['as' => 'projects.tasks', 'uses' => 'ProjectController#seeTasks']);
Route::get('/projects/results','ProjectController#filter');
Route::get('/tasks/results','TaskController#filter');
Route::resource('projects','ProjectController');
Route::resource('clients','ClientController');
Route::resource('tasks','TaskController');
Route::resource('users','UserController');
Route::resource('notes','NoteController');
You are trying to pass the task_id as a route parameter, but your notes.store route has no route parameters.
Verb Path Action Route Name
POST /notes store notes.store
Adding the task_id as a hidden input should properly send it with the request:
<form method="POST" action="{{ route('notes.store') }}">
<input type="hidden" name="task_id" value="{{ $task->id }}">
#include('notes.form')
</form>

Laravel Exception 405 MethodNotAllowed

I'm trying to create a new "Airborne" test in my program and getting a 405 MethodNotAllowed Exception.
Routes
Route::post('/testing/{id}/airbornes/create', [
'uses' => 'AirborneController#create'
]);
Controller
public function create(Request $request, $id)
{
$airborne = new Airborne;
$newairborne = $airborne->newAirborne($request, $id);
return redirect('/testing/' . $id . '/airbornes/' . $newairborne)->with(['id' => $id, 'airborneid' => $newairborne]);
}
View
<form class="sisform" role="form" method="POST" href="{{ URL::to('AirborneController#create', $id) }}">
{{ csrf_field() }}
{!! Form::token(); !!}
<button type="submit" name="submit" value="submit" class="btn btn-success">
<i class="fas fa-plus fa-sm"></i> Create
</button>
</form>
According to my knowledge forms don't have a href attribute. I think you suppose to write Action but wrote href.
Please specify action attribute in the form that you are trying to submit.
<form method="<POST or GET>" action="<to which URL you want to submit the form>">
in your case its
<form method="POST" ></form>
And action attribute is missing. If action attribute is missing or set to ""(Empty String), the form submits to itself (the same URL).
For example, you have defined the route to display the form as
Route::get('/airbornes/show', [
'uses' => 'AirborneController#show'
'as' => 'airborne.show'
]);
and then you submit a form without action attribute. It will submit the form to same route on which it currently is and it will look for post method with the same route but you dont have a same route with a POST method. so you are getting MethodNotAllowed Exception.
Either define the same route with post method or explicitly specify your action attribute of HTML form tag.
Let's say you have a route defined as following to submit the form to
Route::post('/airbornes/create', [
'uses' => 'AirborneController#create'
'as' => 'airborne.create'
]);
So your form tag should be like
<form method="POST" action="{{ route('airborne.create') }}">
//your HTML here
</form>
MethodNotAllowedHttpException signposts that your route isn't available for the HTTP request method specified. Perhaps either because it isn’t defined correctly, or it has a conflict with another similarly named route.
Named Routes
Consider using named routes to allow for the convenient generation of URLs or redirects. They can generally be much easier to maintain.
Route::post('/airborne/create/testing/{id}', [
'as' => 'airborne.create',
'uses' => 'AirborneController#create'
]);
Laravel Collective
Use Laravel Collective's Form:open tag and remove Form::token()
{!! Form::open(['route' => ['airborne.create', $id], 'method' =>'post']) !!}
<button type="submit" name="submit" value="submit" class="btn btn-success">
<i class="fas fa-plus fa-sm"></i> Create
</button>
{!! Form::close() !!}
dd() Helper Function
The dd function dumps the given variables and ends execution of the script. Double-check your Airborne class is returning the object or id you expect.
dd($newairborne)
List available routes
Always make sure your defined routes, views, and actions match up.
php artisan route:list --sort name
First of All
Form don't have href attribute, it has "action"
<form class="sisform" role="form" method="POST" action="{{ URL::to('AirborneController#create', $id) }}">
Secondly
If the above change doesn't work, you can make some changes like:
1. Route
Give your route a name as:
Route::post('/testing/{id}/airbornes/create', [
'uses' => 'AirborneController#create',
'as' => 'airborne.create', // <---------------
]);
2. View
Give route name with route() method in form action rather than URL::to() method:
<form class="sisform" role="form" method="POST" action="{{ route('airborne.create', $id) }}">

How to insert data to database with Laravel

I'm trying to insert my data to database from form.
My URL to create the data is web.com/siswa/create
But when I click submit system show error MethodNotAllowedHttpException.
How I can fix it? Is there anything wrong with my code?
Here is my form:
<form action="{{ url('siswa') }}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">NISN</label>
<input type="text" class="form-control" name="nisn" id="nisn" placeholder="NISN"></div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Siswa</label>
<input type="text" class="form-control" name="nama_siswa" id="nama_siswa" placeholder="Nama Siswa"> </div>
<button type="submit" class="btn btn-success btn-sm font-weight-bold">Submit</button></form>
Controller:
public function tambah()
{
return view('siswa.create');
}
public function store(Request $request)
{
$siswa = new \App\Siswa;
$siswa->nisn = $request->nisn;
$siswa->nama_siswa = $request->nama_siswa;
$siswa->tanggal_lahir = $request->tanggal_lahir;
$siswa->jenis_kelamin = $request->jenis_kelamin;
$siswa->save();
return redirect('siswa');
}
Route:
Route::get('/siswa/create', [
'uses' => 'SiswaController#tambah',
'as' => 'tambah_siswa'
]);
Route::get('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
change your store function route from get to post
Route::post('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
Use Csrf protection field in your form for the session timeout error
{{ csrf_field() }}
OR
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
OR if you are using Form builder
{!! Form::token() !!}
In Route please use post instead of get
Route::post('/siswa','SiswaController#store');
and also include {{ csrf_field() }} in form
you are using method="POST" on your form but in on your route you are using Route::get
Use Route::post for your route
In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.
Try this
Route::post('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
You are using POST method in your form and using GET in route.
try this
Route::post( '/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
] );

Two forms on same view in Laravel 4

I've tried number of solutions from other posts but still can't get it work.
I have two forms on the page(view)
{{ Form::open(array('action' => 'AdminController#shopMode')) }}
....// form fields
<button type="submit" class="btn btn-primary">Change</button>
{{ Form::close() }}
<hr/>
{{ Form::open(array('action' => 'AdminController#preferencesSubmit')) }}
....// second form fields
<button type="submit" class="btn btn-primary">Save Changes</button>
{{ Form::close() }}
Then in routes I have
Route::post('/admin/preferences', ['uses' => 'AdminController#preferencesSubmit', 'before' => 'csrf|admin']);
Route::post('/admin/preferences', ['uses' => 'AdminController#shopMode', 'before' => 'csrf|admin']);
When I hit submit button nothing change in database. Just page is refreshed and I got success message from FIRST form even if I submit second one.
Is it because url's in routes are same for both posts?
Update: First form input field:
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" <?php if ($settings['preferences_shop_mode'] == 0){ ?> checked="checked" value="1"<?php }else{ ?> value="0" <?php } ?>>
Here I check if preference is =0 to set value to 1 otherwise value = 0. In source I see that value is =1 which is correct because in database I have 0
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked="checked" value="1">
This is the controller
public function shopMode() {
$preferences = Preferences::where('preferences_id', 1)->first();
if (!$preferences) {
App::abort(404);
}
Input::merge(array_map('trim', Input::all()));
$preferences->preferences_shop_mode = Input::get('onoffswitch');
$preferences->save();
return Redirect::to('/admin/preferences')->with('message', 'Shop mode changed successfully.');
}
Any idea why isn't updated in database?
Routes are read in cascade. Since both routes have the same path, the first takes priority (an entry was found, so no further route lookup is needed).
You should split them with just different paths, for example:
Route::post('/admin/preferences/general', ['uses' => 'AdminController#preferencesSubmit', 'before' => 'csrf|admin']);
Route::post('/admin/preferences/shop', ['uses' => 'AdminController#shopMode', 'before' => 'csrf|admin']);

Categories