dqcwl02022 2016-11-30 04:36
浏览 31

试图在Laravel 5中获得非对象的属性

I'm getting the

ErrorException: Trying to get property of non-object

I'm trying to display all the relationship that is link using this function. The relationship is ->

-Card can have many notes, Notes only have one card. -Each notes contain only one user, User contain only one card. My code is:

Model

  1. Note

    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. User

    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. Card

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

Controller

  1. CardsController

    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();
        }
    }
    

DB

1. notes table

$card->load('notes.user'); -when I just put (notes) in the cardControllers function, it will appear all data that link from card and notes table. -but when I try to add (notes.user) it said that trying to get non property object. Am I missing something?

  • 写回答

3条回答 默认 最新

  • doutangliang7769 2016-11-30 04:59
    关注

    This is a very common and self explanatory error, it comes when you are trying to access a class property which doesn't really exist.

    To avoid this, use isset() before access the property.

    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了