laravel date_format() 期望参数 1 为 DateTimeInterface,给定 null(视图:/home/vagrant/code/blog/resources/views/singlePost.blade.php)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50059101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:40:53  来源:igfitidea点击:

date_format() expects parameter 1 to be DateTimeInterface, null given (View: /home/vagrant/code/blog/resources/views/singlePost.blade.php)

phplaravel

提问by mariona zahra

I cant find whats wrong with my code. I have tried looking at the whole code but I can't put my finger on it. It would be really helpful if someone pointed out my mistake. It is saying that something is wrong with the date but I cant see what I typed wrong.

我找不到我的代码有什么问题。我曾尝试查看整个代码,但我无法理解。如果有人指出我的错误,那将非常有帮助。它说日期有问题,但我看不到我输入的错误。

@extends('layouts.master')
@section('content')

    <!-- Page Header -->
    <header class="masthead" style="background-image: url('{{asset('assets/img/post-bg.jpg')}}')">
      <div class="overlay"></div>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <div class="post-heading">
              <h1>{{$post->title}}</h1>
              <span class="meta">Posted by
                <a href="#">{{$post->user->name}}</a>
                on {{date_format($post->created_at,'F d,Y')}}</span>
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- Post Content -->
    <article>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            {!!nl2br($post->content)!!}
          </div>
        </div>
<div class="comments">
  <hr>
  <h2>Comments</h2>
  <hr>
  @foreach($post->comments as $comment)
  <p>{{$comment->content}} <br>
  <p><small>by {{$comment->user->name}}, on {{date_format($comment->created_at,'F d,Y')}}</small></p>
  <hr>
  @endforeach

@if(Auth::check())
<form action="{{route('newComment')}}" method="POST">
  @csrf
  <div class="form-group">
<textarea class="form-control" placeholder="Comment..." name="comment" id="" cols="30" rows="4"></textarea>
<input type="hidden" name="post" value="{{$post->id}}">
</div>
<div class="form-group">
  <button class ="btn btn-primary" type="submit">Make Comment</button>
</div>
</form>
@endif

</div>
</article>
    @endsection


comments.blade.php code

回答by Niklesh Raut

Error show you must have DateTimeobject

错误显示您必须有DateTime对象

  $created_at = new DateTime("2018-02-01");
  echo date_format($created_at,'F d,Y');

Live Demo

现场演示

If you have just date like 2018-02-01Then you can use simple date()function

如果你只有日期,2018-02-01那么你可以使用简单的date()功能

$created_at = "2018-02-01";
echo date('F d,Y',strtotime($created_at));

Live Demo

现场演示

You can also do it by

你也可以通过

echo (new DateTime("2018-02-01"))->format('F d,Y');

Demo

演示