skip to Main Content

I have a laravel crud with relations. but when i want to load the page some table rows dont have a value. and this gives the error: ‘Attempt to read property "name" on null’

But how do i ignore this message and keep loading in the page?

index.blade:

    @foreach($files as $file)
        <tr>
            <td>{{$file->id}}</td>
            <td>{{$file->title}} </td>
            <td>{{$file->description_short}} </td>
            <td>{{$file->description_long}} </td>
            <td>{{$file->file}}</td>
            <td>{{$file->language->name}} </td>
            <td>{{$file->subfolder->name}} </td>
          
            <td>{{$file->role->name}} </td>
            <td>
                <a href="{{ route('admin.file.edit',$file->id)}}" class="btn btn-primary">Edit</a>
            </td>
            <td>
                <form action="{{ route('admin.file.destroy', $file->id)}}" method="post">
                  @csrf
                  @method('DELETE')
                  <button class="btn btn-danger" type="submit">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </tbody>
  </table>

controller:

 public function index()
    {
        $files = File::with('subfolder', 'language', 'tag')->get();
        $languages = Language::all();
        $tags = Tag::all();
        $subfolders = Subfolder::all();
        $users = User::all();    
        return view('admin.file.index', compact('files', 'languages', 'tags', 'users', 'subfolders'));
    }

i want the index to ignore all the NULL properties

4

Answers


  1. Using the @ before your php Variable will work for you like below.

    {{@$file->language->name}} 
    
    Login or Signup to reply.
  2. And also, by using

    {{ $file->language->name ?? '' }} # typical PHP way
    {{ $file->language?->name }} # if object/relation "language" exist then access "name" propery
    

    Note:

    When you use @ to the variable {{@$file->language->name}}, we’re skipping the error message. This means we’re telling Laravel to "Leave this Variable alone". And It’s not the best way to check if empty

    Login or Signup to reply.
  3. There are various ways:

    • Error control operator:

      @$file->language->name the @ operator suppress the errors messages (imho not very clean 🙂 , but same result)

      From documentation:

      // Note: The @-operator works only on expressions. A simple rule of thumb   is: if one can take the value of something, then one can prepend the @ operator to it.
      // For instance, it can be prepended to variables, functions calls, certain language construct calls (e.g. include), and so forth. 
      // It cannot be prepended to function or class definitions, or conditional structures such as if and foreach, and so forth.
      

    • Classical ternary operator way

      $file->language ? $file->language->name : null;

    • From PHP7+ : Null Coalescing Operator

      $file->language->name ?? null;

    • From PHP8+ : null safe Operator

      $file->language?->name; (amazing! :-))

    Login or Signup to reply.
  4. Laravel has a built-in helper for this, called optional().

    https://laravel.com/docs/9.x/helpers#method-optional

    <td>{{ optional($file->language)->name }} </td>
    

    The optional function accepts any argument and allows you to access properties or call methods on that object. If the given object is null, properties and methods will return null instead of causing an error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search