【问题标题】:model with user id laravel具有用户 ID laravel 的模型
【发布时间】: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; } 从那开始。

标签: php model laravel-4


【解决方案1】:

看起来问题是没有登录 - 这个模型有效。

因为这可能会在将来对其他人有所帮助:当您创建用户帐户时,请在将密码添加到数据库时使用 Hash::make,因为当您在登录方法中使用 Auth::attempt 时,Laravel散列用户输入的密码并比较两者,如果它们都没有散列,则用户不会登录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2015-03-02
    • 2022-11-15
    • 1970-01-01
    • 2018-11-06
    • 2014-03-04
    • 2021-09-28
    相关资源
    最近更新 更多