Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

Showing error

Attempt to read property "address" on bool My controller


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student;
use Illuminate\Http\Response;
use App\Http\Requests\StorePostRequest;
use Illuminate\Auth\Events\Validated;

class FormController extends Controller
{
    public function index ()
    {
        return view ('form.index');
    }

    public function create ( StorePostRequest $request) 
    {
        $store = $request->validated();
        Student::create($store);
        session()->flash('success', 'Your account has been created');
        return redirect('/student');
    }

    public function show(Student $student) 
    {
        return view('form.show', compact('student'));
    }
}

My show blade file code

@extends('layouts.app')
@section('content')
    <div>
        <table>
            <tr>
                <td>Id</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Email</td>
                <td>Contact</td>
                <td>Address</td>
                <td>Image</td>
            </tr>
                
          @foreach ($student as $students )
            {{ $students->address}}              
          @endforeach
        </table>
    </div>
@endsection
0 likes
21 replies
Sinnbeck's avatar

You have just 1 student. Remove the foreach

      {{ $student->address}}              
Shivamyadav's avatar

@Sinnbeck after this ,it showing same error

@extends('layouts.app')
@section('content')
    <div>
        <table>
            <tr>
                <td>Id</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Email</td>
                <td>Contact</td>
                <td>Address</td>
                <td>Image</td>
            </tr>

          @foreach ( $students as $student )
          <td> 
                {{ $student->id}} 
            </td>

            <td>
                {{ $student->first_name}}
            </td>

           <td>
                {{ $student->last_name}}
           </td>

           <td>
                {{ $student->email}}
           </td>

            <td>
                {{ $student->contact}}
            </td>

            <td>
                {{ $students->address}}
            </td>

            <td>
                {{ $student->image}}
            </td>
          @endforeach
        </table>
    </div>
@endsection
Shivamyadav's avatar

controller code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student;
use Illuminate\Http\Response;
use App\Http\Requests\StorePostRequest;
use Illuminate\Auth\Events\Validated;

class FormController extends Controller
{
    public function index ()
    {
        return view ('form.index');
    }

    public function create ( StorePostRequest $request) 
    {
        $store = $request->validated();
        Student::create($store);
        session()->flash('success', 'Your account has been created');
        return redirect('/student');
    }

    public function show(Student $students) 
    {
        return view('form.show', compact('students'));
    }
}

Sinnbeck's avatar

@Shivamyadav You have one student, so dont name it $students (multiple). Name it $student (singular) :)

    public function show(Student $student) 
    {
        return view('form.show', compact('student'));
    }
Shivamyadav's avatar

@Sinnbeck done this thing and removed foreach loop ,but it is unable to show data from the table to the show page. Important thing is error has gone

Shivamyadav's avatar

@Sinnbeck chaged code , where foreach loop is commented

@extends('layouts.app')
@section('content')
    <div>
        <table>
            <tr>
                <td>Id</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Email</td>
                <td>Contact</td>
                <td>Address</td>
                <td>Image</td>
            </tr>
                
          {{-- @foreach ( $students as $student ) --}}
          <td> 
                {{ $student->id}} 
            </td>

            <td>
                {{ $student->first_name}}
            </td>

           <td>
                {{ $student->last_name}}
           </td>

           <td>
                {{ $student->email}}
           </td>

            <td>
                {{ $student->contact}}
            </td>

            <td>
                {{ $student->address}}
            </td>

            <td>
                {{ $student->image}}
            </td>
          {{-- @endforeach --}}
        </table>
    </div>
@endsection
Sinnbeck's avatar

@Shivamyadav try explaining what you see. You say there are no errors, but what do you see instead?

Sinnbeck's avatar

@Shivamyadav ok so sounds like your route model binding isn't working. Did you remember to change {students} to {student} there as well?

MichalOravec's avatar
Level 75

index - all students (here use @foreach)

public function index()
{
    $students = Student::all();

    return view ('form.index', compact('students'));
}

show - only one student

public function show(Student $student) 
{
    return view('form.show', compact('student'));
}
1 like
Sinnbeck's avatar

@Shivamyadav curious what in that answer fixed it? I assumed we were taking about the show method, and I have the same code 20 minutes ago?

1 like
Sinnbeck's avatar

@Shivamyadav not in the show method 😊 I assumed we were talking about the show method the whole time as that was where the error came from

But feel free to revert the show.blade.php to how it was in the beginning and see if it works 👍

1 like

Please or to participate in this conversation.