Laravel : object implementing __toString(), "object" given

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Laravel : object implementing __toString(), "object" given

Post by gautamz07 »

I have the folllowing route in my route.php file:

Code: Select all

Route::get('/edit/{id}' , [
	'uses' => 'adminController@editArticle',
	'as' => 'editArticle'
]);
The editArticle function looks like the folllowing:

Code: Select all

public function editArticle(Request $request) {
		/* Get the last part of the URI */
		$explodedUrl = explode('/', $request->url());
		$urlSlug = array_pop($explodedUrl);
		$article = DB::table('admin')->where('slug', 'LIKE', '%' . $urlSlug . '%')->first();
		return $article;
		// $tagsArray = $this->getTags();
		return view('pages.editArticle')->with('article', $article)->with('tags' , $tagsArray);
}
now when i type the following into my URL in the browser:

Code: Select all

http://localhost:8080/laravel-blog-production/edit/animated-button-border-using-css-background-and-keyframes
I get the following error:
http://imgur.com/a/tynu6

Code: Select all

The Response content must be a string or object implementing __toString(), "object" given.
Just to clarify , i have a admin table and in it i have a slug column in which i do have a value of

Code: Select all

animated-button-border-using-css-background-and-keyframes
as you must have seen in the url.

I am not sure what is causing this bug and how should i go about resolving this ?

can somebody help me out please.

Thank you.
Gautam.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Laravel : object implementing __toString(), "object" giv

Post by requinix »

Code: Select all

return $article;
That's the bug. Why is that there?
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Laravel : object implementing __toString(), "object" giv

Post by gautamz07 »

@requinix , because i wanna see whats retrived in the $article variable , also if i remove that variable the next line throws the same error.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Laravel : object implementing __toString(), "object" giv

Post by requinix »

You can't just decide to change a return value from a function and expect Laravel to somehow make it work.

Look at the trace and you can tell that $article is a stdClass. That's because it came from a database query using PDO. Laravel doesn't know what to do with a stdClass.

Are you saying that removing that line and uncommenting the next one that defines $tagsArray causes

Code: Select all

return view('pages.editArticle')->with('article', $article)->with('tags' , $tagsArray);
that line to error? What message and what does the trace show?
Post Reply