Php 对null上的成员函数comments()的调用

Php 对null上的成员函数comments()的调用,php,laravel,Php,Laravel,基本上,我想解决这个错误 对null上的成员函数comments()的调用 我正在尝试在我的网站上建立一个系统,用户可以发布、查看帖子评论和回复 当用户对http://127.0.0.1:8000/post/show/6例如,它意味着转到http://127.0.0.1:8000/comment/store将注释插入数据库,然后显示注释 但是 目前的情况是,在http://127.0.0.1:8000/post/show/6它指向http://127.0.0.1:8000/comment/sto

基本上,我想解决这个错误

对null上的成员函数comments()的调用

我正在尝试在我的网站上建立一个系统,用户可以发布、查看帖子评论和回复

当用户对
http://127.0.0.1:8000/post/show/6
例如,它意味着转到
http://127.0.0.1:8000/comment/store
将注释插入数据库,然后显示注释

但是 目前的情况是,在
http://127.0.0.1:8000/post/show/6
它指向
http://127.0.0.1:8000/comment/store
它在laravel的PrettyPageHandler上显示此错误:

对null上的成员函数comments()的调用

我不知道我做错了什么

请帮忙

以下是我的代码:

PostController.php

    <?php

// PostController.php

namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;

class PostController extends Controller
{
    protected $fillable = [
        'Uploader',
        ];

    public function __construct()
    {
        return $this->middleware('auth');
    }

    public function create()
    {
        return view('post');
    }

    public function store(Request $request)
    {
        {


            $post =  new Post;

            $post->title = $request->get('title');
            $post->type = $request->get('type');
            $post->description = $request->get('description');
            $post->body = $request->get('body');
            $post->UniqueId = str_random(16);
            $post->Uploader = Auth::user()->name;
            $post->Language = 'en';
            $post->Location=Location::get()->countryCode;            
            $post->views = 0;
            $post->Applauds = 0;
            $post->Boos = 0;
            $post->Tags =  "hey";

                         if ($request->get('agegroup')) {
                            $post->agegroup = $request->get('agegroup');
                         } else {
                            $post->agegroup ='undefined';
                         }

            $post->AllowComments = 'true';
            $post->CommentsBg = 'default';
            $post->Visibility = 'globally public';
            $post->others = 'embeddable';


            $post->save();

            return redirect('posts');

        }
    }

   public function index()
   {
    $posts = Post::all();

    return view('index', compact('posts'));
   }

   public function show($id)
   {
    $post = Post::find($id);

    return view('show', compact('post'));
   }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->get('comment_body');
        $comment->user()->associate($request->user());
        $post = Post::find($request->get('post_id'));
        $post->comments()->save($comment);

