dqwh2717
2018-08-07 19:39
浏览 148
已采纳

将数据从下拉列表推送到Laravel中的数据库

I have a problem with adding data to MySQL with Laravel. Let's say that we have a table "Books" with "categories" column, let's also say that there are only 3 categories which can be choose by User. So we have a drop-down list with "Thrillers, Horrors, Actions" and when we want to add a New Book, we have to provide its title and also select from drop-down list a category. I wrote:

create.blade.php
            <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                <label>Book title</label>
                <input type="text" class="form-control" name="name" placeholder="The title of the book" value="{{ old('title') ? : $books->title }}">
            <div class="form-group{{ $errors->has('category_id') ? ' has-error' : '' }}">
                <label>Category</label>
                <select class="form-control" name="category_id">
                        <option value="{{ old('name') ? : $books->category_id }}">Horror</option>
                        <option value="{{ old('name') ? : $books->category_id }}">Action</option>
                        <option value="{{ old('name') ? : $books->category_id }}">Thriller</option>

                </select>
            </div>

But i get an error :/

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category_id' cannot be null (SQL: insert into `books` (`category_id`, `title`, `updated_at`, `created_at`) values (, , 2018-08-07 19:37:28, 2018-08-07 19:37:28))

Also I'm a newbie in PHP and Laravel so please contain a short description for every solution. Thank you!

BooksRequest.php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BooksRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
        ];
    }
}

BooksController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Books;
use App\Http\Controllers\Controller;
use App\Services\TranslationService;
use App\Translation;
use Illuminate\Http\Request;
use App\Http\Requests\BooksRequest;

class BooksController extends Controller
{
    public function index()
    {
        $books = books::all();
        return view('books.index', compact('books'));
    }
    public function create()
    {
        $books = new Books();
        return view('books.create', compact('books'));
    }
    public function store(BooksRequest $request)
    {
        $books = new Books();
        $books ->title = $request->title;
        $books ->category_id= $request->category_id;

        if($books ->save()) {
            return redirect()->route('books.index')->with(['success' => true, 'message_type' => 'success', 'message' => 'Success '.$request->title.'.']);
        }
        return back()->with(['success' => false, 'message_type' => 'danger', 'message' => 'Bad'.$request->title.'. Try later.']);
    }
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douwen1937 2018-08-07 21:55
    最佳回答

    I used Input(„valuename”) with request and it’s working :) https://laravel.com/docs/5.6/requests

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题