我在Laravel有一个表格
{{ Form::label('address', trans('Address')) }}
@if ( $bounty->address == "NULL")
{{ Form::text('address', Input::old('address'), array('class' => 'form-control')) }}
@else
{{ Form::text('address', $bounty->address, array('class' => 'form-control')) }}
@endif
这是控制器
public function bounty($id)
{
$title = $this->title->byId($id);
$bounty = array();
$bounty = DB::table('bounties')
->where('title-id', $id)
->first();
return View::make('Titles.EditBounty')->withTitle($title)->withBounty($bounty);
基本上,如果address
字段的bounty表中已有值,我希望表单将该值设置为默认值,否则,如果应该返回空白。
但是,我一直收到此错误
尝试获取非对象的属性(查看:/home/stephenm/public_html/app/views/Titles/EditBounty.blade.php)
<?php echo Form::text('address', Input::old('address'), array('class' => 'form-control')); ?>
address
字段
答案 0 :(得分:2)
您可以使用object_get()
辅助功能:
@if(!object_get($bounty,'address'))
{{ Form::text('address', Input::old('address'), array('class' => 'form-control')) }}
@else
{{ Form::text('address', $bounty->address, array('class' => 'form-control')) }}
@endif
甚至可以使用if
默认值完全消除Input::old()
条件:
{{ Form::text('address', Input::old('address', object_get($bounty,'address')), array('class' => 'form-control')) }}