2016-11-18 4 views
0

私はモーダルのpicklistフォームを作成したいと思います。ここに私のコントローラは、次のとおりです。未定義の変数:picklistを作成しようとするとき

public function picklist(Request $request) 
{ 
    $q = $request->q; 
    $lists = Customer::where('name', 'like', "%$q%") 
     ->orderBy('name')->paginate('5'); 
    return view('customer._customer_list') 
     ->with('lists', $lists); 

} 

、これは私はそれが働いたlocalhostの/顧客/リスト を開いたときに、これは私のルート

Route::get('/customer/list/', '[email protected]'); 

ある

<tbody> 
     @foreach ($lists as $key => $list) 
      <tr> 
       <td> 
        {{ $list->name }} 
       </td> 
       <td> 
        <button data-dismiss="modal" class="btn btn-warning btn-xs btn-choose" 
         type="button" data-id="{{ $list->id }}" data-name="{{ $list->name }}">Pilih</button> 
       </td> 
      </tr> 
     @endforeach 
    </tbody> 
</table> 
{!! $lists->appends(Request::except('page'))->render() !!} 

_customer_list.blade.php図であり、

しかし、このような別のビューに渡そうとすると、

このようなルートの場合 Route :: get( '/ customer/add'、 'C​​ustomerController @ create');

私はエラーを得た:

Undefined variable: lists (View: /srv/web/resources/views/customer/_customer_list.blade.php) (View: /srv/web/resources/views/customer/_customer_list.blade.php)

答えて

0

Undefined variable: lists (View: /srv/web/resources/views/customer/_customer_list.blade.php) (View: /srv/web/resources/views/customer/_customer_list.blade.php)

このエラーは、ブレードがリスト変数を取得することはできません(foreachループで)あなたのビューを意味します。

は、2番目の引数としてパラメータを与えるとのビューをレンダリングしてみ、

public function picklist(Request $request) 
{ 
    $q = $request->q; 
    $lists = Customer::where('name', 'like', "%$q%") 
     ->orderBy('name')->paginate('5'); 
    return view('customer._customer_list', ['lists' => $lists]); 
} 
以下のようなもの

これはあなたのために働く希望:)

+0

アイブ試みてきたが、それはまだエラーにundifined変数を与えます。 –

+0

もちろん、** customer._customer_list ** bladeにパラメータを渡さない別のブレードに@include( 'customer._customer_list')を含めると、エラーが発生します。 ** @ include( 'customer._customer_list'、[ 'lists' => 'your list']); ** –

+0

これを試してみると、@section( ' ( 'customer'_customer_list'、['lists' => 'lists "]) @endsection しかし、私は、foreachのために供給されるエラー無効な引数()(閲覧:/srv/web/resources/views/customer/_customer_list.blade.php)を得るので、私は、私は_customer_listに@foreachを削除すると思います –

関連する問題