为什么我一直在Laravel 5.4中获得'Undefined variable:charities'?

I keep getting this undefined variable error. I am trying to display all the charities to the users, I am doing the exact same function for the 'charityowners' type of user and it is working for them.

Route:

Route::get('charities', 'HomeController@displayCharities');

Home Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use Image;
use App\Charities;

class HomeController extends Controller
{
    public function displayCharities()
    {
    $charities = Charities::all();

    return view('charities', ['charities' => $charities]);
    }

}

View:

<table class="table">

<tr>

    <td> <strong> Name </strong> </td>
    <td> <strong> Image </strong> </td>
    <td> <strong> Description </strong> </td>
    <td> <strong> Action </strong> </td>

</tr>

@foreach($charities as $charity)

<tr>

    <td> {{ $charity->name }} </td>
    <td> Not Available </td>
    <td> {{ $charity->description }} </td>

</tr>

@endforeach

</table>

The error I keep getting is:

ErrorException in d9249250b321fd33ae875d6ca2417a0420928492.php line 39: Undefined variable: charities (View: C:\wamp64\www\EasyDonationNew\laravelesources\views\charities.blade.php)

What am I missing??

It works now by adding:

@if(isset($ownedCharities))

Before the foreach loop.

Try initializing the variable $charities globally as an empty string or as null.

On the global scope a var is only seen if it is defined.

You can try this

return view('charities')->with('charities', $charities);