0

So Im following this tutorial, Im at minute 27 https://www.youtube.com/watch?v=6Oxfb_HNY0U There, the code for the controller looks like this:

<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    public function showAllArticles(){
      return response()->json(Article::get(['title', 'description', 'status'])); // ::get([]) spezifiziert die zu referenzierenden Attribute
                                                                                // ::all() referenziert alle Attribute einer Tabelle/Relation
    }

    public function showOneArticle($id){
      return response()->json(Article::find($id));
    }

    public function create(Request $request){
      //dd($request); //for debugging whether the request is actually being processed


      $this->validate($request, [
        'title' => 'required',
        'description' => 'required'
      ]);


      //dd($request); //for debugging whether the specified fields are required
      //insert record

      $article = Article::create($request->all());
      return response()->json($article, 201);

    }
}

Now what I'm confused about is the parameter of json() in the following line:

return response()->json($article, 201);

I couldnt find anything on this second option on the laravel or lumen documentation. So far I couldn't detect any effect by this parameter either. It only appears in the log of the Restlet Client of the tutorial, see screenshot. Is it a port??? It does appear in the history log of HTTPS Requests of the tutorial guy: https://i.stack.imgur.com/VH0A6.jpg

When I have the following lines:

$this->validate($request, [
    'title' => 'required',
    'description' => 'required'
  ]);

not commented, then I ALWAYS get the following response: https://i.stack.imgur.com/bftky.jpg

{
  "title": "The title field is required",
  "description": "The description field is required"
}

When I comment these lines, then I get this error: https://textuploader.com/1oq3n

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'field list' (SQL: insert into articles (updated_at, created_at) values (2019-11-25 14:18:33, 2019-11-25 14:18:33))

I couldnt post this markup directly because then my body would exceed the maximum number of characters. So feel free to paste it into an.html file and then display it locally. Sry for that inconvenience...

I don't really understand why I get this error, because I don't reference these columns in my POST request.

The Article eloquent-model itself looks like this:

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Article extends Model
    {

        protected $fillable = [
            'title', 'description', 'status'
        ];

    }

My table on the DB side looks like this:

https://i.stack.imgur.com/Ncenb.jpg

So I don't really see any reason why this won't work for me :(

ege
  • 774
  • 5
  • 19
Narktor
  • 977
  • 14
  • 34

1 Answers1

1

There are multiple issues here.

  1. You have a misspelling of your db column created-at should be created_at as per the Exception thrown by Laravel:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'field list' (SQL: insert into articles (updated_at, created_at) values (2019-11-25 14:18:33, 2019-11-25 14:18:33))

  1. The reason you get the errors "The title field is required" in your json response is because your validation does not pass.

Try with this approach instead:

$validatedData = $request->validate([
  'title' => 'required',
  'description' => 'required',
]);
  1. The application type of your POST request is likely also wrong, try adding the Content-Type-header and set it to application/json like so
curl ... -H "Content-Type: application/json"
  1. The ->json($data, 201) is as the others suggest the HTTP Response Code as described in the standard.
ege
  • 774
  • 5
  • 19
  • Wow, Thanks a lot! About point three, the Content-Type-Header: Where do I need to insert this line of code? Also, what do I need to insert in the periods? I once configured my apache headers from both the httpd.conf and the php files. In the files like this: Header("Access-Control-Max-Age: 360"); and in the httpd.conf like this Header set Access-Control-Allow-Origin "http://localhost:8000" Where do I need to set this? In the file? Or on the apache side? or the php side? – Narktor Nov 25 '19 at 15:37
  • @baryon123 Point 3 is about the curl command that your *RESTClient sends* seen in your screenshot. You need to edit the *client* to send the Content-Type header so that Laravel understands the data coming in. – ege Nov 26 '19 at 06:51
  • Concerning the setting of the Header in RESTClient, I found a nice SO Thread with multiple helpful answers on how to do it here :=) https://stackoverflow.com/questions/13132794/firefox-add-on-restclient-how-to-input-post-parameters – Narktor Nov 26 '19 at 07:46