PHP Developers Network
http://forums.devnetwork.net/

Laravel : object implementing __toString(), "object" given
http://forums.devnetwork.net/viewtopic.php?f=72&t=144018
Page 1 of 1

Author:  gautamz07 [ Fri Jul 28, 2017 4:31 pm ]
Post subject:  Laravel : object implementing __toString(), "object" given

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

Syntax: [ Download ] [ Hide ]
Route::get('/edit/{id}' , [
        'uses' => 'adminController@editArticle',
        'as' => 'editArticle'
]);
 


The editArticle function looks like the folllowing:

Syntax: [ Download ] [ Hide ]
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:

Syntax: [ Download ] [ Hide ]
http://localhost:8080/laravel-blog-prod ... -keyframes
 


I get the following error:
http://imgur.com/a/tynu6

Syntax: [ Download ] [ Hide ]
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
Syntax: [ Download ] [ Hide ]
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.

Author:  requinix [ Fri Jul 28, 2017 4:58 pm ]
Post subject:  Re: Laravel : object implementing __toString(), "object" giv

Syntax: [ Download ] [ Hide ]
return $article;

That's the bug. Why is that there?

Author:  gautamz07 [ Fri Jul 28, 2017 5:01 pm ]
Post subject:  Re: Laravel : object implementing __toString(), "object" giv

@requinix , because i wanna see whats retrived in the $article variable , also if i remove that variable the next line throws the same error.

Author:  requinix [ Fri Jul 28, 2017 5:55 pm ]
Post subject:  Re: Laravel : object implementing __toString(), "object" giv

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
Syntax: [ Download ] [ Hide ]
return view('pages.editArticle')->with('article', $article)->with('tags' , $tagsArray);

that line to error? What message and what does the trace show?

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/