【问题标题】:Unable to fetch data in view using laravel无法使用 laravel 获取视图中的数据
【发布时间】:2019-10-05 01:26:28
【问题描述】:

这是我在 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)

【问题讨论】:

  • 您的 .env 文件中的连接参数设置是否正确?
  • 是的...我的管理面板已经作为控制器内的管理文件夹运行
  • 在您的查询中缺少 ->get() ?
  • 非常感谢....我是 laravel 的新手
  • @Ahmed,你能添加评论作为答案吗?Arpan 记得添加已接受的答案。

标签: php laravel laravel-5.7


【解决方案1】:

您在查询中缺少 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    相关资源
    最近更新 更多