skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. $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?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search