0

I upgraded to laravel 5.5 and Voyager 1.0 and am using mysql on a vagrant box.

I want to add an entry on the data_rows table for a relationship between a User and a Store. I'm able to create the entry using Voyager's gui.

creating relationship gui view of relationship Which creates the corresponding table entry enter image description here When i copy all of these values into my seeder

$dataRow = $this->dataRow($storeDataType, 'store_belongsto_user_relationship');
if (!$dataRow->exists) {
    $dataRow->fill([
        'type'         => 'relationship',
        'display_name' => 'Manager',
        'required'     => 0,
        'browse'       => 1,
        'read'         => 1,
        'edit'         => 1,
        'add'          => 1,
        'delete'       => 1,
        'details'      => '{"model":"App\\User","table":"users","type":"belongsTo","column":"manager_id","key":"id","label":"email","pivot_table":"ad_store","pivot":"0"}',
        'order'        => 20,
    ])->save();
}

and run my db migrations with my custom seeders, If I reload the /database/stores/bread/edit page I get an error thrown for accessing the property of a non-object triggered on this line.

<?php if($relationshipDetails->type == 'belongsTo'): ?><?php echo e('flexed'); ?><?php endif; ?>

Which implies that the json stored in the details field is invalid. I don't understand how that can be though when I copied it to my seeder verbatim. Is there some other entry added on another table or cached that my seeder doesn't account for? I've crawled through the db entries and can't find anything obvious.

EDIT

Adding this line in reference to the question as i discovered it was relevant to the problem

@php $relationshipDetails = json_decode($relationship['details']); @endphp
WhyAyala
  • 647
  • 7
  • 29

2 Answers2

1

So this was sort of idiotic on my part. My seeder was inserting a string that i defined as such; "model":"App\\User", which i thought was sufficient for getting the \ character into the db. Which it is. The problem is in the last line that i added to the question.

json_decode($relationship['details'])

When this function is called it tries to create a json object from the string in the data base. Unfortunately, it doesn't create the json object successfully because the seeder inserted the string "model":"App\User" into the database.

To fix this, I changed to string declaration in the seeder to

"model":"App\\\\User"

which inserted into my database as

"model":"App\\User"

and when it gets passed json_decode it becomes

"model": "App\User"

let this be an important lesson in escape characters.

WhyAyala
  • 647
  • 7
  • 29
1

i know this is for years ago but i'll answer that for who are new in voyager and have this question.

a simple answer : Use php array in details.
for example :

$dataRow = $this->dataRow($storeDataType, 'store_belongsto_user_relationship');
if (!$dataRow->exists) {
    $dataRow->fill([
        'type'         => 'relationship',
        'display_name' => 'Manager',
        'required'     => 0,
        'browse'       => 1,
        'read'         => 1,
        'edit'         => 1,
        'add'          => 1,
        'delete'       => 1,
        'details'      => [
            "model"=>"App\\User",
            "table"=>"users",
            "type"=>"belongsTo",
            "column"=>"manager_id",
            "key"=>"id",
            "label"=>"email",
            "pivot_table"=>"ad_store",
            "pivot"=>"0",
        ],
        'order'        => 20,
    ])->save();
}

  • I haven't touched laravel or voyager in a few years. I don't know if this would work but I'd be willing to try and test it in my freetime. What version of voyager did this work for you? – WhyAyala Oct 15 '20 at 13:32
  • @WhyAyala it works on ^1.4 . I wasn't check it on other versions . if you checked on another version make me aware. ty – Seyed Mohammadreza Oct 31 '20 at 10:04