【发布时间】:2014-01-12 00:24:05
【问题描述】:
我正在开发一个应用程序,我想在其中获取用户的所有打开任务并显示它们。我创建了一个新的视图、路由、控制器和模型,但我无法让模型正常工作,尤其是在获取 SQL 查询的当前用户 ID 时。
app/models/Task.php
class Task extends Eloquent
{
public static function open()
{
$id = Auth::user()->id;
$tasks = DB::table('tasks')->where('c', 0)->where('user_id', $id)->get();
return $tasks;
}
}
还有
app/controllers/TaskController.php
public function open()
{
$tasks = Task::open();
return View::make('tasks.open')->with('tasks', $tasks);
}
错误:试图获取非对象的属性 - 看起来像 $id = Auth::user()->id;
正确的型号是什么?
【问题讨论】:
-
什么不正常,有什么错误吗?
-
“试图获取非对象的属性”看起来像“$id = Auth::user()->id;”模型中的线。
-
我只能想到用户没有登录?
-
if (Auth::check()) { $id = Auth::user()->id; }从那开始。