1

I am a beginner of using Laravel, in this case i am going to get a data(price) from database and then define it as an variables($price) for further process.

The problem is when i define the variables $price and try to echo it to test the out come, it occurred a error called "Object of class stdClass could not be converted to string".

I try to figure out the problems these day, realize that there are something wrong on the result from the database. It changes to an Object of class stdClass instead of an array? However, when i use all the solution that i found on the internet it still doesn't work........

Solution i have already tried:

$array = json_decode(json_encode($price), true);

$price = array();

->toArray(); //Used in my code

Below are my Controller.blade.php

 /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \App\Menu  $menu
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $day)
        {

            $opt1 = $request->post('dishopt1');

            $price1 = DB::table('dishes')->select('price', 'chg_porridge')->where([
                ['name', '=', $opt1],
            ])->get()->toArray();

            $price1 = $price1[0];

            echo $price1;

        }
Max Cheung
  • 31
  • 1
  • 7

3 Answers3

1
$price1 = DB::table('dishes')->select('price', 'chg_porridge')->where([
                ['name', '=', $opt1],
            ])->get();        
$values = json_decode(json_encode($price1), true);
hemant
  • 156
  • 1
  • 6
0

You can avoid doing all that by using the method value()

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Menu  $menu
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $day)
{

    $opt1 = $request->post('dishopt1');

    $price1 = DB::table('dishes')->where([
        ['name', '=', $opt1],
    ])->value('price');

    echo $price1;

}
N69S
  • 16,110
  • 3
  • 22
  • 36
  • Thanks! You have save my project! Dont know that it also have a ->value(); function would easily get the value that i want! Thanks again!! – Max Cheung Nov 21 '19 at 10:16
0

DB::table()->get() returns a collection of StdClass (see: What is stdClass in PHP?) object and that is why you have an object as result when you picked the first one (See: Laravel Query Builder - Retrieving Result).

Well this is intended by Laravel. Its either you simply cast the response with (array)$price1 or you use ->json() method of Illuminate\Contracts\Routing\ResponseFactory interface which makes your return like this:

return response()->json($price1);

NB:

  • You don't need to use get() then convert to array and retrieve the first item, you can simply do ->first() instead of get() then you have the first StdClass object.
  • You can also use Eloquent model instead and it would convert it to Response compatible value when retrieving from the database.
  • You can just return your value instead of using echo. Laravel would do the magic for you.
  • To test what a value has you can also use dd() - dump and die or dump()
  • Now i finally know that what is the problem Thanks!!! However, i still i still cant figure out why the Jason encode and decode function would not fix it. Like: $array = json_decode(json_encode($price), true); – Max Cheung Nov 21 '19 at 10:21
  • 1. `json_decode()` takes a json string and converts it to an object or array (depending on the flag you give it). 2. `json_encode()` converts an object or array to a json string. It means you should encode before decoding, and that's where you did the wrong thing. However, it does look like calling the two functions just to get an array that you want to return as response is unnecessary. – Oluwatobi Samuel Omisakin Nov 21 '19 at 10:36