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>