I have a problem with inserting Data into Database.
All i have done till now is :
Create a Model with Controller and Migration via:
php artisan make:model Cars -mcr
So, now all of my files looks this way :
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cars extends Model
{
}
<form action="{{ action('CarsController@store') }}" method="post">
{{ csrf_field() }}
<input type="text" name="brand" placeholder="Marka">
<input type="text" name="model" placeholder="Model">
<input type="text" name="doors" placeholder="Ilość drzwi">
<input type="text" name="priceHour" placeholder="Cena za godzinę">
<input type="text" name="priceDay" placeholder="Cena za dzień">
<input type="text" name="priceWeek" placeholder="Cena za tydzień">
<input type="text" name="priceMonth" placeholder="Cena za miesiąc">
<input type="text" name="priceYear" placeholder="Cena za rok">
<input type="submit" value="Osadź">
</form>
namespace App\Http\Controllers;
use App\Cars;
use Illuminate\Http\Request;
class CarsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return "test";
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$cars = new Cars;
$cars->brand = $request->input('brand');
$cars->brand = $request->input('model');
$cars->brand = $request->input('type');
$cars->brand = $request->input('doors');
$cars->priceHour = $request->input('priceHour');
$cars->priceDay = $request->input('priceDay');
$cars->priceWeek = $request->input('priceWeek');
$cars->priceMonth = $request->input('priceMonth');
$cars->priceYear = $request->input('priceYear');
$cars->save();
return redirect('admin.AddCar');
}
Route::resource('/cars', 'CarsController');
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCarsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('brand');
$table->string('model');
$table->string('type');
$table->integer('doors');
$table->string ('priceHour')->nullable();
$table->string('priceDay')->nullable();
$table->string('priceWeek')->nullable();
$table->string('priceMonth')->nullable();
$table->string('priceYear')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cars');
}
}
Error I receiving after fill out all fields and click "Osadz" = submit is:
SQLSTATE[HY000]: General error: 1364 Field 'model' doesn't have a default value (SQL: insert into
cars
(brand
,priceHour
,priceDay
,priceWeek
,priceMonth
,priceYear
,updated_at
,created_at
) values (3, 3, 53, 3, 35, 3, 2018-01-30 09:36:57, 2018-01-30 09:36:57))
And my question is, what default value is missing in my code ?
Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.
This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function.
"model"
You should add the field 'model' in your Cars's Model, fillable fields.
Or if you don't want to give it a value at first, in the migrating file, you can add ->nullable() to the field.
Thank you guys, you are brilliant !
Already solved the problem, i missed out the field in the View :
<input type="text" name "type" placeholder="Typ"/>
which can not be NULL
and i "fix" the model with :
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cars extends Model
{
protected $fillable = [
"brand", "model" , "type" , "doors" , "priceHour" ,
"priceDay" , "priceWeek" , "priceMonth" , "priceYear"
];
}
Thank you and have a nice day !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With