Answer the question
In order to leave comments, you need to log in
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
<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>
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)
{
//
}
}
class Car extends Model
{
protected $table = 'cars';
protected $fillable = ['car'];
}
Resource('car', 'CarsController');
class PublishCarRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'car' => 'required',
'mark' => 'required',
'color' => 'required',
'age' => 'required',
'power' => 'required'
];
}
}
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();
});
}
http://localhost:8000/car?car=Bmw&mark=x6&color=black&age=2016&power=570
Answer the question
In order to leave comments, you need to log in
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">
class Car extends Model
{
protected $table = 'cars';
protected $fillable = ['car'];
}
protected $fillable = ['car', 'mark', 'color', 'age', 'power'];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question