Did I just forget how to add new columns into my Controller? I know iv'e done this a few times, I just can't see the error right away, I need some pointers, please... I will briefly go through what I did:
- Added a migration, "create_categories_table" with the foreign key statements, etc
- Included the relationship between the "Post" and "Category" model
- In my "create" view form, I added the foreach loop and gave it the name: "categories[]"
- On the controller it's added inside the query request, on the create method, and since I have a validate variable I added the name once again in that one as well.
Store method, chained query request:
$post = new Post(request(['title', 'body', 'slug', 'image_url', 'categories']));
Create method:
public function create()
{
return view('posts.create', [
'tags' => Tag::all(),
'categories' => Category::all()
]);
}
Validate method:
protected function validatePost()
{
return request()->validate([
'title' => 'required|max:255',
'body' => 'required',
'slug' => 'required|max:100',
'tags' => 'exists:tags,id',
'categories' => 'required'
]);
}
SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into `posts` (`title`, `body`, `slug`, `image_url`, `user_id`, `updated_at`, `created_at`) values (STOFF, <p>get better</p>, thps, vol4_1608502655.jpg, 1, 2020-12-20 22:17:35, 2020-12-20 22:17:35))
Back at the "posts" table, the column name is: category_id, and this column is also included inside the $fillable property on the Post model.
After doing all this, I can read the data from my html form view, so Im able to select it, and I can manage to add a category, manually, to a post (inside SequelPro) but im just not able to send it through the HTML so, im confused did I forget to add it somewhere else, or are my namings mixed up somehow?