Arpita Siddiquee
Arpita Siddiquee

Reputation: 1

Laravel 5.6 helper class

Laravel 5.6 , model name is not found in helper class (N:B: helper class is located in App\Helpers\Helper.php)

enter image description here

Upvotes: 0

Views: 756

Answers (3)

Florian Laforgue
Florian Laforgue

Reputation: 299

The model name is not enough for Laravel to retrieve the right class, it needs the namespace too.

You can try this if all your models are in the App\Modelsnamespace :

public static function getAll($modelName)
{
    $modelName = '\App\Models\' . $modelName;
    return $modelName::all();
}

But if it is not the cas, you should send the whole name to your helper method like that :

Helper::getAll(\App\Models\User::class);
Helper::getAll(\App\Models\Subfolder\OtherModel::class);
// ...

Let me know if it helped you :)

Upvotes: 1

nazzalra
nazzalra

Reputation: 1

try this.
first import the Model namespace like this

use Illuminate\Database\Eloquent\Model;

then, add Model keyword before $model. like

public function getAll(Model $model){ // your code }

may it can help :)

Upvotes: 0

W Kristianto
W Kristianto

Reputation: 9313

You need to add namespace.

<?php

namespace App\Helpers; // <--- Add this

class Helper{
...

Upvotes: 0

Related Questions