Laravel 5.4: 无法在视图中显示 crud 内容

Laravel 5.4: Unable to show crud contents in views

我这里有一个简单的博客网络应用程序。问题是当我想在数据库中输出博客类别时。我成功地保存在数据库中,并在我使用 return 语句进行测试时显示了它。但是当我试图将输出放在视图上时,类别不会显示在视图页面上。当我在 blade 视图中使用

时,它只显示空白项目符号。此外,当我在数据库的类别上添加一些内容并刷新页面时,另一个项目符号将添加到我的视图中。

博客应用程序还有一个主页,其中显示了预览以便能够显示完整的博客条目,您必须单击查看主页上列出的博客条目。

blade 发生不对劲的地方

\posts\view.blade.php
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">

            <div class="panel panel-default">
                <div class="panel-heading">Post View</div>

                <div class="panel-body">
                    <div class="col-md-4">
                        <ul>
                            @if(count($categories) > 0)
                                @foreach($categories->all() as $category)
                                    <li>
                                        <a href='{{url("category/{$category->id}")}}'>
                                        {{ $category->$category  }}</a>
                                    </li>
                                @endforeach
                            @else
                                <p>No category found!</p>
                            @endif
                        </ul>
                        <ul> adsfafs</ul>




                    </div>
                    <div class="col-md-8">


                    </div>                    
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

路线 //web.php Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/post', 'PostController@post');
Route::get('/profile', 'ProfileController@profile');
Route::get('/category', 'CategoryController@category');
Route::post('/addCategory', 'CategoryController@addCategory');
Route::post('/addProfile', 'ProfileController@addProfile');
Route::post('/addPost', 'PostController@addPost');

Route::get('/view/{id}', 'PostController@view');

控制器

//PostController.php
<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use App\Category;
use App\Post;
use Auth;

class PostController extends Controller
{
    //
    public function post(){
        $categories = Category::all();
        $posts= Post::all();
        //return $posts;
        //exit(); pang testing

        return view('posts.post', ['categories'=> $categories, 'posts'=>$posts]);
    }

    public function addPost(Request $request){
        //return $request->input('post_title');
        $this->validate($request,[
            'post_title'=> 'required',
            'post_body'=> 'required',
            'category_id'=> 'required',
            'post_image'=> 'required',

        ]);
        //return "validation pss";
        $posts = new Post;
        $posts->post_title=$request->input('post_title');
        $posts->user_id= Auth::user()->id; //returns user details
        $posts->post_body=$request->input('post_body');
        $posts->category_id=$request->input('category_id');

        if(Input::hasFile('post_image')){
            $file = Input::file('post_image');
            $file -> move(public_path(). '/posts/', $file->getClientOriginalName());
            $url = URL::to("/") . '/posts/' . $file->getClientOriginalName();

        }
        $posts->post_image= $url;
        $posts->save();

        return redirect('/home')->with('response','Post Published Successfully');

    }

    public function view($post_id){
        $post = Post::where('id', '=', $post_id)->get();
        $categories = Category::all();

        return view('posts.view', ['post'=>$post, 'categories'=>$categories ]);

    }
    public function edit($post_id){
        return $post_id;

    }
}

首页

@extends('layouts.app')
<style type="text/css">
    .avatar{
        border-radius: 100%;
        max-width: 100px;
    }
</style>
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            @if(count($errors)>0)
                        @foreach($errors->all() as $error)
                            <div class="alert alert-danger">{{ $error }}</div>
                        @endforeach

            @endif

                    @if(session('response'))
                        <div class="alert alert-success">{{  session('response')}}</div>

                    @endif
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    <div class="col-md-4">
                        @if(!empty($profile))
                        <img src="{{ $profile->profile_pic }}" class="avatar" alt="">

                        @else
                        <img src="{{ url('images/avatar.jpg')}}" class="avatar" alt="">
                        @endif

                        @if(!empty($profile))
                        <p class="lead">{{ $profile->name }}</p>

                        @else
                        <p></p>
                        @endif

                        @if(!empty($profile))
                        <p class="lead">{{ $profile->designation }}</p>

                        @else
                        <p></p>
                        @endif



                    </div>
                    <div class="col-md-8">
                        @if(count($posts) > 0)
                            @foreach($posts->all() as $post)
                                <h4>{{ $post -> post_title }} </h4>
                                <img src="{{ $post -> post_image }}" alt="">
                                <p>{{ substr($post->post_body, 0, 150) }}</p>

                                <ul class="nav nav-pills">
                                    <li role="presentation">
                                        <a href="{{ url("/view/{$post->id}") }}">
                                            <span class="fa fa-eye">View</span>
                                        </a>
                                    </li>
                                    <li role="presentation">
                                        <a href="{{ url("/edit/{$post->id}") }}">
                                            <span class="fa fa-pencil-square">Edit</span>
                                        </a>
                                    </li>
                                    <li role="presentation">
                                        <a href="{{ url("/delete/{$post->id}") }}">
                                            <span class="fa fa-trash">Delete</span>
                                        </a>
                                    </li>
                                </ul>

                                <cite style="float left;">Posted on: {{ date('M j, Y H:i', strtotime($post->updated_at)) }}</cite>
                            @endforeach

                        @else
                            <p>No Post Available!</p> 
                        @endif

                    </div>                    
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<li>
    <a href='{{url("category/{$category->id}")}}'>
    {{ $category->$category  }}</a>
</li>

我猜$category->$category应该是$category->category,如果属性 category是数据库中的类别名称。

尝试删除 post/view 中的 $categories->all() 中的 "all()"。blade.php

编辑这部分:

 @foreach($categories->all() as $category)
   <li>
     <a href='{{url("category/{$category->id}")}}'>
        {{ $category->$category  }}</a>
   </li>
 @endforeach

替换为:

  @foreach($categories as $category)
   <li>
     <a href='{{url("category/{$category->id}")}}'>
        {{ $category  }}</a>
   </li>
 @endforeach

您曾在控制器中使用 all() 从模型中获取类别。现在只需循环浏览类别即可。