猿问

为 foreach() Facade\Ignition\Exceptions\

@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\ViewException

Invalid 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>

这是我的控制器


<?php


namespace 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>

让我知道我是否应该发布更多代码。


森栏
浏览 125回答 2
2回答

qq_笑_17

在您的路线中:&nbsp; &nbsp; Route::get('/about', function () {&nbsp; &nbsp; &nbsp; &nbsp; $articles = App\Article::latest()->get();&nbsp; &nbsp; &nbsp; &nbsp; return view('about', compact('about', 'articles'));&nbsp; &nbsp; });它将返回一个集合,您可以循环它:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($articles as $article)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class = "first">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<h3>{{ $article->title}}</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p><a href="#">{{ $article->excerpt }}</a></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>

墨色风雨

更新你的观点&nbsp;<? if (is_array($articles) || is_object($articles)) ?>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($articles as $article)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class = "first">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>{{ $article->title}}</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><a href="#">{{ $article->excerpt }}</a></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </ul>错误在你的 foreach 循环中:https ://www.php.net/manual/en/control-structures.foreach.php
随时随地看视频慕课网APP
我要回答