Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 未传递的对象值_Mysql_Laravel_Controller_Laravel Blade - Fatal编程技术网

Mysql 未传递的对象值

Mysql 未传递的对象值,mysql,laravel,controller,laravel-blade,Mysql,Laravel,Controller,Laravel Blade,我正在使用Laravel 5.6,在将数据传递到刀片文件时遇到问题 博客控制器: namespace App\Http\Controllers; use App\Mail\Practice; use App\Mail\Mailable; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use App\Post; use Session; class BlogCon

我正在使用Laravel 5.6,在将数据传递到刀片文件时遇到问题

博客控制器:
namespace App\Http\Controllers;

use App\Mail\Practice;
use App\Mail\Mailable;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Post;
use Session;

class BlogController extends Controller
{
  public function getSingle($slug){
    // Fetch from the DB based on Slug --first stops after one, get pulls everything
  $post = Post::where('slug', '=', $slug)->first();
  print_r($slug);

  // return the view and pass in the post object
  return view('blog.single')->withPost($post);

 }
}
single.blade.php:

@extends('main')

@section('title', "| $post->title")

@section('content')


  <div class="row">
   <div class="col-md-8 col-md-offset-2">
    <h1>{{ $post->title}}</h1>
    <p>{{ $post->body }}</p>
</div>

您的返回语句不正确,您需要更改此行:

return view('blog.single')->withPost($post);
对此,它应该解决您的问题

return view('blog.single')->with('post', $post);

首先,您调试的是slug,而不是post。尝试调试您的帖子,看看是否找到了它。你得到这个错误是因为帖子根本不存在。如果不存在,则中止

if(!$post){
    abort(404);
}

withPost
实际上是Laravel中的一个函数吗?可能尝试
返回视图('blog.single')->使用('post',$post)
,或
返回视图('blog.single',['post'=>$post])。您确实应该实现一些在找不到帖子时处理的功能。这样你就不需要打印了,你就知道有帖子了。我尝试了这两种建议,但仍然拉了一个空数组。它回击“这个集合实例上不存在属性[title]”。在实现了withPost to with('Post',$Post)之后,你能用
打印($Post)检查视图中
$Post
的内容吗;它只是加载一个空的pagelight\Database\elounce\Collection对象([items:protected]=>Array())这就是它的回击尝试:
Post::where('slug',$slug)->first()而不是
Post::where('slug','=',$slug)->first()
if(!$post){
    abort(404);
}