Home > Software design >  laravel 9 select column value without column name
laravel 9 select column value without column name

Time:09-11

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?

CodePudding user response:

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

CodePudding user response:

$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?

  • Related