1 year ago

how to set up published_at form in laravel 5.8

Posted 1 year ago by forteirp

I have an issue with the published_at showing up when the form is submitted.

So I can layout the view for the form

<div class="form-group">
      {!! Form::label('published_at', 'Publish On:') !!}
      {!! Form::date('published_at', (isset($post) && $post->published_at ? $post->published_at : date('Y-m-d')), ['class' => 'form-control', 'style'=> 'line-height: 19px']) !!}

But when I submit the data is null... As seen here:

As I'm typing this post I'm asking myself, "do I need to add the published_at to the validate and make it a "$post->published_at = $request->published_at;" on the store function?" but I'm not sure, that why I'm asking the question to the discussion?

This is what I have right now in the store function...

public function store(Request $request)
        $this->validate($request, array(
            'title'         => 'required|max:255',
            'slug'          => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
            'category_id'   => 'required|integer',
            'body'          => 'required'
        //create Post
        $post = new Post;
        $post->title = $request->title;
        $post->slug = $request->slug;
        $post->category_id = $request->category_id;
        $post->excerpt = str_limit($request->body, 100);
        $post->body = $request->body;
        $post->author_id = auth()->user()->id;
        return redirect('/posts')->with('success', 'Your post created created successfully');

I also have a protected $dates = ['published_at']; in my Post model, but Im not sure if this is causing my error?

Here's the Post model just in case you need to see it?


namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;

class Post extends Model
    //Table Name
    protected $table = 'posts';
    // Primary Key
    public $primaryKey = 'id';
    // Timestamps
    public $timestamps = true;
    protected $fillable = [
    protected $dates = ['published_at'];

public function author()
        return $this->belongsTo(User::class);

public function category()
        return $this->belongsTo(Category::class);

public function getImageUrlAttribute($value)
    $imageUrl = "";
    if( ! is_null($this->image))
        $imagePath = public_path() . "/img/" . $this->image;
        if(file_exists($imagePath)) $imageUrl = asset("img/" . $this->image);
    return $imageUrl;
public function getDateAttribute()
        return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();    

public function getExcerptHtmlAttribute()
        return $this->excerpt ? Markdown::convertToHtml(e($this->excerpt)) : NULL;

public function getBodyHtmlAttribute()
        return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL;

public function scopeLatestFirst($query)
        return $query->orderBy('published_at', 'desc');

public function scopePublished($query)
        return $query->where('published_at', '<=', Carbon::now());


Any help would be helpful.

Please sign in or create an account to participate in this conversation.