laravel/php从请求中获取对象值
我的laravel/php从请求中获取对象值,php,laravel,Php,Laravel,我的OffersController中有基本的存储功能: public function store(Request $request) { $offer = new Offer; $offer->title = $request->title; $offer->body = $request->body; $offer->region = $request->region; $offer->user_i
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);
}
我的api请求正在使用如下对象调用OffersController的存储函数:
{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "eu33", "icon": "0",
"name": "speciality name 33" }, "region": "region1" }
这给了我一个错误:内部服务器错误
"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))",
看来我犯了明显的错误,所以我更正了我的请求:
$offer->specialities()->sync($request->specialities->id, false);
这给了我一个错误:
内部服务器错误,试图获取非对象的属性。
我做错了什么
编辑:表架构:
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();
});
$request->specialities
是数组,所以要访问id,您说的$request->specialities['id']
$request->specialities
是数组,所以要访问id,您说的$request->specialities['id']
specialities
不会从数据库记录中自动水合,模型绑定仅在相应地设置路由时发生
如果给定“id”:“eu33”
,则您似乎没有为表主键使用无符号整数,对吗
尝试使用json_解码($request->get('specifications')代码>首先,然后同步到优惠
这里只是猜测一下,但也要先查询特产
记录,如:
$offer->save();
$specialties = DB::table('specialties')->where('name', $request->get('specialties')['name'])
->first();
$offer->specialities()->sync($specialities->id, false);
但是很难说没有看到您的模式。特殊性不会自动从数据库记录中删除,只有在相应地设置路由时才会发生模型绑定
如果给定“id”:“eu33”
,则您似乎没有为表主键使用无符号整数,对吗
尝试使用json_解码($request->get('specifications')代码>首先,然后同步到优惠
这里只是猜测一下,但也要先查询特产
记录,如:
$offer->save();
$specialties = DB::table('specialties')->where('name', $request->get('specialties')['name'])
->first();
$offer->specialities()->sync($specialities->id, false);
如果没有看到您的模式,很难说。这应该是一个很好的解决方案,但是当我写:$offer->specialities()->sync($request->specialities[0]->number,false)
我有一个错误:“Undefined offset:0”,
这是$request->specialities['id']不是$request->specialities[0],你访问json对象的所有属性就像访问数组元素一样,它是一个关联数组,对象属性是数组的键。没关系,我们都是初学者$int=(int)$data['specialities'][0]['id'];谢谢你的链接!这是我最后使用的工具:$id=$request->input('specialities.id');
然后$offer->specialities()->sync($id,false);
这应该是一个很好的解决方案,但是当我写:$offer->specialities()->sync($request->specialities[0]->number,false);
我有一个错误:“Undefined offset:0”
it's$request->specialities['id']而不是$request->specialities[0],您可以访问json对象的所有属性,就像访问数组元素一样,它是一个关联数组,对象属性是数组的键。没关系,我们都是初学者$int=(int)$data['specialities'][0][id'];谢谢你的链接!这是我最后使用的工具:$id=$request->input('specialities.id');
然后$offer->specialities()->sync($id,false)
我是laravel的初学者。让我看看我有基本的优惠
和特色
表和特色
表和特色
表和特色和特色
列。这段代码以前运行良好,但我不得不将我的特色
修改为一个带有nid
在每个对象中。我的id现在是eu33
,但我可以将其转换为仅数字的id。希望这能解释一些我尝试过的事情:$offer->specialities()->sync(json_解码($request->get('specialties')->id,false)
没有Lucki如果你可以发布你的表格模式,这可能会有助于找到答案。为什么?我只想获得id
值:)我对laravel非常熟悉。让我看看我有基本的优惠
和特色
带有id的表格。还有提供特色
带有提供id的表格
和专业id
列。此代码以前工作正常,但我必须将我的专业
修改为一个对象数组,每个对象中都有id
。我的id现在是eu33
,但我可以将其转换为仅数字id。希望这能解释一些我尝试过的事情:$offer->specialities()->sync(json\u decode>)($request->get('specialties')->id,false);
如果没有Lucki,您可以发布您的表架构,这可能有助于找到答案。为什么?我只想获取id
值:)