Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marsuch's avatar

Object of class stdClass could not be converted to string

Hi, I need advice. In my content management system I have a list of categories that are recursively saved. And I need to find out from the URL in which category I am.

First, I use a query to find out if the category has a parent

$url = DB::table('menus')
            ->select('parent_id')->where('url', '=', '$param['param']')->get();

Then, with the next query, I will list all subcategories that fall under the current category:

        $data = DB::table('menus')
                ->join('article_categories', 'article_categories.id', '=', 'menus.parent_id' )->where('parent_id', '=', $url[0])->get();

I get data from the url address as follows:

//route - web.php
Route::get('articles/{param?}', 'ArticleController@articles')->name('articles');


//ArticleController.php
$param = request()->route()->params('param');

How it should work, but it doesn't work: I read which category it is from the parameter, then I get the id of the given category using a sql query. Finally, I will use the ID of the given category to list the descendants, if any exist.

So the whole thing looks like this>


        $param = request()->route()->params('param');
        
        $url = DB::table('menus')
            ->select('parent_id')->where('url', '=', $param['param'])->get();

        $data = DB::table('menus')
                ->join('article_categories', 'article_categories.id', '=', 'menus.parent_id' )->where('parent_id', '=', $url)->get();

        return view('article.articles', compact('data', 'url'));
0 likes
10 replies
guybrush_threepwood's avatar

That's because the following query returns a collection of objects and not a string:

$url = DB::table('menus')
            ->select('parent_id')
            ->where('url', '=', $param['param'])
            ->get();

You should do:

        $url = DB::table('menus')
            ->select('parent_id')
            ->where('url', '=', $param['param'])
            ->first();

        $data = DB::table('menus')
                ->join('article_categories', 'article_categories.id', '=', 'menus.parent_id')
                ->where('parent_id', '=', $url->parent_id)
                ->get();

Or this if you expect multiple results from the first query:

        $url = DB::table('menus')
            ->select('parent_id')
            ->where('url', '=', $param['param'])
            ->pluck('parent_id');

        $data = DB::table('menus')
                ->join('article_categories', 'article_categories.id', '=', 'menus.parent_id')
                ->whereIn('parent_id', $url)
                ->get();

marsuch's avatar

I edited the code according to you, but I still don't get back the content of the given category. The first query is the variable $ url, it already works as it should, but the second query does not return anything after testing with dd


        $param = request()->route()->parameters('param');

        $url = DB::table('menus')
            ->select('parent_id')
            ->where('url', '=', $param['param'])
            ->first();

        $data = DB::table('menus')
            ->join('article_categories', 'article_categories.id', '=', 'menus.parent_id')
            ->where('parent_id', '==', $url->parent_id)
            ->get();

        dd($data);
guybrush_threepwood's avatar

You have a typo in your where statement:

->where('parent_id', '==', $url->parent_id)

It should be:

->where('parent_id', '=', $url->parent_id)

Also check that $url->parent_id is not returning null.

marsuch's avatar

I checked, $ url-> parent_id, returns what it has. and what a typo in the operator, I also tried a variant or you wrote me. And yet it returns no value

guybrush_threepwood's avatar

Could you show me some sample data? It's gets difficult without knowing the table structure.

What does the following return?

dd($param['param']);
        $url = DB::table('menus')
            ->select('parent_id')
            ->where('url', '=', $param['param'])
            ->first();
        dd($url);
        $data = DB::table('menus')
            ->join('article_categories', 'article_categories.id', '=', 'menus.parent_id')
            ->limit(3)
            ->get();
	dd($data);
marsuch's avatar

The URL is localhost/articles/example. The parameter used to search the database is: example

output - dd($param['param']);

"articles"

which is fine

output of first query, i.e. - dd($url);

{#1394 ▼
  +"parent_id": 0
}

which is fine

output first query, i.e. - dd($url->parent_id)

	0

which is fine

output od second query, i.e. - dd(data);

Illuminate\Support\Collection {#1381 ▼
  #items: array:3 [▼
    0 => {#1380 ▶}
    1 => {#1430 ▶}
    2 => {#1393 ▼
      +"id": 1
      +"title": "company articles"
      +"url": "company-articles"
      +"description": "Information articles and messages from local companies"
      +"parent_id": 1
      +"created_at": "2020-06-07 15:33:24"
      +"updated_at": "2020-06-07 15:33:24"
      +"article_id": 1
      +"menu_id": 9
    }
  ]
}

which is fine

output od second query, i.e. - dd($data);, if i use $url->parent_id

Illuminate\Support\Collection {#1381 ▼
  #items: []
}

which isn't fine

guybrush_threepwood's avatar
Level 33

It seems like you're using the wrong columns to join both tables:

$data = DB::table('menus')
                   ->join('article_categories', 'article_categories.menu_id', '=', 'menus.id')
                   ->where('parent_id', '=', $url->parent_id)
                   ->get();
marsuch's avatar

It seems that the connection is kind of stupid, as soon as I commented on the join, that's how it works. Not as you imagined, but now that I have something wrong with me, it will no longer be a problem to fix. Thanks for your time :-)

1 like
marsuch's avatar

So it only took a few minutes to work, and in the end I found out that the error was in the query that was supposed to secure my category ID. When did I need the category ID as such to look for a descendant. But I was returning the ID of the offspring. So logically it couldn't find anything: D

1 like

Please or to participate in this conversation.