2017-01-24 30 views
0

我有一個students_record表,我救了我的JSON格式有在結果上編輯嵌套陣列工作(laravel 5.3)

$students_data = Students::findOrFail($id); 

    $attributes = DB::table('studentsdata')->where('id', $student_data->id)->select('attributes')->first(); 
    $result = json_decode($student_data->meta_attributes); 
      echo "<pre>"; 
      print_r($result); 
      die; 

我得到的結果

stdClass Object 
(
    [name] => test 
    [status] => Suscess 
    [country] => Array 
     (
      [0] => Pakistan 
      [1] => USA 
     ) 

    [days_time] => months 

) 

I數據通過結果來查看我的越來越名字簡稱爲

<label> Name</label> 
     <div class="col-md-8"> 
      <div class="input-icon right"> 
       <input type="text" name="name" value="{{isset($result->name) ? $result->name : '' }}" /> 
      </div> 
       </div> 

其在該領域的精細給我的我的名字了

我想國家陣列也是我的國家字段陣列中的所有國家,請幫助我怎麼可以在這裏我已經在$結果如我上面對我的查詢輸入

<label> Country </label> 
        <div class="col-md-8"> 
         <div class="input-icon right"> 
          <select id="country" multiple name="country[]" onchange=" 
          getValue('opens-clicks-region', 'student_tracking', 'region', 'country', this.value, this.id)" disabled="disabled"> 
            <option value=""></option> 
          </select> 
         </div> 
        </div> 

回答

0

您可以使用foreach循環產生<option>標籤,如果你正在使用laravel其刀片更容易

@foreach ($result->country as $country) 
    <option value="{{ $country }}"> {{ $country }} </option> 
@endforeach 

看看這裏的刀片文檔:https://laravel.com/docs/5.3/blade#loops

+0

我已經下拉在這種情況下,它應該是選擇國家我如何達到它 –

0

嘗試這樣

<select id="country" multiple name="country[]" onchange="getValue('opens-clicks-region', 'student_tracking', 'region', 'country', this.value, this.id)" disabled="disabled"> 
    @if(!empty($result->country)) 
     @foreach($result->country as $key => $country) 
      <option value="{{ $key }}">{{ $country }}</option> 
     @endforeach 
    @else 
     <!-- Default case if there is no country --> 
     <option value="default_key">Default value</option> 
    @endif 
</select> 

FYI一些事情:Laravel刀片具有check爲isset輸入字段

可以更換

<input type="text" name="name" value="{{isset($result->name) ? $result->name : '' }}" /> 

<input type="text" name="name" value="{{ $result->name or '' }}" /> 

希望它有助於!

+0

我有下拉在這種情況下,它應該被選中國家如何達到它 –

+0

它說undefined變量$結果 –

+0

然而在我的編輯其結果罰款$結果 - >國家 –