Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

madsynn's avatar

Creating an Accessor need guidance

I have two cruds and want to create an accessor to be used on queries to return boolean and not really sure how to do it.

Have the following details for the models

// doctype.php

protected $fillable = [
        'title',
        'slug',
    ];

public function Docs()
 {
        return $this->belongsToMany(Doc::class);
 }
// doc.php

 protected $fillable = [
        'published',
        'set_name',
        'industry',
        'slug',
        'status_id',
    ];

public function Doctypes()
 {
        return $this->belongsToMany(DocType::class);
 }

I need a doc scope that will query the doctype and @if current doc has a doctype wth id == 2 return true @else return false @endif

Like i said i have never created one before and could use your help.

Thank you

0 likes
2 replies
madsynn's avatar

So do i create a scope to return boolean from doctype if its id == 1 (id of type I am trying to get)

    /**
     * Scope a query to return true if doctype id == 1
     *  1 being the title mcode I am trying to check on
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeIsMcode($query)
    {
        return $query->where('id', 1);
    }

so how does the doc.php accessor use the scope in doctype.php to return true if the doc / doctype is mcode in this case? This is where I get confused. will an accessor only work on data from itself and not traverse to the model with relationship of belongsToMany?

Please or to participate in this conversation.