SQLSTATE[23000]: Integrity constraint violation When I store my post , I get an error.
Controller
public function store(Request $request)
{
$post = Post::create([
'user_id' => 1,
'title' => $request->title,
'body' => $request->body,
]);
$post->categories()->attach($request->category);
}
@oxbir Probably need to add title to your model's mass assignment array.
Post.php
protected $fillable = ['title', 'body'];
or title and body are not included within the request data. How are you submitting the request?
Either what @talinon said or make sure that your form fields have the correct names when passed to the store method.
A simple
dd($request);
```
Should tell you if the data is passed from the form or not.
I'm thinking it's not mass assignment, because I'm pretty sure the query statement wouldn't even have the columns included, which they are according to the query exception.
@talinon
I already added the following code in the Post.php model.
protected $fillable = [
'user_id',
'title',
'body',
];
@tray2
I get this message
array:4 [▼
"_token" => "wducadsyWiF9ntyDbWWYHQchRkEJ8p3CakWmIKqe"
"title" => "aaaaaaa"
"body" => "aaaaaaaa"
"category" => "1"
]
what do you get from dd($request->all());
where is the data coming from?
@snapey
I get this message
array:4 [▼
"_token" => "wducadsyWiF9ntyDbWWYHQchRkEJ8p3CakWmIKqe"
"title" => "aaaaaaa"
"body" => "aaaaaaaa"
"category" => "1"
]
blade
<div class="p-5">
<h3><i class="fas fa-plus-square mr-4 ml-1 mt-2"></i>create post</h3><hr>
@include('Admin.layouts.alert')
<form action="{{ route('posts.store') }}" method="POST">
@csrf
<div class="form-group">
<label for="title">title</label>
<input type="text" name="title" id="title" class="form-control">
</div>
<div class="form-group">
<label for="body">body</label>
<textarea name="body" id="body" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="category">category</label>
<select name="category" id="category" class="form-control">
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">save</button>
</div>
</form>
</div>
table
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
no $guarded array in your model?
no accessors?
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $guarded = [
'user_id',
'title',
'body',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
@oxbir You earlier said you defined $fillable on your model, now you posted it with $guarded??
Change it to fillable, as was suggested 3 hours ago...
And I'd highly suggest against including the user_id field, unless you're going to make sure you handle that within your controller using auth()->id()
protected $fillable = [
'title',
'body',
];
I get this error.
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into posts (title, body, updated_at, created_at) values (testt, test, 2020-11-24 01:15:08, 2020-11-24 01:15:08))
OK, When I make:auth I see this error.
@oxbir
You can add user_id to the fillable array, just make sure you do this:
$post = Post::create([
'user_id' => auth()->id(),
'title' => $request->title,
'body' => $request->body,
]);
Or:
$post = new Post;
$post->user_id = auth()->id();
$post->title = $request->title;
$post->body = $request->body;
$post->save();
As for your auth error, make a new thread for a new problem.. don't keep polluting the same thread with different issues.
Please sign in or create an account to participate in this conversation.