无法使用Laravel在视图中获取数据

时间:2019-05-17 17:30:29

标签: php laravel laravel-5.7

这是我在laravel上的第一个项目。我想在我的网站主页中获取一些数据,但是我无法做到这一点。得到一些错误。

namespace App\Http\Controllers;

use Validator;
use App;
use Lang;
use DB;
//for password encryption or hash protected
use Hash;
use App\Administrator;

//for authenitcate login data
use Auth;



use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class WebController extends Controller
{
function loadPage(){
    $categories = DB::table('categories')
        ->leftJoin('categories_description','categories_description.categories_id', '=', 'categories.categories_id')
        ->select('categories.categories_id as id', 'categories.categories_image as image',  'categories.categories_icon as icon',  'categories.date_added as date_added', 'categories.last_modified as last_modified', 'categories_description.categories_name as name', 'categories_description.language_id')
        ->where('parent_id', '0')->where('categories_description.language_id', '1');

    return view("home")->with('categories', $categories);
}
}

这是我的控制器。收到此错误。

  

未定义的属性:Illuminate \ Database \ MySqlConnection :: $ name(视图:/Applications/XAMPP/xamppfiles/htdocs/rssl/resources/views/home.blade.php)

1 个答案:

答案 0 :(得分:0)

您在查询的开头缺少get()

$categories = DB::table('categories')
        ->leftJoin('categories_description','categories_description.categories_id', '=', 'categories.categories_id')
        ->select('categories.categories_id as id', 'categories.categories_image as image',  'categories.categories_icon as icon',  'categories.date_added as date_added', 'categories.last_modified as last_modified', 'categories_description.categories_name as name', 'categories_description.language_id')
        ->where('parent_id', '0')->where('categories_description.language_id', '1')->get();