isset for form builder - php

I am using form builder and I share one form for edit and crete page so there are some variables I don't want to use.
I know that:
{!! Form::text('name', isset($admin_link->name) ? $admin_link->name : null, ['class' => 'form-control']) !!}
would work but I do not consider it the best way. Is there any way to use something else like isset for form builder?

I sent the model to the blade with values what caused autocomplete of the input what is probably what I have been looking for:
{!! Form::model( $admin_link, ['route' => ['admin.links.update', $admin_link->id], 'method' => 'post', 'class' => 'main_link_form']) !!}

Related

How to put translation into array in Laravel Form?

I need to have a prompt box which would be set through the key 'onsubmit' in Form array. This version works:
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => 'return confirm(\'Are you sure?\')']) !!}
But I am unable to inject a translation into it to make it work. So far I tried this without success:
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => 'return confirm(\''.{{trans('Users::users.current.account.index.box.confirm')}}.'\')']) !!}
Is there a way to concatenate function output inside the array value?
Please Try
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => "return confirm('".trans('Users::users.current.account.index.box.confirm')."')"]) !!}

Laravel no message error in Form::open

Hi I am using Laravel version 5.6 and got No message in a view using a Form::open with the code below
{!! Form::open(['route' => array('admin.msgs.send', 'id'=>$user->id)]) !!}
But I got the route on registred on my file, just like this:
Route::get('msgs/send/{id}', ['as'=>'admin.msgs.send', 'uses'=>'MsgsController#send']);
Some one knows what is wrong?
Try:
{!! Form::open(['route' => ['route.name', $user->id]]) !!}
or:
{!! Form::open(['route' => ['route.name', ['id' => $user->id]]]) !!}
The problem was the type of the method, I realized the route was registred as GET and not as POST, which is the type of a form.

Drop down select form in Laravel

may I know what is wrong with my codes? I have three user type name registered in my database however my codes will result to three drop down menus with individual user type name on each.
#foreach($user_types as $usertype)
<div class="form-group">
{!! Form::select('chap_user_type_name', array('chap_user_name' => $usertype), null, ['class' => 'form-control']) !!}
</div>
#endforeach
remove foreach and pass array into select like this
{!! Form::select('chap_user_type_name', $user_types, null, ['class' => 'form-control']) !!}
If $user_types is a collection, you need to use pluck() to build correct array for ::select:
$user_types = UserTypes::pluck('name', 'id');
Then just build select element like this:
{!! Form::select('chap_user_type_name', $usertype, null, ['class' => 'form-control']) !!}
You don't need the foreach above form::select will take care of it

How to set correct URL in Laravel?

I use Laravel 5 and have the following routing configuration:
$router->resource('restaurants', 'Registration');
And method:
public function store(Auth $userModel, Request $request){
});
In my view file I specifed form like:
{!! Form::open(array('url' => 'restaurants.store')) !!}
When I submit form I am transfered on the address: restaurants.store and get error:
Sorry, the page you are looking for could not be found.
You can use the url syntax which needs a slash like so:
{!! Form::open(array('url' => 'restaurants/store')) !!}
Or the route syntax (if you've got the route set up) like so:
{!! Form::open(array('route' => 'restaurants.store')) !!}
{!! Form::open(array('url' => route('restaurants.store'))) !!}
change {!! Form::open(array('url' => 'restaurants.store')) !!} with {!! Form::open(array('route' => 'restaurants.store')) !!} and since you use restful, in your routes.php $router->resource('restaurants', 'RestaurantsController');

Laravel 5.2 form url using named route

Using Laravel 5.2, I have a form in one of my Blade templates which I want to open and have it point to a named route, which includes a variable.
Essentially I want the resolved form command to open as follows:;
<form method="POST" action="http://my.url.com/dash/varname">
</form>
So, using Blade, I want to open the form using the named route so if this changes in my routes.php, the form will still work.
I am trying to do it as follows:
{!! Form::open(['url' => "route('dashboard.setup', ['var' => 'varname'])", 'method' => 'post']) !!}
{!! Form::close() !!}
also
{!! Form::open(['route' => "dashboard.setup, ['var' => 'varname']", 'method' => 'post']) !!}
{!! Form::close() !!}
But this does not work. How can I do this and avoid hard coding the url and variable? Thanks!
Try like this. By default, a POST method will be assumed;
{!! Form::open(array('route' => array('dashboard.setup', 'varname'))) !!}
{!! Form::close() !!}
Try the following:
{!! Form::open(array('route' => array('dashboard.setup', 'varname'), 'method' => 'post')) !!}
{!! Form::close() !!}

Categories