Laravel 5.4:查询返回空值或空

时间:2018-11-18 11:46:01

标签: php laravel laravel-5.4

我这里有一个简单的博客应用程序,用户可以在其中添加/编辑/删除帖子;喜欢不喜欢的帖子,并查看帖子的类别排列。这里不起作用的是当我尝试检查查询是否会返回某些内容,但仅返回空数组/空值

当我尝试检查是否单击某个类别时,帖子控制器的类别功能未返回任何值。

这也是单击我想要的类别后URL上的示例输出,但是它在页面[[]]上显示空白值,但是我猜该URL提供了一些线索,但我不知道自己该怎么做。 http://localhost:8000/category/%7B%20%7B%22id%22:4,%22category%22:%22animals%22,%22created_at%22:%222018-07-09%2009:54:49%22,%22updated_at%22:%222018-07-09%2009:54:49%22%7D-%3E%20id%7D

后控制器     

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;

use DB;

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){

        $posts = Post::where('id', '=', $post_id)->get();
        $categories = Category::all();


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






    }
    public function edit($post_id){
        //return $post_id;
        $categories = Category::all();
        $posts = Post::find($post_id);
        $category = Category::find($posts->category_id);
        //return $posts;

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

    }
    public function editPost(Request $request, $post_id){
        $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;
        $data = array(
            'post_title' => $posts->post_title,
            'user_id' => $posts->user_id,
            'post_body' => $posts->post_body,
            'category_id' => $posts->category_id,
            'post_image' => $posts->post_image
        );
        Post::where('id', $post_id)
        ->update($data);
        $posts->update();

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

    }
    public  function deletePost($post_id){

        Post::where('id', $post_id)
        ->delete();
         return redirect('/home')->with('response','Post Deleted Successfully');

    }
    public function category($cat_id){

        $categories = Category::all();
        $posts = DB::table('posts')
            ->join('categories', 'posts.category_id', '=', 'categories.id')
            ->select('posts.*', 'categories.*')
            ->where(['categories.id' => $cat_id])
            ->get(); 
        return $posts;
        exit();
        return view('categories.categoriesposts', ['categories' => $categories]);
    }
}

观看次数

view.blade在此处是您选择特定帖子时在博客的索引页面之后的位置,您可以在右侧看到类别,在其中可以选择所需的类别以及与之相关的帖子。

@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 text-center">
                <div class="panel-heading">Post View</div>

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

意见: categorypost.blade

@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 text-center">
                <div class="panel-heading">Post View</div>

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




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


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

路线

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('auth.login');
});

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');
Route::get('/edit/{id}', 'PostController@edit');
Route::post('/editPost/{id}', 'PostController@editPost');
Route::get('/delete/{id}','PostController@deletePost');
Route::get('/category/{id}','PostController@category');

Home.blade 这是博客的索引页面,您可以看到博客文章的概述,可以在此处删除和编辑文章

  @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

                        {{ $posts->links() }}

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

0 个答案:

没有答案