dongsuishou8039 2017-04-23 18:47
浏览 664
已采纳

为什么我一直在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??

  • 写回答

3条回答 默认 最新

  • duai3681 2017-04-27 18:32
    关注

    It works now by adding:

    @if(isset($ownedCharities))
    

    Before the foreach loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题