Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create new article throwing up category_id doesn't have a default value #16

Closed
venkatavinash opened this issue Aug 16, 2017 · 9 comments
Labels
Status: Solved Used for solved questions Type: User Error

Comments

@venkatavinash
Copy link

venkatavinash commented Aug 16, 2017

It is not taking lots of other fields like title,body and others. It is working fine if i comment out the below
use HasPageVisitsCounter;

error:

SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into articles (updated_at, created_at) values (2017-08-16 12:05:23, 2017-08-16 12:05:23))

controller:

public function store(Request $request)
   {
    $input = $request->all();
    $input['slug'] = str_slug($request->input('title'), '-');
    $article = \App\Article::create($input);
    return redirect('master/articles');
   }

Article Model:

 use HasPageVisitsCounter;
 protected $fillable = [
    'category_id','author','title','body', 'slug','excerpt'
];
public function category() {
    return $this->belongsTo('App\Category');
}

Do not know where the issue is.

@cyrildewit
Copy link
Owner

How does your migration file looks like?

@venkatavinash
Copy link
Author

venkatavinash commented Aug 17, 2017

    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('category_id')->index();
            $table->string('author')->default('admin');
            $table->string('title');
            $table->text('excerpt');
            $table->text('body');
            $table->string('slug');
            $table->string('feature_image')->default('test.jpg');
            $table->timestamps();
        });
    }

@cyrildewit
Copy link
Owner

I found some articles that are maybe related to your problem.

https://laravel.io/forum/08-08-2015-eloquent-create-says-column-has-no-default-value
https://stackoverflow.com/questions/43186521/laravel-5-4-field-doesnt-have-a-default-value
https://stackoverflow.com/questions/39301589/laravel-5-3-creating-models-returns-field-doesnt-have-a-default-value

I'm currently on vacation, using a public computer with a weird keyboard. I will be back in a week. I'm sorry. I personally don't think it has something to do with this package, but I will of course take a look at it. I suggest you to keep searching for an answer. See you then.

@venkatavinash
Copy link
Author

venkatavinash commented Aug 17, 2017

Every thing is working fine normally. But when I add "use HasPageVisitsCounter;" in the model it is throwing error and only showing category_id error even when the data is coming to the controller.I have used dd() to dump if the data is coming to the controller just before create($input) and it dumped all. The moment it is run with "use HasPageVisitsCounter;" it appears. I have checked your code but unable to narrow down the problem.

Thank you for the links.Appreciate you taking time to reply. Will try to sort it out ASAP and update.

@cyrildewit
Copy link
Owner

What happens if you turn strict mode off in your database config file?

https://laracasts.com/discuss/channels/laravel/laravel-530-throwing-error-field-test-1-doesnt-have-a-default-value

@venkatavinash
Copy link
Author

I have tried it already.That way It just creates id, timestamps and a column with default value. rest are all empty.

@cyrildewit
Copy link
Owner

This threat is quite interesting: https://laravel.io/forum/08-08-2015-eloquent-create-says-column-has-no-default-value

Try to change this: $article = \App\Article::create($input);
To:

$article = new \App\Article();
$article->category_id = $input['category_id'];
$article->author = ........
..... etc.

It think that it has something to do with Laravel Eloquent.

@venkatavinash
Copy link
Author

Changed the code as you said and it worked like charm!
Thank you for your time and effort.

@cyrildewit
Copy link
Owner

Maybe a little bit late but a while ago @LuisNeighbur created a pull request that probably fixed this issue. I forgot to pass the fillable attributes in the constructor function.

@cyrildewit cyrildewit added the Status: Solved Used for solved questions label Apr 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Solved Used for solved questions Type: User Error
Projects
None yet
Development

No branches or pull requests

2 participants