Accessor not working with chunk()?

Posted 10 months ago by Alun

I'm new in Laravel, and checking this forum very often to solve lots of issues while learning. So I tried to create an accessor in one of my models to basically convert the colum "title" into a slug. I followed several answers from this forum, but none of them seems to work for me, basically the method I created doesn't get executed at all. Here's the code:

// app/Http/Models/Competence.php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use App\Http\Helpers\StringHelper;

class Competencia extends Model {
    protected $table = 't_competences';
    public $timestamps = false;
    protected $fillable = ['name', 'other_non_relevant_fields'];
    protected $appends = ['slug'];
    // ...
    public function getSlugAttribute(){
        return (new StringHelper())->slug($this->nombre);
    // ...
<!-- app/resources/parts/competences.blade.php -->

@foreach($competences->chunk(2) as $c)
        <!-- Definition of some vars, I know it's a bad practice, but it's a need -->
        $classname = 'my-classes'
    <div id="{{ $c->slug }} class="{{ $classname }}>
        <!-- some more code -->

What I don't get to understand is why, if I remove the "chunk(2)" method, which I need so I can split the collection in two columns, the accessor works, but with it I get the exception "Property [slug] does not exist on this collection instance.".

Any idea of why this happens or any way to split this collection?

Thanks very much.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.