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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程