Laravel 5.2 : Undefined Variable in WhereHas -


i undefined variable $jenis_mobil on $q->where('name', $jenis_mobil->name)

    $jenis_mobil = car_class::find($request->jenis_mobil);     $dari_kota = city::find($request->dari_kota);             $vehicles = vehicle::wherehas('car', function($q){         $q->wherehas('car_class', function($q){             $q->where('name', $jenis_mobil->name);         });     })     ->wherehas('partner', function($q) {         $q->wherehas('kota_pool', function($q){             $q->where('name', $dari_kota->name);         });     })     ->where('year', $request->tahun_mobil)     ->get(); 

is wrong code? think because $jenis_mobil not passed wherehas

you should use use() pass variables closures:

$vehicles = vehicle::wherehas('car', function($q) use($jenis_mobil) {     $q->wherehas('car_class', function($q) use($jenis_mobil) {         $q->where('name', $jenis_mobil->name);     }); }) ->wherehas('partner', function($q) use ($dari_kota) {     $q->wherehas('kota_pool', function($q) use ($dari_kota) {         $q->where('name', $dari_kota->name);     }); }) ->where('year', $request->tahun_mobil) ->get(); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -