voyager: can't save when Bread model has a multi select with relationship

  • Laravel Version: 5.4.*
  • Voyager Version: 0.11.7
  • PHP Version: 7.0.13
  • Database Driver & Version:

Description:

I can’t save custom Bread form when it has a multi select with relationship . laravel thown the error:

BadMethodCallException in Builder.php line 2450: Call to undefined method Illuminate\Database\Query\Builder::tags_id()

Steps To Reproduce:

model news

class News extends Model
{
    public $timestamps = false;
    public function tagsId(){
      return $this->belongsToMany(Tag::class);
    }
}

model tag class Tag extends Model { public $timestamps = false; } news Bread 2 tags BREAD 3 table:news_tag 4 then i add/edit news ,I got the error 1

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (12 by maintainers)

Most upvoted comments

Got it “working”, but it’s not the perfect solution. If you like to try it: In the voyager\src\Http\Controllers\Traits\BreadRelationshipParser.php line 103 Replace

if (!is_object($item[$field])) {
    $item[$field] = $relation[$relationData->label];
} else {
    $tmp = $item[$field];
    $item[$field] = $tmp;
}
if (isset($relationData->page_slug)) {
    $id = $relation->id;
    $item[$field.'_page_slug'] = url($relationData->page_slug, $id);
}

for

$tmp = $item[$field];
$item[$field] = $tmp;

And in the browse.blade.php line 42 replace the “multiple block” for

@elseif($row->type == 'select_multiple')
@if(property_exists($options, 'relationship'))
    
        @foreach($data['relations'][$row->field] as $item)
        
        <span class="label label-info">
          {{ $item->title}}  
        </span>
        @if(!$loop->last)
           
            @endif
        @endforeach

    @elseif(property_exists($options, 'options'))
        @foreach($data->{$row->field} as $item)
         {{ $options->options->{$item} . (!$loop->last ? ', ' : '') }}
        @endforeach
    @endif

In my case I’m not using the link to the item. If you want to use it just replace the <span> for

<a href="{{ $options->relationship->page_slug}}/{{$item->id}}">
 {{ $item->title}}  
</a>

voyager_relations voyager_browse voyager_result

Not the perfect soultion but it may help