Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel accessors using Query Builder

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'));
}
like image 884
Rahul Sharma Avatar asked Jun 30 '17 15:06

Rahul Sharma


People also ask

How does Laravel query builder work?

In Laravel the database query builder provides an easy interface to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. and it works on all supported database systems like a champ.

What is query Builder in Laravel with example?

Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems.

What is the difference between query builder and eloquent?

Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.

What is accessors in Laravel?

Accessors and mutators allow you to format Eloquent attributes when retrieving them from a model or setting their value. For example, you may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model.


2 Answers

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).

like image 73
Dominic Avatar answered Oct 21 '22 05:10

Dominic


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));

}

}
like image 33
schumskie Avatar answered Oct 21 '22 06:10

schumskie