简体   繁体   中英

laravel 9 select column value without column name

I'm using model and i want to select the data from table then only get the value. I've tried this:

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

but the result is like this:

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

[95]

I need the result to be like this:

Magni temporibus non et ratione qui consequatur qui.

95

What can I do?

with eloquent if your your relation is setup correctly you can just do this for example you have the model Person and in this model we have id, name, email, country columns so if we want to call to just name of this model right this code

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

and this how you catch a specific column with eloquent relationship

$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
]);

It looks like your question is "How to convert array to string in laravel?" based on your desired result If yes Here How to convert array to string in laravel?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM