在中间件laravel

时间:2016-12-13 17:28:41

标签: php laravel laravel-5

我试图在中间件中调用模型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

1 个答案:

答案 0 :(得分:1)

您的Readdb存在于名称空间App中,而不是App\Models,因此要么在中间件中执行此操作:

use App\Readdb;

或者,如果您的composer.json说要在App\Models目录中查找app/Models,请更新Readdb上的命名空间:

namespace App\Models;