0

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();
    });

2 Answers 2

1

$request->specialities is array, so to access the id you say $request->specialities['id']

Sign up to request clarification or add additional context in comments.

18 Comments

this should be good solution, but when I write: $offer->specialities()->sync($request->specialities[0]->number, false); I've got an error: "Undefined offset: 0",
it's $request->specialities['id'] NOT $request->specialities[0], you access all the properties of the json object just like accessing array elements, it's an associative array with object properties being keys to the array
it's ok we all were beginners
$int = (int)$data['specialities'][0]['id'];
thank you for this link! this is finally works I use simply: $id= $request->input('specialities.id'); and then $offer->specialities()->sync($id, false);
|
1

specialities is not being automatically hydrated from a database record, model binding only occurs when you setup the route accordingly.

It appears you're not using an unsigned integer for the specialties table primary key, given "id": "eu33", is that correct?

Try using json_decode($request->get('specialties')); first then sync to offers.

Just taking a guess here, but also doing a query for the specialities record first like:

$offer->save();

$specialties = DB::table('specialties')->where('name', $request->get('specialties')['name'])
                                       ->first();

$offer->specialities()->sync($specialities->id, false);

Hard to say though without seeing your schema.

12 Comments

I'm very beginner on laravel. Let me see
I've got basic offers and specialities table with id's. And offer_speciality table with offer_id and speciality_id columns. This code works fine previously, but I had to modify my speciality into an array of objects with an id inside each object. My id is now eu33 but I can convert it into digit only id. Hope this explained something
I tried: $offer->specialities()->sync(json_decode($request->get('specialties'))->id, false); with no luck
If you can post your table schemas it would probably help get to the answer.
why? I just want to get the id value :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.