簡體   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