PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given

 March 03, 2022     laravel-5, laravel-eloquent, php     No comments   

Issue

i am trying to post title and article in laravel rest api i am getting this error

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given, called in C:\xampp\htdocs\LaravelProject\cpapi\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1440

This is my route/api.php file of post article data

Route::post('articles', 'ArticleController@store');

Route::post('articles', function(Request $request) {
   return Article::create($request->all);
});

And this store function of ArticleController.php file

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

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

This is article model class

class Article extends Model
{
    //new
    protected $fillable = ['title', 'body'];
}

i tried change this in articlecontroller file but getting same error

$article = Article::create($request->only([
            'title',
            'body']));

How can I solve this issue?


Solution

As #Masivuye_Cokile suggested me, i modified my code in route and controller function and it fixed my problem.

Route/api.php

Route::post('articles', function(Request $request) {
    $data = $request->all();
        return Article::create([
            'title' => $data['title'],
            'body' => $data['body'],
        ]);
});

In Controller function

 public function store(Request $request)
    {
       $article = Article::save();
       return response()->json($article, 201);
    }


Answered By - Raikumar Khangembam
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing