I've got basic store function in my OffersController:
public function store(Request $request)
{
$offer = new Offer;
$offer->title = $request->title;
$offer->body = $request->body;
$offer->region = $request->region;
$offer->user_id = auth()->user()->id;
$offer->save();
$offer->specialities()->sync($request->specialities, false);
return response()->json([
'created' => true
], 201);
}
my api request is calling OffersController store function with an object as below:
{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "eu33", "icon": "0",
"name": "speciality name 33" }, "region": "region1" }
this gives me an error: Internal Server Error
"message": "SQLSTATE[HY000]: General error: 1366 Incorrect
integer value: 'pl' for column 'speciality_id' at row 1 (SQL:
insert into `offer_speciality` (`offer_id`, `speciality_id`)
values (59, en))",
so it seems I'm doing obvious mistake, so I correct my request:
$offer->specialities()->sync($request->specialities->id, false);
and this gives me an error:
Internal Server Error, Trying to get property of non-object.
What am I doing wrong here?
edit: table schemas:
Schema::create('offers', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('region');
$table->mediumText('body');
$table->integer('user_id');
$table->timestamps();
});
Schema::create('specialities', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('offer_speciality', function (Blueprint $table) {
$table->increments('id');
$table->integer('offer_id')->unsigned();
$table->integer('speciality_id')->unsigned();
});