我试图在中间件中调用模型Readdb
。但是我收到了错误:
Class' app \ Models \ Readdb'找不到
我的middlware代码是:
<?php
namespace App\Http\Middleware;
use Closure;
use App\Models\Readdb;
class Adminlogin {
public function handle($request, Closure $next) {
if (!$request->session()->has('userid')) {
$db = new Readdb();
return response()->view('admin.auth.login');
} else {
return response()->view('admin.dash');
}
return $next($request);
}
}
Readdb文件:
<?php
namespace App;
use \Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Readdb extends Model {
public function get_setting($name) {
$data = DB::table('setting')->select('value')->where('name', '=', $name)->get();
return $data->value;
}
}
readdb的路径是:/var/www/html/ecommerce/app/Models/Readdb.php
答案 0 :(得分:1)
您的Readdb
存在于名称空间App
中,而不是App\Models
,因此要么在中间件中执行此操作:
use App\Readdb;
或者,如果您的composer.json
说要在App\Models
目录中查找app/Models
,请更新Readdb
上的命名空间:
namespace App\Models;