SQLSTATE[HY000]: General error: 1364 Field 'mov_id' doesn't have a default value
have a question related with the 2 tables which are movie and categories
categories are belong to movie which mean i can set many kind of categories for the movie.
So in the categories models, I put this code
public function movie(){
return $this->belongsTo('AppMovie', 'mov_id');
}
which I want to set categories.mov_id = movie.id
On Controller Part, validation is fine off course show the error message when i put blank. But the problem is cant save to database and display mov_id doesn't have a default value
public function store(Request $request)
{
$rules = array(
'cat_title' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('category/create')
->withErrors($validator);
} else {
$movie = new Category();
$movie->cat_title = Input::get('cat_title');
$movie->save();
Session::flash('message', 'Successfully created!');
return Redirect::to('category');
}
}
and last, here is the view file
<div class="table-container">
<form method="POST" action="{{ route('category.store') }}" role="form">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<input type="text" name="cat_title" id="cat_title" class="form-control input-sm" placeholder="分类">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<input type="submit" value="Save" class="btn btn-success btn-block">
<a href="{{ route('category.index') }}" class="btn btn-info btn-block" >Back</a>
</div>
</div>
</form>
</div>
Category schema
Schema::create('category', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
$table->integer('mov_id');
$table->timestamps();
});
The image of error message:-
php laravel
|
show 4 more comments
have a question related with the 2 tables which are movie and categories
categories are belong to movie which mean i can set many kind of categories for the movie.
So in the categories models, I put this code
public function movie(){
return $this->belongsTo('AppMovie', 'mov_id');
}
which I want to set categories.mov_id = movie.id
On Controller Part, validation is fine off course show the error message when i put blank. But the problem is cant save to database and display mov_id doesn't have a default value
public function store(Request $request)
{
$rules = array(
'cat_title' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('category/create')
->withErrors($validator);
} else {
$movie = new Category();
$movie->cat_title = Input::get('cat_title');
$movie->save();
Session::flash('message', 'Successfully created!');
return Redirect::to('category');
}
}
and last, here is the view file
<div class="table-container">
<form method="POST" action="{{ route('category.store') }}" role="form">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<input type="text" name="cat_title" id="cat_title" class="form-control input-sm" placeholder="分类">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<input type="submit" value="Save" class="btn btn-success btn-block">
<a href="{{ route('category.index') }}" class="btn btn-info btn-block" >Back</a>
</div>
</div>
</form>
</div>
Category schema
Schema::create('category', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
$table->integer('mov_id');
$table->timestamps();
});
The image of error message:-
php laravel
Can you add the proper error what are you exactly getting
– Arzeb Mansuri
Nov 24 '18 at 9:14
2
belongsTo
means that each category table row is related to exactly one movie. That does not seem like what you indended. It makes more sense to saybelongsToMany
to say that a category is related to many movies and vice versa or (if a movie has a single category) say a categoryhasMany
movies
– apokryfos
Nov 24 '18 at 9:16
You have pass mov_id in your form as a hidden field
– Arzeb Mansuri
Nov 24 '18 at 9:16
Please post category table schema and the full error message
– Sapnesh Naik
Nov 24 '18 at 9:18
@ArzebMansuri Could you briefly show the example ?
– lun7code
Nov 24 '18 at 9:35
|
show 4 more comments
have a question related with the 2 tables which are movie and categories
categories are belong to movie which mean i can set many kind of categories for the movie.
So in the categories models, I put this code
public function movie(){
return $this->belongsTo('AppMovie', 'mov_id');
}
which I want to set categories.mov_id = movie.id
On Controller Part, validation is fine off course show the error message when i put blank. But the problem is cant save to database and display mov_id doesn't have a default value
public function store(Request $request)
{
$rules = array(
'cat_title' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('category/create')
->withErrors($validator);
} else {
$movie = new Category();
$movie->cat_title = Input::get('cat_title');
$movie->save();
Session::flash('message', 'Successfully created!');
return Redirect::to('category');
}
}
and last, here is the view file
<div class="table-container">
<form method="POST" action="{{ route('category.store') }}" role="form">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<input type="text" name="cat_title" id="cat_title" class="form-control input-sm" placeholder="分类">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<input type="submit" value="Save" class="btn btn-success btn-block">
<a href="{{ route('category.index') }}" class="btn btn-info btn-block" >Back</a>
</div>
</div>
</form>
</div>
Category schema
Schema::create('category', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
$table->integer('mov_id');
$table->timestamps();
});
The image of error message:-
php laravel
have a question related with the 2 tables which are movie and categories
categories are belong to movie which mean i can set many kind of categories for the movie.
So in the categories models, I put this code
public function movie(){
return $this->belongsTo('AppMovie', 'mov_id');
}
which I want to set categories.mov_id = movie.id
On Controller Part, validation is fine off course show the error message when i put blank. But the problem is cant save to database and display mov_id doesn't have a default value
public function store(Request $request)
{
$rules = array(
'cat_title' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('category/create')
->withErrors($validator);
} else {
$movie = new Category();
$movie->cat_title = Input::get('cat_title');
$movie->save();
Session::flash('message', 'Successfully created!');
return Redirect::to('category');
}
}
and last, here is the view file
<div class="table-container">
<form method="POST" action="{{ route('category.store') }}" role="form">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<input type="text" name="cat_title" id="cat_title" class="form-control input-sm" placeholder="分类">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<input type="submit" value="Save" class="btn btn-success btn-block">
<a href="{{ route('category.index') }}" class="btn btn-info btn-block" >Back</a>
</div>
</div>
</form>
</div>
Category schema
Schema::create('category', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
$table->integer('mov_id');
$table->timestamps();
});
The image of error message:-
php laravel
php laravel
edited Nov 24 '18 at 9:32
lun7code
asked Nov 24 '18 at 9:09
lun7codelun7code
548
548
Can you add the proper error what are you exactly getting
– Arzeb Mansuri
Nov 24 '18 at 9:14
2
belongsTo
means that each category table row is related to exactly one movie. That does not seem like what you indended. It makes more sense to saybelongsToMany
to say that a category is related to many movies and vice versa or (if a movie has a single category) say a categoryhasMany
movies
– apokryfos
Nov 24 '18 at 9:16
You have pass mov_id in your form as a hidden field
– Arzeb Mansuri
Nov 24 '18 at 9:16
Please post category table schema and the full error message
– Sapnesh Naik
Nov 24 '18 at 9:18
@ArzebMansuri Could you briefly show the example ?
– lun7code
Nov 24 '18 at 9:35
|
show 4 more comments
Can you add the proper error what are you exactly getting
– Arzeb Mansuri
Nov 24 '18 at 9:14
2
belongsTo
means that each category table row is related to exactly one movie. That does not seem like what you indended. It makes more sense to saybelongsToMany
to say that a category is related to many movies and vice versa or (if a movie has a single category) say a categoryhasMany
movies
– apokryfos
Nov 24 '18 at 9:16
You have pass mov_id in your form as a hidden field
– Arzeb Mansuri
Nov 24 '18 at 9:16
Please post category table schema and the full error message
– Sapnesh Naik
Nov 24 '18 at 9:18
@ArzebMansuri Could you briefly show the example ?
– lun7code
Nov 24 '18 at 9:35
Can you add the proper error what are you exactly getting
– Arzeb Mansuri
Nov 24 '18 at 9:14
Can you add the proper error what are you exactly getting
– Arzeb Mansuri
Nov 24 '18 at 9:14
2
2
belongsTo
means that each category table row is related to exactly one movie. That does not seem like what you indended. It makes more sense to say belongsToMany
to say that a category is related to many movies and vice versa or (if a movie has a single category) say a category hasMany
movies– apokryfos
Nov 24 '18 at 9:16
belongsTo
means that each category table row is related to exactly one movie. That does not seem like what you indended. It makes more sense to say belongsToMany
to say that a category is related to many movies and vice versa or (if a movie has a single category) say a category hasMany
movies– apokryfos
Nov 24 '18 at 9:16
You have pass mov_id in your form as a hidden field
– Arzeb Mansuri
Nov 24 '18 at 9:16
You have pass mov_id in your form as a hidden field
– Arzeb Mansuri
Nov 24 '18 at 9:16
Please post category table schema and the full error message
– Sapnesh Naik
Nov 24 '18 at 9:18
Please post category table schema and the full error message
– Sapnesh Naik
Nov 24 '18 at 9:18
@ArzebMansuri Could you briefly show the example ?
– lun7code
Nov 24 '18 at 9:35
@ArzebMansuri Could you briefly show the example ?
– lun7code
Nov 24 '18 at 9:35
|
show 4 more comments
1 Answer
1
active
oldest
votes
The error msg tells you exactly what the problem is. You are trying to insert a category record, but your form does not include a mov_id
value. Your schema specifies that mov_id
is an integer, but does not specify any default value, nor that it can be null. So Laravel does not know what to use for mov_id
if you don't tell it, so it can't create a record.
The quick fix is to update your schema to allow null values for mov_id
:
$table->integer('mov_id')->nullable();
But I think you're going to have bigger problems. A category probably should not have a movie ID at all - that would mean that every category could have at most 1 movie.
I think what you really want is a many-to-many relationship between movies and categories. That means one movie can have many categories, and one category can be assigned to many movies.
The Laravel docs describe it well and include an example. You'll need to update your Laravel relationships:
// Category model
public function movie() {
return $this->belongsToMany('AppMovie');
}
// Movie model
public function category() {
return $this->belongsToMany('AppCategory');
}
And your schema:
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
// ... etc
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
// ... etc
});
Schema::create('category_movie', function (Blueprint $table) {
$table->integer('category_id');
$table->integer('movie_id');
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456727%2fsqlstatehy000-general-error-1364-field-mov-id-doesnt-have-a-default-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error msg tells you exactly what the problem is. You are trying to insert a category record, but your form does not include a mov_id
value. Your schema specifies that mov_id
is an integer, but does not specify any default value, nor that it can be null. So Laravel does not know what to use for mov_id
if you don't tell it, so it can't create a record.
The quick fix is to update your schema to allow null values for mov_id
:
$table->integer('mov_id')->nullable();
But I think you're going to have bigger problems. A category probably should not have a movie ID at all - that would mean that every category could have at most 1 movie.
I think what you really want is a many-to-many relationship between movies and categories. That means one movie can have many categories, and one category can be assigned to many movies.
The Laravel docs describe it well and include an example. You'll need to update your Laravel relationships:
// Category model
public function movie() {
return $this->belongsToMany('AppMovie');
}
// Movie model
public function category() {
return $this->belongsToMany('AppCategory');
}
And your schema:
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
// ... etc
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
// ... etc
});
Schema::create('category_movie', function (Blueprint $table) {
$table->integer('category_id');
$table->integer('movie_id');
});
add a comment |
The error msg tells you exactly what the problem is. You are trying to insert a category record, but your form does not include a mov_id
value. Your schema specifies that mov_id
is an integer, but does not specify any default value, nor that it can be null. So Laravel does not know what to use for mov_id
if you don't tell it, so it can't create a record.
The quick fix is to update your schema to allow null values for mov_id
:
$table->integer('mov_id')->nullable();
But I think you're going to have bigger problems. A category probably should not have a movie ID at all - that would mean that every category could have at most 1 movie.
I think what you really want is a many-to-many relationship between movies and categories. That means one movie can have many categories, and one category can be assigned to many movies.
The Laravel docs describe it well and include an example. You'll need to update your Laravel relationships:
// Category model
public function movie() {
return $this->belongsToMany('AppMovie');
}
// Movie model
public function category() {
return $this->belongsToMany('AppCategory');
}
And your schema:
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
// ... etc
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
// ... etc
});
Schema::create('category_movie', function (Blueprint $table) {
$table->integer('category_id');
$table->integer('movie_id');
});
add a comment |
The error msg tells you exactly what the problem is. You are trying to insert a category record, but your form does not include a mov_id
value. Your schema specifies that mov_id
is an integer, but does not specify any default value, nor that it can be null. So Laravel does not know what to use for mov_id
if you don't tell it, so it can't create a record.
The quick fix is to update your schema to allow null values for mov_id
:
$table->integer('mov_id')->nullable();
But I think you're going to have bigger problems. A category probably should not have a movie ID at all - that would mean that every category could have at most 1 movie.
I think what you really want is a many-to-many relationship between movies and categories. That means one movie can have many categories, and one category can be assigned to many movies.
The Laravel docs describe it well and include an example. You'll need to update your Laravel relationships:
// Category model
public function movie() {
return $this->belongsToMany('AppMovie');
}
// Movie model
public function category() {
return $this->belongsToMany('AppCategory');
}
And your schema:
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
// ... etc
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
// ... etc
});
Schema::create('category_movie', function (Blueprint $table) {
$table->integer('category_id');
$table->integer('movie_id');
});
The error msg tells you exactly what the problem is. You are trying to insert a category record, but your form does not include a mov_id
value. Your schema specifies that mov_id
is an integer, but does not specify any default value, nor that it can be null. So Laravel does not know what to use for mov_id
if you don't tell it, so it can't create a record.
The quick fix is to update your schema to allow null values for mov_id
:
$table->integer('mov_id')->nullable();
But I think you're going to have bigger problems. A category probably should not have a movie ID at all - that would mean that every category could have at most 1 movie.
I think what you really want is a many-to-many relationship between movies and categories. That means one movie can have many categories, and one category can be assigned to many movies.
The Laravel docs describe it well and include an example. You'll need to update your Laravel relationships:
// Category model
public function movie() {
return $this->belongsToMany('AppMovie');
}
// Movie model
public function category() {
return $this->belongsToMany('AppCategory');
}
And your schema:
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
// ... etc
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
// ... etc
});
Schema::create('category_movie', function (Blueprint $table) {
$table->integer('category_id');
$table->integer('movie_id');
});
edited Nov 24 '18 at 11:35
answered Nov 24 '18 at 9:39
Don't PanicDon't Panic
5,12221132
5,12221132
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456727%2fsqlstatehy000-general-error-1364-field-mov-id-doesnt-have-a-default-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you add the proper error what are you exactly getting
– Arzeb Mansuri
Nov 24 '18 at 9:14
2
belongsTo
means that each category table row is related to exactly one movie. That does not seem like what you indended. It makes more sense to saybelongsToMany
to say that a category is related to many movies and vice versa or (if a movie has a single category) say a categoryhasMany
movies– apokryfos
Nov 24 '18 at 9:16
You have pass mov_id in your form as a hidden field
– Arzeb Mansuri
Nov 24 '18 at 9:16
Please post category table schema and the full error message
– Sapnesh Naik
Nov 24 '18 at 9:18
@ArzebMansuri Could you briefly show the example ?
– lun7code
Nov 24 '18 at 9:35