0

I have the following edit method in my laravel controller:

public function editArtilce(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 . '%')->get();
        return $article;
}

Now since i am doing:

return $article;

I get the following output in the browser:

[{"id":10,"title":"This is a title","description":"This is a description","keywords":"Keyword1 , Keyword2 , Keyword3 , Keyword4 , Keyword5 , Keyword6","blog_content":"<p>I am a lovely burger<\/p>","tag":"gulp","filePath":"2017-02-23-21-54-30-blog-post-image.jpg","slug":"this-is-a-title","created_at":"2017-02-23 21:54:30","updated_at":"2017-02-23 21:54:30"},{"id":11,"title":"This is a title","description":"This is a description","keywords":"Keyword1 , Keyword2 , Keyword3 , Keyword4 , Keyword5 , Keyword6","blog_content":"<p>I am a lovely burger<\/p>","tag":"gulp","filePath":"2017-02-23-21-56-29-blog-post-image.jpg","slug":"this-is-a-title","created_at":"2017-02-23 21:56:29","updated_at":"2017-02-23 21:56:29"}]

But when i try to access the properties of this array like so:

return $article->title

I get the following error:

enter image description here

Why am i unable to access the properties of an array in laravel ? What am i doing wrong ?

2 Answers 2

4

Instead of $article->title try:

$article[0]->title; 

as $article is a Std class object its structure is like:

array(
    {  }  // 0th index
    {  }  // 1st index
)

To make it dynamic use foreach() like:

foreach($article as $data)
{
    $data->id;
    $data->title;
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can use first() instead of get() to get the object directly.

$article = DB::table('admin')
             ->where('slug', 'LIKE', '%' . $urlSlug . '%')
             ->first();

and you have to check if it not null before trying access it's properties. $article->title or whatever you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.