I am very new to laravel and sqlite and am trying to create a new 'dish' to my list of dishes, however, once I submit the creation form I receive the following error: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: dishes.user_id (SQL: insert into "dishes" ("name", "price", "user_id", "updated_at", "created_at") values (Supreme Pizza, 15, ?, 2019-09-29 05:43:42, 2019-09-29 05:43:42))
I have tried a number of different modifications in my web.php, DishController, UserController, and the create.blade.php form itself, but none of the changes have worked. There were some similar questions asked here but none of the solutions helped my particular problem either.
This is my web.php file
Route::resource('dish', 'DishController');
Route::resource('user', 'UserController');
Auth::routes();
Route::get('/', 'UserController@index');
Route::get('/show/{id}', 'UserController@show');
Route::get('/show/{id}', 'UserController@create');
Route::get('users/{user}', ['as' => 'users.edit', 'uses' => 'UserController@edit']);
Route::patch('users/{user}/update', ['as' => 'users.update', 'uses' => 'UserController@update']);
Route::get('/dish', 'DishController@index');
Route::get('/show/{id}', 'DishController@show');
Route::get('/dish/create', ['as' => 'dish.create', 'uses' => 'DishController@create']);
Route::get('dishes/{dish}', ['as' => 'dish.edit', 'uses' => 'DishController@edit']);
Route::patch('dishes/{dish}/update', ['as' => 'dishes.update', 'uses' => 'DishController@update']);
Route::get('/dish/{dish}', 'DishController@destroy');
The relevant part of my DishController:
public function index()
{
$dishes = Dish::all();
return view('dishes.index')->with('dishes', $dishes);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return View::make('dishes.create_form')->with('users', User::all());
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
'price' => 'required|integer|min:1',
'user' => 'exists:user,id'
]);
$dish = new Dish();
$dish->name = $request->name;
$dish->price = $request->price;
$dish->user_id = $request->user;
$dish->save();
return redirect("dish/$dish->id");
}
And the relevant part of my create_form:
<form method="POST" action=''>
<p><label>Name: </label><input type="text" name="name" value=""></p>
<p><label>Price: </label><input type="text" name="price" value=""></p>
<p><select name="Restaurant">
@foreach ($users as $user)
@if ($user->role == 'restaurant')
<option value="" selected="selected"></option>
@endif
@endforeach
</select></p>
<input type="submit" value="Create">
</form>
The general idea is that I list my users that have a 'role' of 'restaurant' and when I click on one, it takes me to the dishes they have (i.e. lists all dishes with selected user_id), and the option to create a new dish.
When I try to create a new dish, I input all necessary data and submit the form but I get the error message as seen in the Title. It says that dishes.user_id is null, and shows it as '?'.
This is odd since the POST data shows that:
name = "Supreme Pizza"
price = "15"
Restaurant = "7" (which is correct)
I can't for the life of me figure out what the problem is, so any help would be greatly appreciated.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ogZEei
via IFTTT
Aucun commentaire:
Enregistrer un commentaire