samedi 17 novembre 2018

add slug from a relation

I have a relation between Post and Group BelongsToMany relation.

class Group extends Model
{
 use Sluggable;
 use SluggableScopeHelpers;
 use SoftDeletes;


 public function Posts(){
        return $this->belongsToMany('App\Post');
    }

}

And Post :

class Post extends Model
{


 public function Groups(){
        return $this->belongsToMany('App\Group');
    }

}

so I'm using Sluggable in group model.

I want to use the Post name in the slug for group.

I tried to use Getter to get the post name :

 public function getPostnameAttribute() {

        foreach($this->Posts()->get() as $cc){
            return $cc->name;
        }

it works only if I updated the group. when I created the group it doesn't work.

 * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable()
    {

        return [
            'slug' => [

                'source' => ['post_name','group_name']

            ]
        ];

    }

how I can make this happen?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire