Use variable of current Eloquent in `WITH` method - LARAVEL -


so have code (which won't work):

$accessuser = accessuser::where('user_id', auth::user()->id)               ->with(['overtimes' => function ($qry) use ($accessuser) {                   $qry->where('waiting_for', $accessuser->access_level)->get();               }])->get(); 

obviously, want value of access_level column , compare waiting_for column.

is there solution this? let me know if need additional information table structure, etc.

edited

here's accesschartusermap model:

schema::create('accesschart_user_maps', function (blueprint $table) {     $table->increments('id');     $table->integer('accesschart_id')->unsigned();     $table->integer('user_id')->unsigned()->nullable();     $table->integer('access_level')->unsigned()->nullable();     $table->timestamps();      $table->foreign('user_id')         ->references('id')->on('users')         ->ondelete('cascade');      $table->foreign('accesschart_id')         ->references('id')->on('access_charts')         ->ondelete('cascade'); }); 

my overtime model:

schema::create('overtimes', function (blueprint $table) {     $table->increments('id');     $table->integer('user_id')->unsigned();     $table->integer('accesschart_id')->unsigned()->nullable();     $table->integer('remarks_by')->unsigned()->nullable();     $table->integer('waiting_for')->nullable();     $table->datetime('date_from');     $table->datetime('date_to');     $table->longtext('reason');     $table->longtext('remarks')->nullable();     $table->integer('status')->nullable();     $table->timestamps();      $table->foreign('user_id')         ->references('id')->on('users')         ->ondelete('cascade');      $table->foreign('remarks_by')         ->references('id')->on('users')         ->ondelete('cascade'); }); 

here's query want achieve:

select accessuser.user_id,accessuser.accesschart_id,accessuser.access_level,overtimes.waiting_for accesschart_user_maps accessuser inner join overtimes on accessuser.accesschart_id = overtimes.id accessuser.user_id = 7 , accessuser.access_level=overtimes.waiting_for 

i think "query builder" version of query want achieve.

$accessuser = db::table('accesschart_user_maps accessuser') ->join('overtimes', 'accessuser.accesschart_id', '=', 'overtimes.id') ->select('accessuser.user_id','accessuser.accesschart_id','accessuser.access_level','overtimes.waiting_for') ->where('accessuser.user_id', \auth::user()->id) ->where('accessuser.access_level', 'overtimes.waiting_for') ->get(); 

hope helps


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -