10

Trying to get Accessors in query builder but throwing error "Undefined property: stdClass::$shorcontent "

//controller
        public function index(){
        $articles = DB::table('articles')->paginate(10);
        return view('articles.index', ['articles' => $articles], compact('articles'));
    }

Here is the Model file with Accessors

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $fillable = [
        'user_id', 'content', 'live', 'post_on'
    ];

    protected $guarded = ['id'];

    public function getShortContentAttribute()
    {

        return substr($this->content,0, random_int(60, 150));
    }
}

Here is the View

//article/index.blade.php View
<td class="col-md-6">{{ $article->shortcontent }} </td>

The same code working when i use eloquent instead of query builder, like this

public function index()
{
    $articles = Article::paginate(10);
    return view('articles.index', ['articles' => $articles], compact('articles'));
}
15
  • Try shortContent. You're definitely missing the t in the name. Commented Jun 30, 2017 at 15:26
  • @aynber throwing error "Undefined property: stdClass::$shortContent" Commented Jun 30, 2017 at 15:29
  • Tried $article->short_content ? Commented Jun 30, 2017 at 15:31
  • @linktoahref tried short_Content and short_content both, again same error Commented Jun 30, 2017 at 15:32
  • 1
    Why are you passing $articles twice in your controller method? Once is enough. Commented Jun 30, 2017 at 16:23

2 Answers 2

11

This answer is late and you might have found your solution, but hope it helps someone else.

Short answer, the DB facade doesn't have access to accessors and mutators defined in the model. Only objects made by instances of the model can have access to accessors and mutators.

I believe the issue here is that using the DB facade only creates the Query Builder without any reference to accessors or mutators you have set in the Article Model. DB facade only queries the database using the query builder and returns an object independent from the Article Model.

However, the Model facade will build a query builder but the instance of the object created will have access to accessors and mutators as it is an object instance of the Article Model class.

Check out this SO answer: Difference between DB and Model facade

Accessors are only accessed once you attempt to retrieve the value of the attribute from the model instance, for example:

$article = Article::find(1);
$shortContent = $article->short_content;

This is explained further here

Thus if you wish to access accessors, then you would have to use the Model facade i.e. Article::paginate(10).

Sign up to request clarification or add additional context in comments.

Comments

4

You are missing to append short_content attribute. Just add this

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{

protected $fillable = [
    'user_id', 'content', 'live', 'post_on'
];
protected $appends = ['short_content'];
protected $guarded = ['id'];

public function getShortContentAttribute()
{

return substr($this->content,0, random_int(60, 150));

}

}

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.