Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

4xjbh's avatar

Fundamental Date Problem

I have just finished going through the fundamental series with no problems I could not resolve myself until I hit the cleanup episode.

After changing the 'published_at' input etc shown I am getting a 'MethodNotAllowedHttpException in compiled.php line 7907:' The page displays correctly but the error is thrown on the form submit.

Without these changes the create form works fine apart from the published_at field showing the incorrect date. I cannot see any thing in the browser dev tools that leads me to a solution. I have reviewed the code to the vid and it looks the same.

Help would be appreciated, James

// create

{!! Form::model([$article = new \App\Article, 'url' => 'articles','class' => 'form']) !!}
@include('articles.form', ['submitButtonText' => 'Create Article'])
{!! Form::close() !!

// getter

public function getPublishedAtAttribute($date)
{
return Carbon::parse($date)->format('Y-m-d');
}

// Form partitial

<div class="form-group">
{!! Form::label('published_at', 'Published On:') !!}
{!! Form::input('date', 'published_at', $article->published_at, ['class' => 'form-control']) !!}
</div>

// Dev Tools Error

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

// Store

public function store(ArticleRequest $request){
$this->createArticle($request);
flash()->success('Your article has been created.');
return redirect('articles');
}

public function createArticle(ArticleRequest $request){
$article = Auth::user()->articles()->create($request->all());
$this->syncTags($article, $request->input('tag_list'));
return $article;
}
0 likes
5 replies
bobbybouwmann's avatar

You create should look like this

{!! Form::model($article, ['url' => 'articles','class' => 'form']) !!}
    @include('articles.form', ['submitButtonText' => 'Create Article'])
{!! Form::close() !!

As you can see, the first parameter is the actual article from the database. Now you only use Form::model if you already have an entity to work with. So if you create an article you don't need it and you can simply use Form::open

{!! Form::open(['url' => 'articles', 'class' => 'form']) !!}
4xjbh's avatar

@bobbybouwmann unfortunately that wont work as a value for $article as not been defined.

This is the error when going to articles/create with your changes. The form does not even display.

ErrorException in 26aaa3e9ee2fa2c20f7df5ffce6e007c line 21: Undefined variable: article (View: /home/james/code/project/resources/views/articles/form.blade.php) (View: /home/james/code/project/resources/views/articles/form.blade.php)

bobbybouwmann's avatar

Ok, there is a difference between creating and updating articles, this is the recommended workflow:

FIrst we start with the routes

// Create an article (return the view to create an article)
Route::get('article/create', ['as' => 'article.create', 'uses' => 'ArticlesController@create']);

// Store an article (Save the article to the database)
Route::post('article/store', ['as' => 'article.store', 'uses' => 'ArticlesController@store']);

// Edit an article (return the view to edit an article)
Route::get('article/{$id}/edit', ['as' => 'article.edit', 'uses' => 'ArticlesController@edit']);

// Update an article (Update the article to the database)
Route::patch('article/{$id}', ['as' => 'article.update', 'uses' => 'ArticlesController@update']);

Now that we have the routes we create the matching functions in the controller

public function create()
{
    // We only return the view here since we don't have any data for the article
    return view('articles.create');
}

public function store(Request $request)
{
    $article = Article::create($request->all());

    // Now that we created an article we can redirect the user to either 
    // the index or the single view for articles.
}

public function edit($id)
{
    // As you can see we pass in the $id in the function, this is the id in the url
    // and the id of the Article.
    $article = Article::findOrFail($id);

    // Now we do have an article so we can pass that to the view
    return view('articles.edit', compact('article'));
}

public function update($id, Request $request)
{
    $article = Article::findOrFail($id);
    $article->update($request->all());
    
    // Redirect the user to the index of the single article view.
}

So the controller is finished, as you can see only the edit view uses an article variable. Next up the views

  • views/articles/create.blade.php
// We open need to pass in the route, url or action for the form, no data needed here
{!! Form::open(['route' => 'article.create']) !!}

    @include('articles.form')

{!! Form::close(); !!}
  • views/articles/edit.blade.php *
// In this view the $article variable is available and we can now use it here
// The form fields will be filled because we use the model function
{!! Form::model($article, ['route' => ['article.edit', $article->id]]) !!}

    @include('articles.form')

{!! Form::close(); !!}

I hope this helps ;)

4xjbh's avatar

@bobbybouwmann, thank you for the detailed response but this does not resolve my primary problem, the 'published_at' date field.

In the last episode (fundamental series) Jeff changes the html in the create form from open to model and passes a new $article object so the getter can pass through a carbon date so the partial view does not need to be split for the create and edit methods because it is using $article->published_at as the default for the field.

If I dont do the changes Jeff does in the last episode the quick fix would be to duplicate the forms for the create and edit views and everything will work a treat. but when $article->published_at is used in the as the default value it breaks.

How do you get the current date in the 'published_at' field in the create form and the correct saved 'published_at' date in the edit form while still using the shared partial and passing in $article (As per the tutorial)

bobbybouwmann's avatar
Level 88

I guess you can do something like this, (as long as I understand that you want to publish date to be set on the create and edit page)

public function create()
{
    $article = Article::create([
        'publish_date' => Carbon::now()->format('Y-m-d);
    ]); 

    return view('articles.create', compact('article'));
}

public function edit($id)
{
    $article = Article::findOrFail($id);

    return view('articles.edit', compact('article'));
}

This would solve your problem and you should be able to use Form::model on both views. However this is not ideal, let's say someone clicks on the "Create Article" button and then closes the tab. You would have a new record without any useful information.

If you want a default value in your in your input field you can simply do something like this

{!! Form::input('publish_date', isset($article) ? $article->publish_date : date('Y-m-d'), ['class' => 'form-control']) !!}

Well you get the idea. You can set the value manually. Now that means you can't use Form::model on the create page, but is that really an issue? I mean I really like it to keep the create and edit page in their own file and only include the form, like I showed before.

Please or to participate in this conversation.