H
H
HeartOfProgrammer2016-05-16 01:16:24
Laravel
HeartOfProgrammer, 2016-05-16 01:16:24

Why is data not being entered into the database in 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();

        });
    }

When I enter data in the input in the form, after I click submit, I am redirected to the main page localhost: 8000 / car / index, and in the address bar everything that I passed:
http://localhost:8000/car?car=Bmw&mark=x6&color=black&age=2016&power=570

Transmitted, but not entered into the database, why?
PS I know the problem is easy, but still, the masters have one word.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tesla, 2016-05-16
@Tesla

First, your 'car.store' route accepts POST and the form sends GET, which is the same as the main page. Specify in the form

<form action="{{ Route('car.store') }}" method="post">

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

In $fillable you need to list all the properties of the model that can be changed.
protected $fillable = ['car', 'mark', 'color', 'age', 'power'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question