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

Posted

技术标签:

【中文标题】Laravel 5.4:无法在视图中显示 crud 内容【英文标题】:Laravel 5.4: Unable to show crud contents in views 【发布时间】:2018-09-22 12:43:49 【问题描述】:

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

在我的刀片视图中。此外,当我在数据库中添加有关类别的内容并刷新页面时,另一个项目符号将添加到我的视图中。

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

发生不正确的事情的刀片

\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 验证::路由();

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

                        @else
                        <img src=" url('images/avatar.jpg')" class="avatar" >
                        @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 " >
                                <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

【问题讨论】:

【参考方案1】:

编辑此部分:

 @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() 从模型中获取类别。现在只需遍历类别即可。

【讨论】:

【参考方案2】:

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

【讨论】:

【参考方案3】:
<li>
    <a href='url("category/$category->id")'>
     $category->$category  </a>
</li>

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

【讨论】:

以上是关于Laravel 5.4:无法在视图中显示 crud 内容的主要内容,如果未能解决你的问题,请参考以下文章

在 Laravel 5.4 的路由中使用 DELETE 方法

Laravel 5.4 Backpack 无法删除项目,返回“403 Forbidden”错误

Laravel 5.4:无法从关系中检索数据

Laravel 5.4 - 表单/API/视图

Angular 4 和 Laravel 5.4 的刷新路由问题

Vue2 在 Laravel 5.4 中没有变化