0

laravel 8 Property [id] does not exist on this collection instance. I know this question has been asked here before but i've been trying for days to solve this problem I just can't seem to find out where it's coming from, I'm quiet new at laravel so maybe i might have missed something if you could take look and let me know it'll be much apprieciated.

My model Answer

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class Answer extends Model
{
    use HasFactory;
    protected $guarded = ['id']; 

    public function questions(){
        return $this->belongsTo(Questions::class);
    }  
    
}

Route

Route::get('/answer', [QuestionaireController::class, 'answers'])->name('answer');

QuestionaireController

public function answers(Request $request){
        $user= Surveyed::all();
        $surveyedId= $user->id;
        $questions= Question::all();
        $answers= Answer::where('surveyed_id',$surveyedId)->get();
        return view('admin.answer', ['questions'=>$questions, 'answers'=>$answers]);        
    }

My view Answer.blade

<div class="questions">
  <table class="table_questions">
    <thead>
      {{-- <tr>
        <th>Numéro</th>
        <th>Question</th>
        <th>Type</th>
      </tr> --}}
   </thead>
   @foreach ($surveyeds as $surveyed)
    @foreach ($surveyed->answers as $answer)
   <tbody>
     <tr>
       {{-- -1 = 0 qui est le debut du tableau --}}
       <td>{{ [$answer->question_id-1] }}</td>
       <td>{{ [$answer->question_id-1]->body }}</td>
       <td>{{ [$answer->answer] }}</td>
     </tr>
    </tbody>
    @endforeach
    @endforeach
  {{-- </thead> --}}
</table>
</div>
Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
  • 1
    `$user = Surveyed::all();` With this, `$user` is a Collection. `$surveyedId= $user->id;`. You're trying to call `$user->id`, but a Collection of `Surveyed` Objects does not have an `id` property. You'd need to loop: `foreach ($user as $u)`, then `$u->id` is available. – Tim Lewis May 09 '22 at 16:53
  • Or https://stackoverflow.com/questions/63219872/property-role-does-not-exist-on-this-collection-instance, https://stackoverflow.com/questions/70378628/property-image-does-not-exist-on-this-collection-instance-laravel-with, https://stackoverflow.com/questions/67638492/laravel-relationship-property-does-not-exist-in-this-collection-instance, https://stackoverflow.com/questions/70030106/property-id-does-not-exist-on-this-collection-instance, etc. This _has_ been asked a lot, and the answer is almost always the same. – Tim Lewis May 09 '22 at 16:55
  • If you need to use `$user->id`, you can work around it like: `$answers = Answer::whereIn('surveyed_id', $user->pluck('id'))->get();`. You'll probably want to rename `$user` to something more sensical, like `$surveyeds = Surveyed::all()`, then delete the line `$surveyedId = $user->id;` (it has no purpose), then finally: `$answers = Answer::whereIn('surveyed_id', $surveyeds->pluck('id'))->get();`. That will get all `Answer` records for each of the returned `Surveyed` Ids from that Collection. – Tim Lewis May 09 '22 at 17:08
  • Thank you soo much for your detailed explanation, very much apprieciated. Now that I've done as you said it gives me "Undefined variable $surveyeds" error but I clearly defined the variable as you sujested – Daniel Jackson May 09 '22 at 18:31
  • Make sure it is A) spelled correctly and B) defined before you try to access `->pluck('id')`. Beyond that, there isn't much I can do to help you. If that still fails, use a different variable name, like `$records = Surveyed::all()`, the `$records->pluck('id')`. – Tim Lewis May 09 '22 at 18:33
  • Thanks Tim for taking the time to answer you've been grate help. – Daniel Jackson May 09 '22 at 18:38
  • No problem! Also, I just noticed this: `@foreach ($surveyeds as $surveyed)`; you're not sending that `$surveyeds` to the view (and it wasn't defined before this either!), you're only sending `$questions` and `$answers` via `return view('admin.answer', ['questions'=>$questions, 'answers'=>$answers]);` – Tim Lewis May 09 '22 at 18:39
  • Oh your right I wasn't too if I needed to add it or not i guest it only makes sence since i defined it earlier thanks. – Daniel Jackson May 09 '22 at 19:05

0 Answers0