Laravel-调用未定义的方法Illuminate \ Database \ Query \ Builder :: user() [英] Laravel - Call to undefined method Illuminate\Database\Query\Builder::user()

查看:44
本文介绍了Laravel-调用未定义的方法Illuminate \ Database \ Query \ Builder :: user()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于从头开始创建Laravel:更新记录并渴望加载.我已经遵循了tut的规定,但是尝试在CardsController中添加用户数据时出现此错误.我以为我在某处错过了与卡用户关系的一步,但是我已经观看了3次视频,并且在数据库中查询了User,Card&注意与视频完全匹配.

I am busy with Laravel From Scratch: Updating Records and Eager Loading. I have followed the tut but I am getting this error when trying to add user data in CardsController. I am assuming that I've missed a step in the card user relationship somewhere but I've watched the video 3 times and my database queries for User, Card & Note matches the video exactly.

通过迁移创建用户"表后,我是否还需要执行另一步?

Is there another step I need to perform after creating the Users table via the migration perhaps?

错误

BadMethodCallException in Builder.php line 2345:

调用未定义的方法Illuminate \ Database \ Query \ Builder :: user()

Call to undefined method Illuminate\Database\Query\Builder::user()

CardsController代码

<?php
namespace App\Http\Controllers;
use App\Card;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CardsController extends Controller
{
    public function index()
    {
        $cards = Card::all();
        return view('cards.index', compact('cards'));
    }

    public function show(Card $card)
    {
        $card = Card::with('notes.user')->get();
        return $card;
        return view('cards.show', compact('card'));
    }
}

推荐答案

您的 note 模型缺少与其关联的 user 的关系定义,看起来像这样.

Your note model is lacking the relationship definition for it's associated user, it looks like.

您应该能够像这样在 Notes 模型中添加关系:

You should just be able to add the relationship in the Notes model like this:

public function user()
{
    return $this->belongsTo(User::class);
}

这篇关于Laravel-调用未定义的方法Illuminate \ Database \ Query \ Builder :: user()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