Laravel 5.2 : Getting form fields in controller method - php

I have the following form :
{!! Form::open(['action' => 'PublicController#showProfile','method' => 'post', 'class' => 'form-horizontal']) !!}
{!! Form::token(); !!}
<input type="text" class="form-control" name="from">
<span class="input-group-addon"> to </span>
<input type="text" class="form-control" name="to">
<button type="submit" class="btn blue">Query</button>
{!! Form::close() !!}
In my controller I am getting the form fields like this :
public function showProfile(Request $request)
{
$to = $request->get("to");
$from = $request->get("from");
$giftReceived = App\GiftSent::whereBetween('created_at', [$from, $to])->get();
dd($from);
return view('user.profile',compact('giftReceived'));
}
In the above code dd($from) comes null
Am i missing something ?

First of all, as stated in your comments, you are using Route::get and submitting the form as POST which is obviously wrong. So, you should probably get MethodNotAllowedException
So, kindly change the line to
{!! Form::open(['action' => 'PublicController#showProfile','method' => 'GET', 'class' => 'form-horizontal']) !!}
In any case, instead of using Laravel's Request Class, prefer using the helper function request()
So, basically, it should be like
public function showProfile() {
$from = request()->get('from');
$to = request()->get('to');
dd($from, $to);
}
Everything should work fine now :)

If your post data is sending to controller, you can access the values like:
// Get all request data via method POST.
$requestData = Request::instance()->request->all();
dd($requestData['from']);
dd($requestData['to']);

Related

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'
] );

405 method not allowed error coming while posting data from form in Laravel 5.4 backpack Admin

I am making simple form of adding news in Laravel 5.4 backpack admin just to have overview of Laravel 5.4 but got stuck while posting data from News form located at news/add view. Though I am sending action to News Controller at add method but it is showing 405 method not allowed error. Please check my code below and let me know what is the issue in it. Might be I am doing some silly mistake, sorry if that is the case.
View : add.blade.php
{!! Form::open(['action' => 'NewsController#add']) !!}
<div class="form-group">
<label for="title">Title:</label>
<input name="title" id="title" type="text" class="form-control" required>
</div>
<div class="form-group">
<label for="description">Description:</label>
<textarea name="description" id="description" class="form-control">
</textarea>
</div>
<button class="btn btn-default" type="submit" name="submitBtn"
value="Submit">Submit</button>
{!! Form::close() !!}
Controller : NewsController.php
public function add(){
echo "<pre>"; print_r($this->data->request); die;
return view("news.add");
}
Firstly add this in top in your Controller:-
use Illuminate\Http\Request;
use App\Http\Requests;
After that your function should have this parmeter Request $request:-
public function add(Request $request){
$data = $request->all();
return view("news.add");
}
Hope it helps!
It seems like method in the route file is not POST.
Change your route like this :
Route::post('/addnews',['as' => 'news.add', 'uses'=>'NewsController#add']);
I would recommend you to use named route. It will be easy to use.
{!! Form::open(['route' => 'news.add']) !!}
Use this format:
{!! Form::open(array('url' => 'add')) !!}
// your form fields
{!! Form::close() !!}
Your route will be:
Route::post('/add','NewsController#add');
Hope it helps..

How to put input textarea in an array? (Laravel 5.3)

My view blade is like this :
#foreach($reviews as $key => $review)
...
<div class="form-group">
{!! Form::open(['route' => 'message.review.update', 'method' => 'post', 'id' => 'reviewform']) !!}
<label for="review" class="sr-only">Review</label>
{!! Form::textarea('review', null, ['class' => 'form-control', 'id' => 'review', 'rows'=>3,'required'=>'required']) !!}
#if ($errors->has('review'))
<span class="help-block">
<strong>{{ $errors->first('review') }}</strong>
</span>
#endif
{!! Form::hidden('id', $review->_id) !!}
{!! Form::hidden('store', $review->store_id) !!}
{!! Form::close() !!}
</div>
...
#endforeach
My routes is like this :
Route::group(['prefix' => 'message','as'=>'message.'],function(){
Route::post('review/update', ['as'=>'review.update','uses'=>'ReviewController#update']);
});
My controller to update is like this :
public function update(CreateReviewRequest $request)
{
$param = $request->only('id', 'review', 'store');
...
}
Before update, it will call CreateReviewRequest to validation
My CreateReviewRequest is like this :
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateReviewRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'review'=>'required|max:300'
];
}
}
When, only one data, I input comment and submit, it works
But, when more than one data, it does not works
There exist error like this :
An invalid form control with name='review' is not focusable.
How can I solve it?
You made some mistakes here... In HTML IDs MUST be unique. You can't put "review" as id for all your textareas... My comment was about to use an array as name like this name=review[], and in the ID add dynamically the $key at the end of the id to make them unique.
This will give you something like this :
<textarea name="review[]" class="form-control" id="review0" rows="3" required></textarea>
<textarea name="review[]" class="form-control" id="review1" rows="3" required></textarea>
<textarea name="review[]" class="form-control" id="review2" rows="3" required></textarea>
EDIT
I've just founded this stackoverflow topic. Could you try to add novalidate attribute in the <form>?
<form name="myform" novalidate>
Something like this in your case :
{!! Form::open(['route' => 'message.review.update', 'method' => 'post', 'id' => 'reviewform', 'novalidate']) !!}
VIEW
{!! Form::textarea('review[]', null, ['class' => 'form-control', 'id' => 'review', 'rows'=>3,'required'=>'required']) !!}
or
<textarea name="review[]" class="form-control" id="review" rows="3" required></textarea>
You must insert name field like array empty, when you will submit the form you can get all values in your controller calling:
CONTROLLER
public function update(Request $request)
{
// Validate the form
$this->validate($request, [
'review' => 'required',
]);
// get inputs
$review_input = $request->get('review');
dd($review_input); // see if it work
}

Check if Form method is post or patch in Laravel

I have an edit view and i am using a partial _form view.
Is there a way to check if the form is a patch or post?
What i plan to do is to change the hidden field in edit form
#if (form is post)
{!! Form::hidden('signature') !!}
#else
<div class="form-group">
{!! Form::label('signature', 'Signature: ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('signature', null, ['class' => 'col-md-2 form-control', 'required']) !!}
</div>
</div>
#endif
because this variable is already saved to DB and i want to load it for edit.
Or to check if form is post, that would work also!
I usually pass the variable to a view where I set action, like:
$action = 'store';
Then I use this variable to build route name:
{!! Form::open(['route' => 'post'.$action, ....
And detect what type of action is needed:
#if ($action == 'store')
I guess it's the most readable and simple way to achieve what you're trying to achieve. You can do something similar.
Try this:
$isPut= Request::isMethod('put');
if($isPut) {
//
}

Laravel 5 - inserting old data into form via variable

I have a slight problem. I have a system whereby I can drag and drop my own forms. The html code for a form is saved in my database. When it comes to the edit page, I do something like the following
{!! Form::model($project->document, [
'class'=>'form-horizontal',
'method' => 'PATCH',
'route' => ['projects.documents.update', $project, $document->id]
]) !!}
{!! $documentData->documentData !!}
<div class="form-group">
{!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
$documentData->documentData contains the html code for this particular form.
Now my problem is, $documentData->form_data contains the old inputs for this form.
Is there any way to get this old input into the form, the way I am currently handling things?
Thanks
in controller you can access old input by $request->flash(); while in frontend you can access old by input type="text" name="name" value="{{ $name }}"

Categories