        return back();
    }
}
/*
|--------------------------------------------------------------------------
| 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('welcome');
});

Auth::routes();


Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');

Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');

Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');

Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Post</div>
                <div class="card-body">
                    <form method="post" action="{{ route('post.store') }}">
                        <div class="form-group">
                            @csrf
                            <label class="label">Post Title: </label>
                            <input type="text" name="title" class="form-control" required/>
                        </div>
                        <label class="label">Post Type </label>
                        <input type="text" name="type" class="form-control" required/>

                         <label class="label">Tags </label>
                        <input type="text" name="tags" class="form-control" required/>

                        <label class="label">Age-group(optional) </label>
                        <input type="text" name="agegroup" class="form-control" required/>


                        <div class="form-group">
                            <label class="label">Post Description </label>
                            <textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
                        </div>

                        <div class="form-group">
                            <label class="label">Post Body: </label>
                            <textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-success" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidden" name="post_id" value="{{ $post->id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->comment_body;
        $comment->user()->associate($request->user());
        $post = Post::find($request->post_id);
        $post->comments()->save($comment);

        return back();
    }
}
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidde" name="post_id" value="{{ $post->Id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
post.blade.php

    <?php

// PostController.php

namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;

class PostController extends Controller
{
    protected $fillable = [
        'Uploader',
        ];

    public function __construct()
    {
        return $this->middleware('auth');
    }

    public function create()
    {
        return view('post');
    }

    public function store(Request $request)
    {
        {


            $post =  new Post;

            $post->title = $request->get('title');
            $post->type = $request->get('type');
            $post->description = $request->get('description');
            $post->body = $request->get('body');
            $post->UniqueId = str_random(16);
            $post->Uploader = Auth::user()->name;
            $post->Language = 'en';
            $post->Location=Location::get()->countryCode;            
            $post->views = 0;
            $post->Applauds = 0;
            $post->Boos = 0;
            $post->Tags =  "hey";

                         if ($request->get('agegroup')) {
                            $post->agegroup = $request->get('agegroup');
                         } else {
                            $post->agegroup ='undefined';
                         }

            $post->AllowComments = 'true';
            $post->CommentsBg = 'default';
            $post->Visibility = 'globally public';
            $post->others = 'embeddable';


            $post->save();

            return redirect('posts');

        }
    }

   public function index()
   {
    $posts = Post::all();

    return view('index', compact('posts'));
   }

   public function show($id)
   {
    $post = Post::find($id);

    return view('show', compact('post'));
   }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->get('comment_body');
        $comment->user()->associate($request->user());
        $post = Post::find($request->get('post_id'));
        $post->comments()->save($comment);

        return back();
    }
}
/*
|--------------------------------------------------------------------------
| 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('welcome');
});

Auth::routes();


Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');

Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');

Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');

Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Post</div>
                <div class="card-body">
                    <form method="post" action="{{ route('post.store') }}">
                        <div class="form-group">
                            @csrf
                            <label class="label">Post Title: </label>
                            <input type="text" name="title" class="form-control" required/>
                        </div>
                        <label class="label">Post Type </label>
                        <input type="text" name="type" class="form-control" required/>

                         <label class="label">Tags </label>
                        <input type="text" name="tags" class="form-control" required/>

                        <label class="label">Age-group(optional) </label>
                        <input type="text" name="agegroup" class="form-control" required/>


                        <div class="form-group">
                            <label class="label">Post Description </label>
                            <textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
                        </div>

                        <div class="form-group">
                            <label class="label">Post Body: </label>
                            <textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-success" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidden" name="post_id" value="{{ $post->id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->comment_body;
        $comment->user()->associate($request->user());
        $post = Post::find($request->post_id);
        $post->comments()->save($comment);

        return back();
    }
}
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidde" name="post_id" value="{{ $post->Id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
@extends('layouts.app'))
@节(“内容”)
创建帖子
@csrf
职位名称:
职位类型
标签
年龄组(可选)
职位描述
邮政机构:
@端部
show.blade.php

    <?php

// PostController.php

namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;

class PostController extends Controller
{
    protected $fillable = [
        'Uploader',
        ];

    public function __construct()
    {
        return $this->middleware('auth');
    }

    public function create()
    {
        return view('post');
    }

    public function store(Request $request)
    {
        {


            $post =  new Post;

            $post->title = $request->get('title');
            $post->type = $request->get('type');
            $post->description = $request->get('description');
            $post->body = $request->get('body');
            $post->UniqueId = str_random(16);
            $post->Uploader = Auth::user()->name;
            $post->Language = 'en';
            $post->Location=Location::get()->countryCode;            
            $post->views = 0;
            $post->Applauds = 0;
            $post->Boos = 0;
            $post->Tags =  "hey";

                         if ($request->get('agegroup')) {
                            $post->agegroup = $request->get('agegroup');
                         } else {
                            $post->agegroup ='undefined';
                         }

            $post->AllowComments = 'true';
            $post->CommentsBg = 'default';
            $post->Visibility = 'globally public';
            $post->others = 'embeddable';


            $post->save();

            return redirect('posts');

        }
    }

   public function index()
   {
    $posts = Post::all();

    return view('index', compact('posts'));
   }

   public function show($id)
   {
    $post = Post::find($id);

    return view('show', compact('post'));
   }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->get('comment_body');
        $comment->user()->associate($request->user());
        $post = Post::find($request->get('post_id'));
        $post->comments()->save($comment);

        return back();
    }
}
/*
|--------------------------------------------------------------------------
| 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('welcome');
});

Auth::routes();


Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');

Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');

Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');

Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Post</div>
                <div class="card-body">
                    <form method="post" action="{{ route('post.store') }}">
                        <div class="form-group">
                            @csrf
                            <label class="label">Post Title: </label>
                            <input type="text" name="title" class="form-control" required/>
                        </div>
                        <label class="label">Post Type </label>
                        <input type="text" name="type" class="form-control" required/>

                         <label class="label">Tags </label>
                        <input type="text" name="tags" class="form-control" required/>

                        <label class="label">Age-group(optional) </label>
                        <input type="text" name="agegroup" class="form-control" required/>


                        <div class="form-group">
                            <label class="label">Post Description </label>
                            <textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
                        </div>

                        <div class="form-group">
                            <label class="label">Post Body: </label>
                            <textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-success" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidden" name="post_id" value="{{ $post->id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->comment_body;
        $comment->user()->associate($request->user());
        $post = Post::find($request->post_id);
        $post->comments()->save($comment);

        return back();
    }
}
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidde" name="post_id" value="{{ $post->Id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@扩展('layouts.app')
@节(“内容”)
{{$post->title}

{{$post->body}


显示评论 @foreach($post->comments as$comment) {{$comment->user->name}
/*
|--------------------------------------------------------------------------
| 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('welcome');
});

Auth::routes();


Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');

Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');

Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');

Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
{{$comment->body}

@endforeach
添加注释 @csrf @端部
index.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <table class="table table-striped">
                <thead>
                    <th>ID</th>
                    <th>Title</th>
                    <th>Action</th>
                </thead>
                <tbody>
                @foreach($posts as $post)
                <tr>
                    <td>{{ $post->Id }}</td>
                    <td>{{ $post->title }}</td>
                    <td>
                        <a href="{{ route('post.show', ['id' => $post]) }}" class="btn btn-primary">Show Post</a>
                    </td>
                </tr>
                @endforeach
                </tbody>

            </table>
        </div>
    </div>
</div>
@endsection
@extends('layouts.app'))
@节(“内容”)
身份证件
标题
行动
@foreach($posts作为$post)
{{$post->Id}
{{$post->title}
@endforeach
@端部

我想问题出在CommentController.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('welcome');
});

Auth::routes();


Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');

Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');

Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');

Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
$post = Post::find($request->get('post_id'));
$post->comments()->save($comment);
您的假设是在第一行
Post::find()
返回一个新的Post对象。显然不是。为什么?我不知道,但可能是因为身份证不存在

您可以通过执行以下操作来检查此问题:

$post = Post::find($request->get('post_id'));
if (!is_object($post)) echo "Yeah, I really have a problem here...";
$post->comments()->save($comment);

您好,这是因为您没有正确检索输入

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->comment_body;
        $comment->user()->associate($request->user());
        $post = Post::find($request->post_id);
        $post->comments()->save($comment);

        return back();
    }
}
不要使用
$request->get('field\u name')
只需使用
$request->field\u name

编辑注释:

/*
|--------------------------------------------------------------------------
| 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('welcome');
});

Auth::routes();


Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');

Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');

Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');

Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
您的表单方法是post。。如果该函数存在,您还可以使用
$request->post('field\u name')
。。有很多方法可以检索输入,但正如我在回答中所写,我使用了
$request->field\u name


为什么会出现此错误是因为Post::find(null/undefined)返回null..

您会出现此错误,因为执行此操作时:

$post = Post::find($request->get('post_id'));
您找不到任何内容,因此$post为空。因此,你应该找出原因。尝试调试并查看$request->get('post_id')包含的内容。也许它包含了错误的帖子id,或者里面什么都没有

如果是这样,我想答案是:

$post = Post::find($request->input('post_id'));
问候

我明白了

我必须将
post_id
更改为post_id`,反之亦然,如下所示:

CommentController.php

    <?php

// PostController.php

namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;

class PostController extends Controller
{
    protected $fillable = [
        'Uploader',
        ];

    public function __construct()
    {
        return $this->middleware('auth');
    }

    public function create()
    {
        return view('post');
    }

    public function store(Request $request)
    {
        {


            $post =  new Post;

            $post->title = $request->get('title');
            $post->type = $request->get('type');
            $post->description = $request->get('description');
            $post->body = $request->get('body');
            $post->UniqueId = str_random(16);
            $post->Uploader = Auth::user()->name;
            $post->Language = 'en';
            $post->Location=Location::get()->countryCode;            
            $post->views = 0;
            $post->Applauds = 0;
            $post->Boos = 0;
            $post->Tags =  "hey";

                         if ($request->get('agegroup')) {
                            $post->agegroup = $request->get('agegroup');
                         } else {
                            $post->agegroup ='undefined';
                         }

            $post->AllowComments = 'true';
            $post->CommentsBg = 'default';
            $post->Visibility = 'globally public';
            $post->others = 'embeddable';


            $post->save();

            return redirect('posts');

        }
    }

   public function index()
   {
    $posts = Post::all();

    return view('index', compact('posts'));
   }

   public function show($id)
   {
    $post = Post::find($id);

    return view('show', compact('post'));
   }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->get('comment_body');
        $comment->user()->associate($request->user());
        $post = Post::find($request->get('post_id'));
        $post->comments()->save($comment);

        return back();
    }
}
/*
|--------------------------------------------------------------------------
| 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('welcome');
});

Auth::routes();


Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');

Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');

Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');

Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Post</div>
                <div class="card-body">
                    <form method="post" action="{{ route('post.store') }}">
                        <div class="form-group">
                            @csrf
                            <label class="label">Post Title: </label>
                            <input type="text" name="title" class="form-control" required/>
                        </div>
                        <label class="label">Post Type </label>
                        <input type="text" name="type" class="form-control" required/>

                         <label class="label">Tags </label>
                        <input type="text" name="tags" class="form-control" required/>

                        <label class="label">Age-group(optional) </label>
                        <input type="text" name="agegroup" class="form-control" required/>


                        <div class="form-group">
                            <label class="label">Post Description </label>
                            <textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
                        </div>

                        <div class="form-group">
                            <label class="label">Post Body: </label>
                            <textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-success" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidden" name="post_id" value="{{ $post->id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;
        $comment->body = $request->comment_body;
        $comment->user()->associate($request->user());
        $post = Post::find($request->post_id);
        $post->comments()->save($comment);

        return back();
    }
}
<!-- show.blade.php -->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <p><b>{{ $post->title }}</b></p>
                    <p>
                        {{ $post->body }}
                    </p>
                    <hr />
                    <h4>Display Comments</h4>
                    @foreach($post->comments as $comment)
                        <div class="display-comment">
                            <strong>{{ $comment->user->name }}</strong>
                            <p>{{ $comment->body }}</p>
                        </div>
                    @endforeach
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="{{ route('comment.add') }}">
                        @csrf
                        <div class="form-group">
                            <input type="text" name="comment_body" class="form-control" />
                            <input type="hidde" name="post_id" value="{{ $post->Id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-warning" value="Add Comment" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

在这段代码中,
$post=post::find($request->get('post_id')对于post id,post似乎不存在,因此它将返回null。如果父项为空,则无法获取父项的子项。我的答案也适用于您的post controller。。