【问题标题】:Trying to get property of non-object in Laravel 5试图在 Laravel 5 中获取非对象的属性
【发布时间】:2016-11-30 04:36:58
【问题描述】:

我收到了

ErrorException: 试图获取非对象的属性

我正在尝试使用此功能显示所有链接关系。关系是->

-卡片可以有很多笔记,笔记只有一张卡片。 - 每个笔记只包含一个用户,用户只包含一张卡片。我的代码是:

型号

  1. 注意

    class Note extends Model
    {
    protected $fillable = ['body'];
    
        public function cards()
        {
            return $this->belongsTo(Card::class);
        }
    
        public function users()
        {
            return $this->belongsTo(User::class,'id');
        }
    
    }
    
  2. 用户

    class User extends Authenticatable
    {
        use Notifiable;
    
        public function note()
        {
            return $this->belongsTo(Note::class);
        }
    
        protected $fillable = [
            'name', 'email', 'password',
        ];
    
        protected $hidden = [
            'password', 'remember_token',
        ];
    }
    
  3. 卡片

    class Card extends Model
    {
         public function notes()
        {
            return $this->hasMany(Note::class);//relationship
        }
    
        public function addNote(Note $note)
        {
            return $this->notes()->save($note);
        }
    }
    

控制器

  1. 卡片控制器

    class cardsController extends Controller
    {
        public function index()
        {
            $cards = Card::all();
            return view('cards.index',compact('cards'));
        }
    
    public function show(Card $card)
    {
        $card->load('notes.user'); **The Error is here.**
        $return $card;
        return view('cards.show',compact('card'));
    }
    

    }

  2. NotesController

    class notesController extends Controller
    {
    
        public function store(Request $request, Card $card)
        {
            $card->addNote(
                    new Note($request->all())
                ); 
    
            return back();
       }
    
        public function edit(Note $note)
        {
            return view('notes.edit',compact('note'));
        }
    
        public function update(Request $request, Note $note)
        {
            $note -> update($request -> all());
    
            return back();
        }
    }
    

数据库

1。 notes table

$card->load('notes.user'); - 当我将(笔记)放入cardControllers 函数时,它将显示从卡片和笔记表链接的所有数据。 - 但是当我尝试添加(notes.user)时,它说试图获取非属性对象。我错过了什么吗?

【问题讨论】:

标签: php laravel laravel-5


【解决方案1】:

这是一个非常常见且不言自明的错误,当您尝试访问一个实际上并不存在的类属性时会出现此错误。

为避免这种情况,请在访问属性之前使用 isset()

【讨论】:

  • 数据已经存在,但表之间无法连接
【解决方案2】:

在笔记控制器中尝试这样 $notes=DB::table('notes')->get(); return view('cards.show')->with('notes',$notes); 它会起作用的。

如果你想显示使用 @foreach($notes as $row) {{$row->user_id}} @endforeach

`

【讨论】:

  • 是的,但它只连接note table和card table,而不是user table。
  • 左连接可用于根据您的数据连接用户表
【解决方案3】:
$card->load('notes.user');

这意味着您正在尝试从 Card 模型加载两个关系 - 注释和用户。查看您的模型 user 关系在您的 Card 模型中缺失。此外,在您的笔记模型中,您将其称为“用户”。如果您在整个应用程序中使用单数术语会更好。

由于没有加载任何关系,很明显在访问$model->relation$model->relation->property时得到property of non-object

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2018-02-21
    • 2015-09-22
    相关资源
    最近更新 更多