vandan's avatar
Level 13

Trying to get property 'multiple_doc' of non-object

i try to display child table record but when i display then error like

Trying to get property 'multiple_doc' of non-object

here is my blade file

<img src="{{asset('/PG/doc/'.$view->user_id.'/'.$doc->multipledocs->multiple_doc)}}" alt="image">

here is my controller file

public function profile($id)
    {
        $view = Pg::where('user_id','=',$id)->first();
        $doc = Document::where('user_id','=',$id)->first();
        return view('viewprofile',compact('view','doc'));
    }

here is my document model

protected $fillable = [
        'user_id','doc_type', 'doc_name', 'doc_number'
];

    public function docs()
    {
        return $this->hasMany('App\Multipledocument');
    }

here is my multipledocument modal

protected $fillable = [
        'doc_id','multiple_doc'
    ];

public function multipledocs()
    {
        return $this->belongsTo('App\Document','doc_id');
    }

here document is my parent table and multipledocument my child table so how can i display thank you

0 likes
4 replies
sujancse's avatar
sujancse
Best Answer
Level 10

At first the name multipledocs in

public function multipledocs()
    {
        return $this->belongsTo('App\Document','doc_id');
    }

should be multipledoc as it belogs to Document

Second: You have to eager load multipledoc like $doc = Document::with('multipledoc')->where('user_id','=',$id)->first();

My suggestion please go through https://laravel.com/docs/6.x/eloquent-relationships#one-to-many this docs

1 like
vandan's avatar
Level 13

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'multipledocuments.document_id' in 'where clause' (SQL: select * from multipledocuments where multipledocuments.document_id in (4))

1 like
vandan's avatar
Level 13

i solved

change in document model

public function multipledoc()
    {
        return $this->belongsTo('App\Multipledocument','user_id');
    }

change in multiple document

public function docs()
    {
        return $this->hasMany('App\Document');
    }

Please or to participate in this conversation.