0
votes

I have the code at the end of my question. Here is my problem. I can't figure out why my input variable doesn't work in the model. It comes back with "Object of class Illuminate\Database\Eloquent\Builder could not be converted to string" and points to and "f.object_status_code = '$s_status'" line in the query.

Oddly enough, i made a mistake in the model by typing some extra random argument like this at one point...

public function scopegetFeeList($mistake, $s_status){

... and this returns results even though first argument shouldn't be there. I don't know why that works. But the correct version with a single argument of $s_status gives me that "could not be converted to string error."

I am trying to understand how I can successfully pass the argument to the model with a raw db query. (The reason I am using raw query is the query is much complex than represented here. I simplified it for illustration purposes.)

I also tried this notation: where f.object_status_code = '."$s_status."' got same error message.

Any help would be appreciated.

Now my code:

Route:

Route::get('/srfees', 'SRFeesRepController@srfeelist')->name('srfeesrep');

Controller:

<?php

namespace App\Http\Controllers\Apps;

use App\Http\Controllers\Base\HomeController;
use Illuminate\Http\Request;
use App\Http\Models\Apps\SRfeesRepModel;

class SRFeesRepController extends HomeController
{

    public function srfeelist(Request $request)
    {
       
        $s_status = $request->input('s_status','Inactive'); 

        $fee_list = SRfeesRepModel::getFeeList($s_status);
        $base_data = $this->get_base_data();

        return view('pages.apps.srfeesrep')
         ->with('base_data', $base_data)
         ->with('fee_list', $fee_list);       
    }

}

Model:

<?php
namespace App\Http\Models\Apps;
use Illuminate\Database\Eloquent\Model;
use DB;

class SRfeesRepModel extends Model
{
    public function scopegetFeeList($s_status){


        $results = DB::connection('MyDB')
        ->select(DB::raw("

                SELECT 
                f.code as 'code'
                ,f.printcode as 'printcode'
                ,f.object_status_codeas 'status'
                
                  FROM fee f 
                   where f.object_status_code = '$s_status'
                                  
                    ") ) ;
          
         return $results;
         
    }
}
1
this isn't a scope, it should just be a regular method on the modellagbox

1 Answers

0
votes

Replace your scope method with this one

public function scopegetFeeList($query,$mistake, $s_status){
}