PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By

Solved

Cannot update a Database Field through the API

Solved
btaylor6192

May 27th, 2022 09:37 AM

Picking up off of this: https://devdojo.com/question/wave-api-help, I am now trying to write migrations for my database fields. However, since attempting to do that I am now unable to update the new fields through the API. It was working prior to trying to convert them to migrations, and now I cannot update new fields even when going back through the UI and creating them. I have confirmed I'm able to update other fields such as email, just none of the new ones, and they stay as the values that I assigned when I created the tables in the migration script. Any idea what could be wrong there?

bobbyiliev

May 29th, 2022 03:57 AM

Hello,

I think that you might have to update the fillable property in the User model at app/User.php.

In there add the new fields that you want to be able to update.

Let me know how it goes!

Best,

Bobby

btaylor6192

May 31st, 2022 08:25 AM

First off, thanks so much for replying, I really appreciate that. A stack overflow post had led me to that, but I wasn't quite sure where to change that, and your answer helped me fill that in. Once I update the fields, do I need to take any action after for it to take effect? It doesn't seem to work still, and even moving a field from fillable to protected still allows me to change it. I have tried restarting Wave, is there something else I need to do?

bobbyiliev

May 31st, 2022 10:16 AM

Hi there,

I just had another look, I think that what you have to do is to go to Voyager -> Tools -> Database -> Edit BREAD -> and in there select the new fields that you've added using your migration.

That should give permissions for those fields to be editable via the API.

Let me know how it goes!

btaylor6192

May 31st, 2022 01:34 PM

I've been able to confirm that indeed works! I've been able to add new fields to my migration and confirmed they are editable now, but yes I had to go in to Voyager, select those fields, and submit them to make them editable. Is there a way to update that BREAD programmatically after doing new migrations?

bobbyiliev

May 31st, 2022 11:49 PM

Best Answer

Hi there!

Ah happy to hear that it is working that way!

I can think of a bit of hacky solution here:

What the save button in the BREAD does is to add a new entry to the data_rows that looks like this:

*************************** 83. row ***************************
          id: 85
data_type_id: 1
       field: your_new_field
        type: text
display_name: Your new field
    required: 0
      browse: 1
        read: 1
        edit: 1
         add: 1
      delete: 1
     details: {}
       order: 16

What you could do is to add an insert statement in your migration, so that the new filed is also added to the data_rows table.

The data_type_id is a foreign key for the data_types where it references your model.

You can use the TCG\Voyager\Models\DataRow to interact with the data_rows table. Example:

\TCG\Voyager\Models\DataRow::create(
          [
              "data_type_id" => 1,
              "field" => "your_new_field",
              "type" => "text",
              "display_name" => "Your New Field",
              "required" => 0,
              "browse" => 1,
              "read" => 1,
              "edit" => 1,
              "add" => 1,
              "delete" => 1,
              "details" => '{}',
              "order" => 16
        ]);
}

Hope that this helps!