亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為 foreach() Facade\Ignition\Exceptions\

為 foreach() Facade\Ignition\Exceptions\

PHP
森欄 2022-07-16 11:01:16
@foreach沒有語句,代碼運行良好。添加后,我收到以下錯誤:Undefined variable: articles (View: C:\laravel\laravel\resources\views\about.blade.php)$articles is undefined.Make the variable optional in the blade template. Replace {{ $articles }} with {{ $articles ?? '' }}當我添加?? ''它時,它會產生錯誤:Facade\Ignition\Exceptions\ViewExceptionInvalid argument supplied for foreach()這是更大膽的完整代碼: https ://github.com/matthoek/Articles這是我的看法:        <? if (is_array($articles) || is_object($articles)) ?>            {                @foreach ($articles ?? '' as $article)                {                    <li class = "first">                        <h3>{{ $article->title}}</h3>                        <p><a href="#">{{ $article->excerpt }}</a></p>                              </li>                @endforeach                }                       }        </ul>這是我的控制器<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use App\Article;class ArticlesController extends Controller{    public function index()    {        $articles = App/Article::latest()->get();        //$articles = Review::all();        //$article = Review::all();        return view('articles.index',['articles'=>$articles]);        //return view('articles.index', ['articles' => $articles]);    }    public function show($id)    {        $article = App/Article::find($id);        //return view('articles.show', ['article' => $article]);        return view('articles.show')->with('article');    }    public function create()    {        return view('articles.create');    }將路由文件的部分更改為以下內容,但似乎不起作用。 return view('about')->with(['article'=> $article]);我已確保以下內容在我app/Http/Kernel.php的middlewareGroups財產中web \Illuminate\View\Middleware\ShareErrorsFromSession::class,我已將 foreach 循環更改為 each 循環,并且在線上出現相同的錯誤:<h3>{{article->title}}</h3>讓我知道我是否應該發布更多代碼。
查看完整描述

2 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

在您的路線中:


    Route::get('/about', function () {

        $articles = App\Article::latest()->get();

        return view('about', compact('about', 'articles'));

    });

它將返回一個集合,您可以循環它:


             <ul>

                @foreach ($articles as $article)

                <li class = "first">

                   <h3>{{ $article->title}}</h3>

                   <p><a href="#">{{ $article->excerpt }}</a></p>          

                </li>

                @endforeach

              </ul>


查看完整回答
反對 回復 2022-07-16
?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

更新你的觀點


 <? if (is_array($articles) || is_object($articles)) ?>

        {

            @foreach($articles as $article)


                <li class = "first">

                    <h3>{{ $article->title}}</h3>

                    <p><a href="#">{{ $article->excerpt }}</a></p>          

                </li>

            @endforeach   

        }

    </ul>

錯誤在你的 foreach 循環中:https ://www.php.net/manual/en/control-structures.foreach.php


查看完整回答
反對 回復 2022-07-16
  • 2 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號