skip to Main Content

I have been preparing one application in Laravel 10 and in my app I am not connecting with DB as it is just PHP code and based on that need to create CSV file. I have prepared my code in controller and model function as per below.

My controller file:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsgetPaymentDatesModel;
class getpaymentdatescontroller extends Controller
{
    //
    public function getPaymentDatesCSV(Request $filename){

        $storageDir = getPaymentDatesModel::StorageDirPath();
        $filePath = $storageDir.$filename;
        $result = getPaymentDatesModel::GenearePaymentDateListCsv(array($filePath));
        
    }
}

My Model file:

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesStorage;
use IlluminateHttpRequest;

class getPaymentDatesModel extends Model
{
    
    public string $filePath;

    public static function scopeGenearePaymentDateListCsv($filePath){
        echo $filePath;
        exit;
    }
    /**
     * Create Storage File in storage inside app.
     *
     *
     */
    public static function scopeStorageDirPath(){
        $storagePath = storage_path();
        $directoryName = '/Console/'.'PaymentDatesCsv/'; // Store all CSV under common folder
        if (! Storage::exists($directoryName)) {
            Storage::makeDirectory($directoryName);
        }
        return $directoryName;
    }
}

My issue is, while I trigger my code, I am not getting value of $filePath in model file from controller,and it is giving me an error like:

 Object of class IlluminateDatabaseEloquentBuilder could not be converted to string

I have checked in google but couldn’t find proper solution. Can someone guide me how can I receive value from controller to model file?

Thanks.

2

Answers


  1. you are using the Laravel Eloquent’s scope method unnecessarily.

    This is indicated by the eloquent query builder error you mentioned

    `just name your classes without the scope prefix, they are for local query scope when querying the database

    you do not need query scopes if you are not connecting to db.

    Login or Signup to reply.
  2. Remove the scope prefix from the methods generatePaymentDateListCsv and storageDirPath. This prefix is used for defining query scopes in Eloquent and is not appropriate for static methods that are not query scopes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search