Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel crud show on empy

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    @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

>Solution :

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

{{@$file->language->name}} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading