Praful
Praful

Reputation: 31

Property [id] does not exist on this collection instance in laravel8 where condition

I am getting below error in the controller

Property [id] does not exist on this collection instance

Below is my controller , But in the MainBalance where condition getting error. On the first query i want fetch data on the second query. So when i add the id in where condition getting below error

$distributor_list In this variable getting all the distributor list. In that id based i want to fetch data from MainBalance

Property [id] does not exist on this collection instance


    if($request->ajax()){
        $where_str    = "1 = ?";
        $where_params = array(1); 

        if (!empty($request->input('sSearch')))
        {
            $search     = addslashes($request->input('sSearch'));
            $where_str .= " and (distributors.distributor_name like \"%{$search}%\""
            . " or distributors.spoc_name like \"%{$search}%\""
            . ")";
        }                                            

        $columns = ['distributors.id','distributors.distributor_name','admins.name','distributors.updated_at'];

        $distributor_columns_count = Distributor::select($columns)
        ->leftjoin('admins','admins.id','=','distributors.updated_by')
        ->whereRaw($where_str, $where_params)
        ->count();

        $distributor_list = Distributor::select($columns)
        ->leftjoin('admins','admins.id','=','distributors.updated_by')
        ->whereRaw($where_str, $where_params);

        if($request->get('iDisplayStart') != '' && $request->get('iDisplayLength') != '' && $request->get('iDisplayLength') != "-1"){
            $distributor_list = $distributor_list->take($request->input('iDisplayLength'))
            ->skip($request->input('iDisplayStart'));
        }          

        
            $sql_order='';
            for ( $i = 0; $i < $request->input('iSortingCols'); $i++ )
            {
                $column = $columns[$request->input('iSortCol_' . $i)];
                if(false !== ($index = strpos($column, ' as '))) {
                    $column = substr($column, 0, $index);
                }
                $distributor_list = $distributor_list->orderBy($column,$request->input('sSortDir_'.$i));   
            }
      


        $distributor_list = $distributor_list->get();


        $opening = MainBalance::select(DB::raw('(SUM(IFNULL(debit,0)) - SUM(IFNULL(credit,0))) as opening'))
                ->where('distributor_id', $distributor_list.$id)->first();
                // ->where('register_date', "<" , $fromdate)->first();
        if($opening['opening'] > 0) {
            $opening_balance['debit'] = abs($opening['opening']);
            $opening_balance['credit'] = '';
        } else {
            $opening_balance['credit'] = abs($opening['opening']);
            $opening_balance['debit'] = '';
        }
        array_unshift($distributor_list, $opening_balance);
    }

    return view('admin.reports.distributor');

Anyone have idea what is wrong then please let me know

Upvotes: 0

Views: 124

Answers (1)

Saroj Shrestha
Saroj Shrestha

Reputation: 2885

You are accessing it incorrectly. Try this (read full answer before trying instantly):

$opening = MainBalance::select(DB::raw('(SUM(IFNULL(debit,0)) - SUM(IFNULL(credit,0))) as opening'))
                ->where('distributor_id', $distributor_list->id)->first();

But just changing $distributor_list->id won't fix it. Because, $distributor_list is collection instead of an object.

So, you have two options here.

  1. If you need single distributer, you need to first change this $distributor_list = $distributor_list->get(); to:
$distributor_list = $distributor_list->first();

and then the above $opening = MainBalance.... query might work.

  1. Instead if you want to fetch MainBalance having multiple distributer_id then you might have to use query as below:
$distributor_list_ids = $distributor_list->pluck('id')->toArray();
$opening = MainBalance::select(DB::raw('(SUM(IFNULL(debit,0)) - SUM(IFNULL(credit,0))) as opening'))
                ->whereIn('distributor_id', $distributor_list_ids)->get();

Upvotes: 0

Related Questions