我在这里有一个简单的博客网络应用程序。问题是当我想在数据库中输出博客类别时。我成功保存在数据库中,并在使用return语句进行测试时对其进行了显示。但是,当我尝试将输出放在视图类别中时,将不会在视图页面上显示。当我在刀片视图中使用
博客应用程序还具有一个主页,其中显示了预览,以便能够显示完整的博客条目,您必须在该主页上列出的博客条目上单击“查看”。
发生错误的刀片
<input type="file" id="image-input" accept="image/*">
<input type="text" id="top-text" />
<input type="text" id="bottom-text" />
<canvas style="" id="meme-canvas"></canvas>
路线 //web.php Auth :: routes();
\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
控制器
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;
}
}
答案 0 :(得分: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()
从模型中获取类别。立即循环浏览类别。
答案 1 :(得分:0)
<li>
<a href='{{url("category/{$category->id}")}}'>
{{ $category->$category }}</a>
</li>
如果属性$category->$category
是数据库中类别的名称,我想$category->category
应该是category
。
答案 2 :(得分:0)
尝试在post / view.blade.php中的$ categories-> all()中删除“ all()”