Showing dropdown value in laravel form select List? - php

Here is my code:
controller file: EmergencyContactsController.php
$surnames = DB::table('salutations')->pluck('name');
return view('patient/emergencycontacts', ['salutation' => $surnames]);
Blade file: patient/emergencycontacts.blade.php
{!! Form::open(array('route' => 'emergencycontacts_store', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Salutation') !!}
{{ Form::select('role', ['' => 'Select Role'] + $salutation, null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{!! Form::label('First Name') !!}
{!! Form::text('firstname', null, array('required', 'class'=>'form-control', 'placeholder'=>'First Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Last Name') !!}
{!! Form::text('lastname', null, array('required', 'class'=>'form-control', 'placeholder'=>'Last Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Relationship') !!}
{{ Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) }}
</div>
<div class="form-group">
{!! Form::label('Phone') !!}
{!! Form::text('phone', null, array('required', 'class'=>'form-control', 'placeholder'=>'Phone')) !!}
</div>
<div class="form-group">
{!! Form::label('Fax') !!}
{!! Form::text('fax', null, array('class'=>'form-control', 'placeholder'=>'Fax')) !!}
</div>
<div class="form-group">
{!! Form::submit('Save',array('class'=>'btn btn-primary')) !!}
</div>
{{ Form::close() }}
When I go to url http://localhost:8000/patient/emergency-contacts/create it gives me error:
"Unsupported operand types"

You need to change 2 things
In your controller:
$surnames = DB::table('salutations')->pluck('name', 'id')->toArray();
So you get an array as [id => 'value'] and not only ['value']. In the View:
{!! Form::select('role', $salutation, null, ['class' => 'form-control']) !!}
{!! Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) !!}
{!! Form::close() !!}
Always 'escape' the Form tags, if not, the HTML will be printed on screen, not parsed.

please use view('patient.emergencycontacts', ['salutation' => $surnames]);

Related

Laravel blade views not showing updated content

I have a Laravel site running on a GoDaddy share hosting plan. I'm on PHP version 7.1. It has functioned fine up until yesterday. Now the blade views no longer show changes that occur on the database.
I see that the POST functions are working, as the changes are reflected in the database. They're just not showing up in the views themselves.
I've tried to clear the view cache as suggested here: Blade view not reflecting changes
I have also changed the timezone in config/app to 'America/Phoenix' to try and match godaddy's servers: https://www.godaddy.com/community/cPanel-Hosting/How-To-Change-Timezone-on-a-shared-server/td-p/102712
I've contacted GoDaddy's tech support and they couldn't find anything they believed could cause it.
Example Route:
//Resource
Route::resource('beer', 'BeerController');
Example Controller:
public function update(Request $request, Beer $beer)
{
$this->validate($request, ['name' => 'required']);
$beer->update(request(['name', 'beer_style_id', 'style', 'abv', 'ibus', 'srm', 'brewery_id', 'on_tap']));
return view('beers.show', compact('beer'));
}
Example View
#extends('layouts.master')
#section('content')
<div class="row">
<div class="col-sm-12">
<h1>Edit {{ $beer->name }}</h1>
<hr />
{!! Form::model($beer, ['route' => ['beer.update', $beer->id], 'method' => 'patch']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('brewery_id', 'Brewery') !!}<br />
{!! Form::select('brewery_id', $breweries, null, ['placeholder' => 'Select Brewery', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="row" style="margin-bottom: 1rem;">
<div class="col">
{!! Form::label('beer_style_id', 'Style Family') !!}<br />
{!! Form::select('beer_style_id', $beerstyles, null, ['placeholder' => 'Select Style', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="col">
{!! Form::label('style', 'Style') !!}
{!! Form::text('style', $value = null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 1.5rem;">
<div class="col">
{!! Form::label('abv', 'ABV') !!}
<div class="input-group">
{!! Form::number('abv', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
<span class="input-group-addon">%</span>
</div>
</div>
<div class="col">
{!! Form::label('ibus', 'IBUs') !!}
{!! Form::number('ibus', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col">
{!! Form::label('srm', 'SRM') !!}
{!! Form::number('srm', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
</div>
</div>
<div class="form-group" id="on-tap-checkbox">
{!! Form::checkbox('on_tap', '1') !!} On Tap
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
#include('layouts.errors')
{!! Form::close() !!}
</div>
</div>
#endsection

Laravel 5.4 unable to generate csrf token in form

I am using following codes for login form -
{!! Form::open(['url' => '/admin/login']) !!}
<div class="form-group">
{!! Form::label('email', 'Email Id:') !!}
{!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control input-sm']) !!}
</div>
<div class="form-group">
{!! csrf_field() !!}
{!! Form::submit('Login', ['class' => 'btn btn-primary btn-block']) !!}
</div>
{!! Form::close() !!}
But when I inspect it in browser , it is showing input field with name="_token" but with empty value.
Is there anything else I am missing? Please guide.

Laravel Form Model

My form.blade.php is sth like
<div class="form-group">
{!! Form::label('title', 'title :', ['class' => 'awesome']) !!}
{!! Form::text('product[title]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'description : ', ['class' => 'awesome']) !!}
{!! Form::text('product[description]', null, ['class' => 'form-control']) !!}
<div id="phone" class="form-group">
{!! Form::label('reference_id1', 'reference_id1 : ', ['class' => 'awesome']) !!}
{!! Form::text('product[reference_id1]', null, ['class' => 'form- control']) !!}
</div>
<div class="form-group">
{!! Form::label('category_id', 'category_id : ', ['class' => 'awesome']) !!}
{!! Form::select('category[]', $categories,null, ['class' => 'form- control', 'multiple']) !!}
</div>
<div class="form-group">
{!! Form::label('color', 'color : ', ['class' => 'awesome']) !!}
{!! Form::text('feature[0][color]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('height', 'height : ', ['class' => 'awesome']) !!}
{!! Form::text('feature[0][height]', null, ['class' => 'form-control']) !!}
</div> `
and my Edit.blade.php is like
{!! Form::model($product,['method' => 'PATCH', 'action' => ['ProductController#update',$product->id]]) !!}
#include('products.form', ['submitBtn' => 'submit'])
{!! Form::close() !!}
And this my ProductController.php#edit
public function edit($id)
{
$product = Product::with('feature')->findOrFail($id);
$categories = Category::pluck('title','id');
return view('products.edit')->withProduct($product)->withCategories($categories);
}
this is while when i wanna edit a product, the input requests are set empty!!
for instance when i go to http://myLarave/Public/product/2/edit the title and other inputs are empty :(
any suggestions?!
In your route.php or web.php depend to the version of your laravel, you can make the arg {id?}, for example:
Route::get('edit/{id?}', 'ProductController#edit');
and in the edit function you can initialize the variable $id=null or empty:
public function edit($id = null)
{
if($id != null){
$product = Product::with('feature')->findOrFail($id);
$categories = Category::pluck('title','id');
return view('products.edit')->withProduct($product)->withCategories($categories);
}
}

Form model data not running correctly on #yield Laravel 5

all I want is to define the form model on template and then put a yield as the content of form data. But it cannot assign the model data correctly into each field that has been defined.
This is my code:
template.detail.blade.php
#extends('admin.template.lte.layout.basic')
#section('content-page')
{!! Form::model($model, ['url' => $formAction]) !!}
#yield('data-form')
{!! Form::close() !!}
#if ($errors->any())
#stop
partial.edit.blade.php
#extends('template.detail')
#section('data-form')
<div class="form-group">
{!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
{!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
</div>
<div class="form-group">
{!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
{!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label for="Dal_Active">
{!! Form::hidden('Dal_Active', 'N') !!}
{!! Form::checkbox('Dal_Active', 'Y') !!}
Active
</label>
</div>
#stop
My Controller part:
/**
* Show the form for editing the specified resource.
*
* #param int $id
*
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$this->data['model'] = DssAlternative::find($id);
$this->data['formAction'] = \Request::current();
$this->data['dssOptions'] = Dss::lists('Dss_Name', 'Dss_ID');
return view('partial.edit', $this->data);
}
But the model data does not propagate to form correctly.
Sorry for my bad english.
It won't work since you are passing the $model object to partial/edit.blade.php file, and hoping to use it within template/detail.blade.php
exactly at this line
{!! Form::model($model, ['url' => $formAction]) !!}
Solution:
in template/detail.blade.php Take the form model line inside like this:
#extends('admin.template.lte.layout.basic')
#section('content-page')
#yield('data-form')
#if ($errors->any())
#stop
So partial/edit.blade.php would be like below:
#extends('template.detail')
#section('data-form')
{!! Form::model($model, ['url' => $formAction]) !!}
<div class="form-group">
{!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
{!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
</div>
<div class="form-group">
{!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
{!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label for="Dal_Active">
{!! Form::hidden('Dal_Active', 'N') !!}
{!! Form::checkbox('Dal_Active', 'Y') !!}
Active
</label>
</div>
{!! Form::close() !!}
#stop

Laravel 5.1 can't find template using #yield

I have a problem using #yield from one view to another. I couldn't get anything from my question.blade.php file, even if using just a simple text.
My create.blade.php file:
#extends('layout/layoutAdmin')
#section('content')
<div class="container">
<h1>Add a new inventory</h1>
{!! Form::open(['url' => 'admin/menu/inventories/', 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-xs-8 col-md-7 {!! $errors->has('text') ? 'has-error' : '' !!}">
{!! Form::label('text', 'Name:') !!}
{!! Form::text('text', null,['class' => 'form-control']) !!}
{!! $errors->first('text', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group col-xs-8 col-md-7">
{!! Form::label('description', 'Description:') !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
</div>
#yield('content2')
</div>
</div>
{!! Form::close() !!}
#stop
And my question.blade.php file:
#extends('inventory.create')
#section('content2')
<div class="form-group col-xs-8 col-md-7">
{!! Form::label('type', 'Type:') !!}
{!! Form::number('type', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group col-xs-8 col-md-7 {!! $errors->has('introduction') ? 'has-error' : '' !!}">
{!! Form::label('introduction', 'Introductory Text:') !!}
{!! Form::textarea('introduction', null, ['class' => 'form-control']) !!}
{!! $errors->first('introduction', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group col-xs-8 col-md-7">
{!! Form::label('value', 'Values:') !!}
{!! Form::textarea('value', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group col-xs-8 col-md-7 {!! $errors->has('language') ? 'has-error' : '' !!}">
{!! Form::label('language', 'Language:') !!}
{!! Form::text('language', null, ['class' => 'form-control']) !!}
{!! $errors->first('language', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group col-xs-8 col-md-7">
{!! Form::label('is_active', 'Active:') !!}
{!! Form::checkbox('is_active', null, ['class' => 'form-control']) !!}
</div>
#stop
My QuestionController:
public function create()
{
return view('inventory.question');
}
Route:
Route::get('admin/menu/inventories/new/question', 'QuestionController#create');
Edit: fixed the typo
You are yielding to a section called container2 and you have placed your content in a section called content2.

Categories