首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ErrorException未定义变量: posts - $posts未定义

ErrorException未定义变量: posts - $posts未定义
EN

Stack Overflow用户
提问于 2020-12-14 20:13:44
回答 1查看 206关注 0票数 0

我收到一个错误,当点击管理区域的帖子链接。错误消息指出问题出在以下代码行中:

代码语言:javascript
复制
@foreach($posts as $post)

不确定问题出在哪里。

index.blade.php

代码语言:javascript
复制
@extends('layouts.app')

@section('content')


            <div class="d-flex justify-content-left">

                <a href="{{ route('posts.create')}}" class="btn btn-success float-right">Add post</a>

                </div>

                <div class="card card-default">
                    <div class="card-header">Posts</div>

                    <div class="card-body">
                        <table class="table">
                            <thead>
                                <th>Image</th>
                                <th>Title</th>
                            </thead>
                            <tbody>
                                @foreach($posts as $post)
                                    <tr>
                                        <td>
                                            image
                                        </td>
                                        <td>
                                            {{ $post->title }}
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
    
@endsection

PostController.php

代码语言:javascript
复制
<?php

namespace App\Http\Controllers;

use App\Http\Requests\Posts\CreatePostRequest;
use Illuminate\Http\Request;
use App\Models\Post;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('posts.index')->with('posts', Post::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('posts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreatePostRequest $request)
    {
        //upload image to storage
        $image = $request->image->store('posts');
        //create the post
        Post::create([
            'title' => $request->title,
            'description' => $request->description,
            'content' => $request->content,
            'image' => $image
        ]);
        //flash message
        session()->flash('success', 'Post created succesfully.');
        //redirect user
        return redirect(route('posts.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-14 20:17:55

你也应该带上变量,你的代码应该是这样的:

代码语言:javascript
复制
$posts = Post::all();

return view ('posts.index', compact('posts'));
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65288861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档