laravel 5.4 MethodNotAllowedHttpException in RouteCollection.php (line 251) - laravel

I'm an amateur in laravel. I use laravel 5.4. so I want to make process delete without form binding but I have an error message like this. Please tell me how to solving this.
route:
Route::delete('test/{id}','TestController#destroy');
My Form:
<td><button type="button" class="btn"><a href="{{ action('TestController#destroy', $post['id']) }}" method="post" >Hapus</button>{{ csrf_field() }}{{ method_field('DELETE') }}
</td>
My Controller:
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect()->to('coba/test');`
}

Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.
One of the most common ways is to use a form instead to post data to your server.
Delete
{{ Form::open(['url' => 'test/'.$post->id, 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
Edit
{{ Form::open(['url' => 'coba/test/'.$post->id.'/edit', 'method' => 'POST']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }} only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding id of the post so you dont have multiple html forms in your code.

Related

LaravelCollective - Form Model Binding and custom attribute

With LaravelCollective, we can populate a form like this :
{{ Form::model($user, ['route' => ['user.update', $user->id]]) }}
If we declare an input like this (inside a view) :
{{ Form::text('name') }}
the field will be automatically filled by the attribute "$user->name".
However, if we want to add some custom parameters (like class) :
{{ Form::text('name', ['class' => 'form-control']) }}
we have the following error :
htmlspecialchars() expects parameter 1 to be string, array given,
OK so framework is waiting for the second parameter...
{{ Form::text('name', '', ['class' => 'form-control']) }}
But in that case, we lose the "auto-populate" feature.
Is there a solution to custom form fields, and keeping the auto populate functionality ?
Thanks !
Solution given by porloscerros :
{{ Form::text('name', NULL, ['class' => 'form-control']) }}

Laravel Route with two parameters

I'm trying to see why i'm getting MethodNotAllowedHttpException error with put when its working fine with post method
Route::resource('record/{id}/details', 'RecordDetailController');
this works:
{{ Form::open(array('url' => '/records/' . $record->id . '/details/', 'method' => 'post')) }}
while this:
{{ Form::open(array('url' => '/records/' . $record->id . '/details/', 'method' => 'put')) }}
is getting MethodNotAllowedHttpException
thanks for the help, i found the answer from this Laravel 5 MethodNotAllowedHttpException PUT
just put
{{ method_field('PUT') }}

Laravel : I am trying to pass a variable from view to controller but it returns null

This is My View code When i am executing the application it only returns me NULL value, basically my $office_category is not being passed, i need the office category to query the database
<div class="box-body">
{{ Form::open(['route' => 'office.index','class' => 'form-horizontal office-form']) }}
<div class="form-body">
<div class="form-group">
<div class="col-md-3">
{{ Form::select('office_category', [
null=>'Please Select',
'Software' => 'Software',
'Computer Hardware' => 'Computer Hardware',
'Survey Instruments' => 'Survey Instruments',
'Office Equipments' => 'Office Equipments'
], isset($office_category) ? $office_category : '', ['class' => 'form-control input-xlarge select2me', 'placeholder' => 'Project Type', 'id' => 'office_category'] ) }}
</div>
{{ Form::hidden('office_category', $office_category) }}
{{ Form::submit('Search Equipment',['class' => 'btn green']) }}
</div>
</div>
{{ Form::close() }}
My Controller Code: I want the Office category thats it
Class OfficeController extends BaseController{
public function index(){
$office_category = Input::get('office_category');
if($office_category=='')
$offices = Office::orderBy('office_category', 'asc')
->get();
else
$offices = Office::where('office_category','=',$office_category)
->get();
$assets = ['table','datepicker'];
$users = User::where('client_id','=','')
->orderBy('name','asc')
->lists('name','username');
return View::make('office.index',[
'office_category' => $office_category,
'offices' => $offices,
'users' => $users,
'assets' => $assets
]);
}
Where am i going wrong please help.
You have a hidden field directly after your select that has the same name as the select. The value of this hidden field (empty) is what is getting sent to the server.
Delete this line:
{{ Form::hidden('office_category', $office_category) }}
Or rename this hidden field.
By default Form::open creates a POST request and your index method on Controller are expecting a GET request.
You need to add a new route on routes.php to match this POST request.
Route::post('index', 'OfficeController#index');
Or if you don't mind, you can set index to listen any kind of request:
Route::any('index', 'OfficeController#index');
In most of the case, above answer will solve your problem. If not, you can inspect your web request from browser and confirm value in $office_category variable.

In Laravel what is the best method to save or store data in database

For usual Form like the following form I use the following technique to save data into database.
// Controller
public function store()
{
$validator = Validator::make($data = Input::all(), Person::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
Person::create($data);
return Redirect::route('admin.person.index');
}
But in my this form there is an file input field where user can upload multiple files at a time. and I want to store the files name into database i,e
image-1, image-2, image-3 like this. I am trying to use image intervention package to handle image files. But in that case what would my code to store data into database.
// Form
{{ Form::open(array('route' => array('admin.index.store'), 'method' => 'post', 'files' => true)) }}
<li>
{{ Form::label('name', 'Index Name') }}
{{ Form::text('name', null, array( 'class' => 'form-control input-sm', 'placeholder' => 'Name' )) }}
{{ $errors->first('name', '<p class="error">:message</p>' ) }}
</li>
<li>
{{ Form::label('phone_number', 'Phone Number') }}
{{ Form::text('phone_number', null, array( 'class' => 'form-control input-sm', 'placeholder' => 'Phone Number' )) }}
{{ $errors->first('phone_number', '<p class="error">:message</p>' ) }}
</li>
<li>
{{ Form::label('image', 'Profile Picture') }}
{{ Form::file('files[]', array('id' => 'files', 'multiple' => true)); }}
</li>
{{ Form::close() }}
Person::create($data); this wont fly.
You have to parse Input from forms, remove Files, process files separatelly and then update filepath with
//code to get $dataWithoutfiles;
$person = Person::create($dataWithoutFiles);
//code to save files to local
$person->file1 = 'path1';
// 2,3,4, etc.
$person->save();

laravel using twitter bootstrap and datepicker

I am trying to render a datepicker in the form created by laravel tags. So far, nothing else is showing except a regular text field.
I've used this for the datepicker. And I did the below in the form:
<p>{{ Form::text('dob', '', array('class' => 'form-control','placeholder' => 'تاريخ الميلاد', 'data-datepicker' => 'datepicker')) }}</p>
Nonetheless, I am only getting this:
What is the recommendation here? I also added the css and js to the header so all should be in place.
Thanks,
Update: calling datepicker js function in the footer.blade.php where all other calls are:
{{ HTML::script('/public/assets/js/bootstrap-typeahead.js') }}
{{ HTML::script('/public/assets/js/bootstrap-datepicker.js') }}
<script>
$('.datepicker').datepicker()
</script>
This still didn't change anything.
Use an id to initialize the datepicker
{!! Form::input('date', 'dob', date('d-m-Y'), ['class' => 'form-control', 'id' => 'dob', 'data-date-format' => "dd-mm-yyyy"]) !!}
and use this javascript
$('#dob').datepicker();

Resources