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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器