laravel 9选择不带列名的列值



我使用的是模型,我想从表中选择数据,然后只获取值。我试过这个:

Rental::create([
'id_bike' => $request->id,
'bike_brand' => Bike::where('id', $request->id)->pluck('brand'),
'bike_price' => Bike::where('id', $request->id)->pluck  ('price'),
]);

但结果是这样的:

["Magni temporibus non et ratione qui consequatur qui."]
[95]

我需要这样的结果:

Magni temporibus non et ratione qui consequatur qui.
95

我能做什么?

如果您的关系设置正确,您就可以这样做例如,你有模型Person,在这个模型中,我们有id、名称、电子邮件、国家/地区列,所以如果我们只想调用这个模型的名称,这个代码就是

$people = Person::first();
dd($people->name);

这就是你如何捕捉到一个具有雄辩关系的特定专栏

$getBike = Bike::where('id', $request->id)->first();
if($getBike === null){
return "SORRY BIKE DOES NOT EXIST";
OR
return redirect()->route('name_of_your_route');
OR
return redirect()->json([
'status'    => 'error',
'message'   => 'Bike Does Not Exist'
]);
}
Rental::create([
'id_bike'    => $request->id,
'bike_brand' => $getBike->brand,
'bike_price' => $getBike->price
]);

看起来你的问题是";如何在laravel中将数组转换为字符串"基于您想要的结果如果是,在这里如何在laravel中将数组转换为字符串?

最新更新