繁体   English   中英

Laravel:检查记录是否存在

[英]Laravel: Check if record exists else

我在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字段的赏金表中已有值,我希望表单将该值设置为默认值,否则,如果应该返回空白。

但是,我一直收到这个错误

试图获取非对象的属性(查看:/home/stephenm/public_html/app/views/Titles/EditBounty.blade.php)

<?php echo Form::text('address', Input::old('address'), array('class' => 'form-control')); ?>

address字段中没有值时

你可以使用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')) }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM