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'));
}
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.
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.
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.
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.
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)
.
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));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With