@HeartOfProgrammer

Почему данные не заносятся в базу данных в laravel 5.1?

view

<a href="{{ route('car.create') }}" role="button">Добавить машину</a>
    <h1>Машины:</h1>
    @foreach($allCars as $car)
        <h2>Машина: {{ $car->car }}</h2>
        <p>Марка: {{ $car->mark }}</p>
        <p>Цвет: {{ $car->color }}</p>
        <p>Год: {{ $car->age }}</p>
        <p>Сила: {{ $car->power }}</p>
    @endforeach


add
<form action="{{ Route('car.store') }}">
        <label for="title">Названия машины:</label>
        <input type="text" name="car" placeholder="Введите названия машины" required>

        <br/>

        <label for="title">Марка машины:</label>
        <input type="text" name="mark" placeholder="Введите марку машины" required>

        <br/>

        <label for="title">Цвет машины:</label>
        <input type="text" name="color" placeholder="Введите цвет машины" required>

        <br/>

        <label for="title">Год машины:</label>
        <input type="text" name="age" placeholder="Введите год машины" required>

        </br>

        <label for="title">Сила машины:</label>
        <input type="text" name="power" placeholder="Введите силу машины" required>

        <br/>

        <input type="submit">
    </form>


Controller
class CarsController extends Controller
{

    public function index()
    {
        $allCars = Car::all();

        return view('index', compact('allCars'));
    }
    
    public function create()
    {
        return view('add');
    }
    
    public function store(PublishCarRequest $requestData)
    {
        $car = new Car;
        $car->car = $requestData['car'];
        $car->mark = $requestData['mark'];
        $car->color = $requestData['color'];
        $car->age = $requestData['age'];
        $car->power = $requestData['power'];
        $car->save();

        return redirect()->route('car.index');
    }
    
    public function show($id)
    {
        //
    }
    
    public function edit($id)
    {
        //
    }
    
    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}

Model
class Car extends Model
{
    protected $table = 'cars';
    protected $fillable = ['car'];
}


Route
Resource('car', 'CarsController');

Request
class PublishCarRequest extends Request
{
    
    public function authorize()
    {
        return true;
    }
    
    public function rules()
    {
        return [
            'car' => 'required',
            'mark' => 'required',
            'color' => 'required',
            'age' => 'required',
            'power' => 'required'
        ];
    }
}


Migrations
class CreateTableCars extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cars', function (Blueprint $table) {
            $table->increments('id');
            $table->string('car');
            $table->string('mark');
            $table->string('color');
            $table->string('age');
            $table->string('power');
            $table->timestamps();

        });
    }


Когда в форме прописываю данные в input, после нажимаю отправить, меня перенаправляет на главную страницу localhost:8000/car/index, и в адресной строке все то что я передал:
http://localhost:8000/car?car=Bmw&mark=x6&color=black&age=2016&power=570


Передается, но не заноситься в базу данных, почему?

PS знаю проблема легкая, но все же, мастерам одно слово.
  • Вопрос задан
  • 432 просмотра
Пригласить эксперта
Ответы на вопрос 1
Tesla
@Tesla
Во-первых, у вас роут 'car.store' принимает POST, а форма отправляет GET, что соответствует главной странице. Укажите в форме
<form action="{{ Route('car.store') }}" method="post">


А во-вторых:
class Car extends Model
{
    protected $table = 'cars';
    protected $fillable = ['car'];
}

В $fillable надо перечислить все свойства модели, которые можно изменять.
protected $fillable = ['car', 'mark', 'color', 'age', 'power'];
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы