Changing laravel form to html form - laravel

Hi i created a form with laravel form helpers but i want to change it to a standard html form. The issue i am having is with the "PUT" function, when i try to edit my posts no data is displayed so i think my form properties are wrong.
Form header
<form method="post" action="{{route('posts.update',[$post->id])}}" enctype="multipart/form-data">
{{csrf_field()}}
{{method_field('put')}}
<input type=""text" name="name" class="name">
<input type=""text" name="body" class="body">
<button></button>
</form>
(--UPDATED--)
Laravel Form
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, ["class" => 'form-control input-lg']) }}
{ Form::label('body', 'Body:') }}
{{ Form::text('body', null, ["class" => 'form-control input-lg']) }}
{{ Form::submit('Save Changes', array('class' => 'btn btn-success btn-block')) }}
{{ Form::close() }}
I want the blog data to be displayed in the form for me to edit.. When i use form helpers it works fine
Any help will be much appreciated
Thanks
Ash

Ash,
I don't see the Laravel form code here to compare the two, but the easiest way to convert the Laravel form to straight HTML is to view source on the generated form and copy the generated HTML code back into the blade. Then you can replace populated data with variables.
(--UPDATED--)
Hence, put this back into your blade:
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, ["class" => 'form-control input-lg']) }}
{ Form::label('body', 'Body:') }}
{{ Form::text('body', null, ["class" => 'form-control input-lg']) }}
{{ Form::submit('Save Changes', array('class' => 'btn btn-success btn-block')) }}
{{ Form::close() }}
and then copy the HTML output for the "put" action. Or, if you're on the current version of Laravel, you can simply use the put method as follows:
#method('PUT')
(see https://laravel.com/docs/5.8/blade)
(--ORIGINAL--)
Also, you didn't mention what version of Laravel you're on. The syntax for inserting fields into the blade varies.
(--UPDATED--)
#kapitan, you're right. I "learned" that from using LaraShift telling me that all my {{ $field }} needed to be changed to {!! $field !!} to upgrade my app. The syntax has changed as follows.
In older versions of Laravel, variables inserted with {{ $field }} were UNescaped, and variables inserted with {{{ $field }}} were escaped.
In newer versions of Laravel, variables inserted with {{ $field }} are escaped, and variables inserted with {!! $field !!} are UNescaped. So the meaning of {{ $field }} has reversed from older versions to newer versions.

Related

Laravel form model attributes

How to pass attributes like id, class to form model?
This what i try not working and in official Laravel documentation wass not defined.
#if(isset($country))
{{ Form::model($country, ['route' => ['country.show', $country->id]], ['class' => "123"]) }}
#else
{{ Form::open(['route' => 'country.store', 'id'=> 'admin_store_form' ]) }}
#endif
<div class="row mb-2">
<div class="col-sm-1">
{{ Form::label('name', 'Name', ['class' => 'col-sm-2 admin-label col-form-label']) }}
</div>
<div class="col-sm-3">
{{ Form::text('name', old('name'), ['placeholder'=>'Name', 'class' => 'form-control form-control-sm admin-control']) }}
</div>
</div>
{{ Form::close() }}
I define 'class' => "123" but that not work.
The correct way to add a class to Form::model is:
{{ Form::model($country, ['route' => ['country.show', $country->id], 'class' => '123']) }}
You need to add the class key to the same array argument as route.

Select box in form blade Laravel

how can I create a select box which is filled with values from the db?
The view is published with the $groups variable. In my select box i need $groups->id (hidden, only for storing) and $groups->name. This is currently my form.
{!! Form::open(array('route'=>'store.invitation')) !!}
<div class="form-group">
{{Form::label('username', 'Username')}}
{{Form::text('username', '', ['class' => 'form-control', 'placeholder' => 'Enter Username'])}}
{{Form::label('groupname', 'Gruppe')}}
{{Form::select($groups->name) }}
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
Thanks
You should pass an array of the data that you get from the db. Like this:
Form::select('size', array('L' => 'Large', 'S' => 'Small'));
This documentation might be old, but thats how you can create Form::select in blade.

Laravel checkbox $request issue

I am trying to pass 2 array of checkboxes, to my controller: roles and permissions to insert and/or patch in DB.
in my controller I am passing the collections to display on blade:
$roles = Role::all();
$permissions = Permission::all();
and all works fine as it displays the name and the checkbox.
Yet 2 issues when submitting:
$request is showing a simple array for each without the name of the role/permission.
I do not get an "off" for non checked.
http://prntscr.com/jq61p6
I am expecting to insert/patch roles and permissions for a specific $user->id.
I am assuming the problem is in blade and in my checkbox inputs:
<form method="POST" action="{{ route('roles-permissions',['id' =>$user->id]) }}">
{{ csrf_field() }}
<h5>User Roles</h5>
<div class="col-xs-12">
#foreach ($roles as $role)
{{ Form::checkbox('roles[]', null, true, ['class' => 'name'] ) }}
{{ Form::label($role->name, ucfirst($role->name)) }}<br>
#endforeach
</div>
<div class="break-20"></div>
<h5>User Permissions</h5>
<div class="col-xs-12">
#foreach ($permissions as $permission)
{{ Form::checkbox('permissions[]', null , false, ['class' => 'name']) }}
{{ Form::label($permission->name, ucfirst($permission->name)) }}<br>
#endforeach
</div>
{!! Form::submit('Save Roles and Permissions', ['class=', '"btn btn-primary width-250 mt-20"']); !!}
</form>
How can I fix this so I can see the role and permission name so I can pick up the right permissions/roles by my controller and save them into my roles/user and permissions/user tables?
Thanks.
3rd argument will decide whether it will be checked or not. Use null instead of true & let's check whether it works or not.

Resizing checkbox in form group in Laravel

<div class="col-md-3">
<div class="white-box">
{{ Form::open(['route' => ['resources.select'], 'method' => 'ANY', 'role' => 'select']) }}
<div class="form-group">
{{ Form::label('category_label', 'Categories : ') }}
{{ Form::select('category_select', $category_select, ['class'=>'form-control']) }}
{{ Form::button('Submit',['type'=>'submit','class'=>'btn btn-success waves-effect waves-light m-r-10', 'id'=>'select_resource']) }}
</div>
{{ Form::close() }}
</div>
</div>
This is my code. I have three different divisions that look the same. When I tried to change to a grid view, The select boxes stay overlapping the div block.
I want them to stay within the block in the view.
The reason why your select is overlapping is because you are missing null as a parameter in the declaration.
Change this line;
{{ Form::select('category_select', $category_select, ['class'=>'form-control']) }}
to this;
{{ Form::select('category_select', $category_select, null, ['class'=>'form-control']) }}
Also you do not need the to style the form. A more efficient way of creating the form in your question would be like the example below
<div class="col-md-3">
<div class="white-box">
{{ Form::open(['route' => ['resources.select'], 'method' => 'ANY', 'role' => 'select']) }}
<div class="form-group">
{{ Form::label('category_label', 'Categories : ') }}
{{ Form::select('category_select', $category_select, null, ['class'=>'form-control']) }}
</div>
{{ Form::button('Submit',['type'=>'submit','class'=>'btn btn-success waves-effect waves-light m-r-10', 'id'=>'select_resource']) }}
{{ Form::close() }}
</div>
</div>

laravel blade form show me an exception error

First of all sorry for my bad english.
I have a laravel form using blade template engine that show me an exception error. If i remove the code and use the php form with echo statmente everything fine and the page show up.
where is the problem? Here is my code
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'news')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::title('name', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', Input::old('description'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Create the Nerd!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
There is no such thing as Form::title() in laravel.
Try this
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'news')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', Input::old('description'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Create the Nerd!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
Make sure you figure these errors yourself as these are typing mistakes.

Resources