How can I pass a parameter from the controller index function to the model's functions? LARAVEL

Viewed 91

//------------------------------// //below is the controller index function code // //------------------------------//

public function index($from, $to,$id)
    {
         $data=Attendance::where('attendance_date','>=',$from)
         ->where('attendance_date','<=',$to)
         ->with('userAttendance')//--> **I need to pass the $id to this function in the model**
         ->with('admin')
        ->get()

//---------------------------------// //below is the model code // //----------------------------------//

class Attendance extends Model
{
    protected $table = "attendances";
    protected $fillable=[
        'attendance_date',
        'admin_id',
    ];
                                       //---> **i need the $id passed to here**
    public function userAttendance()
    {
       return $this->belongsToMany('App\User', 'user_attendances','attendance_id','user_id')
       ->withPivot(
        'present_absent',
        'excuse',
        'attendance_key_amount',
        'verified_date',
        'comment',
       );
    }
}
1 Answers

just try this

Controller:

 public function index()
    {
        $id = 50;
        Product::getId(50);
    }

Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public static function getId($id){
        dd($id);
    }
}
Related