Form data comes null in Laravel [duplicate] - php

This question already has answers here:
Does form data still transfer if the input tag has no name?
(3 answers)
Closed 3 years ago.
i don't understand why my form data comes null, someone can help me please ?
Contorller (CartesController.php):
public function store(Request $request)
{
$carte = new Cartes();
dd(request('numero')); // null
}
Route (web.php):
Route::post('/addcartes', 'CartesController#store');
Form (addcartesview.blade.php):
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-xl-12">
<div class="card">
<div class="card-header">Ajouter une carte : </div>
<div class="card-body">
<form method="post" action="./addcartes">
{{ csrf_field() }}
<label for="numero">Numero</label>
<input type="number" class="form-control" id="numero" placeholder="Numero" >
<button type="submit" class="btn btn-primary">Add Card</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection}

Your form is wrong. it is not
<input type="number" class="form-control" id="numero" placeholder="Numero" >
It should be
<input type="number" class="form-control" id="numero" placeholder="Numero" name="numero" >
Your controller is wrong. it is not
dd(request('numero'));
It should be
dd($request->input('numero'));
If you want to print out all request for debugging you should use
dd($request->all());
Read more about Laravel request here.

add name attribute on input tag

Check your action and try dd($request->all());

Related

From validation Throws error The GET method is not supported for this route. Supported methods: POST."

i am new to laravel..Kind of stuck at this place. Tried many solutions for this but none worked yet, There are similar question but most unresolved, or proper evident solution not posted yet(from google,stackoverflow ..etc)
i have defned a custom route
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
whenever i submit the view with 'POST' method as
<form action="/ComplaintGenerate" method="POST" >
without any validation rule in my Complaintscontroller everything works fine and i can save data. but when i put validation either through Requests or direct it throws error Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
if i remove validation everything works fine. I also tried with GET method but still dint work.
A little peace of advice will be very much appreciated.
Web.route
Route::middleware(['auth'])->group(function(){
Route::post('/Complaint', 'ComplaintsController#find');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');});
My controller:
public function generate(GenerateInitialComplaintRequest $request)
{
$complaint = Complaint::find($request->complaint_id);
$complaint->update([
'complaint_date'=>$request->complaint_date,
'complaint_description'=>$request->complaint_description,
]);
return redirect(route('Complaints.index')->with('complaint', Complaint::all()));
}
my View:
<div class="container my-5">
<div class="col d-flex justify-content-center my-4">
<div class="card">
<div class="card-header">
<form action="/ComplaintGenerate" method="POST" >
#csrf
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
What is the route for displaying your form? When validation fails, Laravel makes redirection using GET method to the route it was displayed from.
I assume the form might be displayed in the find method of your ComplaintsController, and when validation fails, there's redirection to this route and that is what throws an error.
Can you also show your validation methods and what data are you trying to send through form?
i found the solution as mentioned by Ankur Mishra and Aryal,
We have to remember as mentioned by Aryal When validation fails, Laravel makes redirection using GET method to the route it was displayed from. And i displayed my form through below
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Controller method:
public function save_customer($id)
{
$complaint = Complaint::create([
'customer_id'=>$id
]);
// $complaint = Complaint::whereCustomer_id($id)->firstorfail();
return view('complaints.initial_complaint')->with('complaint', $complaint);
}
'complaints.initial_complaint' is the view which has the form which gave me the error of
The GET method is not supported for this route. Supported methods: POST. on submission
So i change POST route to GET :-
Route::middleware(['auth'])->group(function(){
//Route::resource('Complaints', 'ComplaintsController');
Route::get('/Complaint', 'ComplaintsController#find');
Route::get('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');
});
and in view i passed GET as hidden method
<form action="/ComplaintGenerate" method="POST" >
#csrf
#method('GET')
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
and now it is working me.. Just posted so if anybody could use it for future reference
you should add
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');

How to view data based on date range using laravel and mysql

Route:
Route::post('dategraph','Chatbot\TrackerController#dategraph');
Controller:
public function dategraph(Request $request)
{
$dategraph = DiraStatistics::all()->whereBetween('date_access', [$from, $to])->get();
$dates = $dategraph('date_access');
return view('AltHr.Chatbot.graph', compact('dates'));
}
View:
<form id="form-project" role="form" action="{{action('AltHr\Chatbot\TrackerController#dategraph')}}" autocomplete="off" method="POST">
{{csrf_field()}}
<!-- <canvas id="myChart" width="150" height="50"></canvas> -->
<div class="form-group-attached">
<div class="row">
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>From</label>
<input type="date" class="form-control" name="from" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>To</label>
<input type="date" class="form-control" name="to">
</div>
</div>
</div>
</div>
<button class="btn alt-btn-black btn-xs alt-btn pull-right" type="submit">Next</button>
</form>
Hi guys, so im trying to view the data from the selected dates as the code ive written. But im getting an error. Did i write it correctly? or am i missing something?
You do not have a $from variable.
You need to pull out posted variables from the request.
The method get() will return a Collection of objects. You can, for example, turn it to a flat array by plucking the column and turning it toArray()
$dategraph = DiraStatistics::whereBetween(
'date_access',
[
$request->get('from'),
$request->get('to')
]
)->get();
$dates = $dategraph->pluck('date_access')->toArray();

Laravel create custom method to get form data

How do I make custom method to get form data? I want this method same with Laravel update method with parameters request and id. I try this but get error.
In controller
public function updatePassword(Request $request, int $id) {
dd($request->all());
}
In route
Route::post('staffs/{id}/upassword', 'Admin\StaffController#updatePassword')->name('admin.staffs.upassword');
In blade file
<form method="post" accept-charset="utf-8" action="{{ action('Admin\StaffController#updatePassword', ['id' => $staff_id]) }}">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password">New Password</label>
<input class="form-control" name="password" type="password">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password_confirmation">Confirm New Password</label>
<input class="form-control" name="password_confirmation" type="password">
</div>
</div>
</div>
<input class="btn btn-primary" type="submit">
</form>
I am using Laravel 5.4.
here are some stuff to fix:
First in the tag you can set the action to :
action="route('admin.staffs.upassword', $staff_id)" since it's
easier to write and since you already gave the route a name, so why
not using it ;)
Second add {{csrf_field() }} right before your form closing tag
</form>
what error are you getting? the error is probably because you are not using {{csrf_field()}} after the form declaration, it is needed so that laravel can validate the request. if you want to get the data from the form you can use:
$request->get('inputname');

SQLSTATE[HY000]: General error: 1364 Field 'reply_text' doesn't have a default value

I have a page that shows a topic, And underneath the topic there are replies. In between these 2, there is a text field where the user can type a reply. The problem is. I get the error in the title when I try to post a reply. I used the same method on a previous project of mine and there it works just fine. How can I solve this?
Here are the files
topic.blade.php
<div class="card">
<div class="card-content">
<span class="card-title">Leave a Reply</span>
<div class="row">
<form method="POST" action="{{ route('createreply') }}">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
<input type="hidden" name="post_id" value="{{ $topic->id }}">
<div class="form-group col s12">
<textarea id="message-body textarea1" class="form-control materialize-textarea" name="reply" placeholder="Type your reply"></textarea>
</div>
<div class="col s12">
<button class="btn right blue-grey darken-4" type="submit">Reply</button>
</div>
</form>
</div>
</div>
</div>
ReplyController.php (Store method)
public function store(Request $request)
{
Reply::create($request->input());
return back();
}
Web.php
route::post('/reply/create', 'ReplyController#store')->name('createreply');
Thank you in advance!
<textarea id="message-body textarea1" class="form-control materialize-textarea" name="reply_text" placeholder="Type your reply"></textarea>
Try this. The name attribute was not reply_text as you have in db

No query results for model [App\WhatTodoModel]

I am newbie in laravel and I try to insert a data form a form having foreign key by using hide such as the code is mention below:-
<form class="form-horizontal" role="form" action="/WhatTodo/store" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="task_id" value=" {{$what->task_id}}">
<input type="hidden" name="work_id" value="{{$what->work_id}}">
<div class="form-group">
<label class="control-label col-sm-2" for="name"> Name</label>
<div class="col-sm-5">
{!!Form::select('name',$name)!!}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="work">work:</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="work" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Submit">
</div>
</div>
</form>
I have the controller with function:-
public function create($id)
{
$what=WhatTodoModel::findorFail($id);
$name=WOrk::lists('name','name');
return view('what/create',compact('what','name'));
}
You haven't really told us what your issue is or what error you're getting, but my guess given the current question is:
Assuming you're trying to implement a resource route and resourceful controller, the create method is used to show a form to create a new object, not edit an existing one. The create method does not take any parameters, therefore $id will be blank and WhatTodoModel::findorFail($id); will throw an exception.
If you want to edit an existing record, you do that using the edit action.
For creating a new record, create shows the form, store saves the record.
For editing an existing record, edit shows the form, update saves the record.

Categories