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

warpig's avatar
Level 12

Livewire can't insert something, deployed code

I can't make a new Idea when I try to do so on the deployed website, https://playazchoice.herokuapp.com I can locally but not there, why? It gives a 500 Server Error message

CreateIdea.php

<?php

namespace App\Http\Livewire;

use App\Models\Idea;
use Livewire\Component;
use App\Models\Category;
use Illuminate\Http\Response;


class CreateIdea extends Component
{
    public $title;
    public $category = 1;
    public $description;

    protected $rules = [
        'title' => 'required|min:4',
        'category' => 'required|integer',
        'description' => 'required|min:4',
    ];

    public function createIdea()
    {
        if (auth()->check()) {
            $this->validate();
            
            Idea::create([
                'user_id' => auth()->id(),
                'category_id' => $this->category,
                'status_id' => 2,
                'title' => $this->title,
                'description' => $this->description,
            ]);

            session()->flash('success_message', 'Idea was added successfully');

            $this->reset();
            
            return redirect()->route('idea.index');
        }
        abort(Response::HTTP_FORBIDDEN);
    }

    public function render()
    {
        return view('livewire.create-idea', [
            'categories' => Category::all(),
        ]);
    }
}

If it works locally... and I receive no errors or warnings...all the settings ever needed to run a database are there, I been migrating and running my seeds now with no problem but I can't make use of a form sadly.

<form wire:submit.prevent="createIdea" action="#" method="POST" class="space-y-4 px-4 py-6">
    <div>
        <input 
            wire:model.defer="title"
            type="text"
            class="w-full border-none bg-gray-100 text-sm rounded-xl text-gray-900 placeholder-gray-500 px-4 py-2"
            placeholder="Your idea"
            required
        >
        @error('title')
            <p class="text-red text-xs mt-1">{{ $message }}</p>
        @enderror
    </div>
    <div>
        <select 
            wire:model.defer="category"
            name="category_add"
            id="category_add"
            class="w-full bg-gray-100 rounded-xl px-4 py-2 border-none text-gray-900 text-sm"
            required
        >
            @foreach ($categories as $category)
                <option value="{{ $category->id }}">{{ $category->name }}</option>
            @endforeach
        </select>
    </div>
        @error('category')
            <p class="text-red text-xs mt-1">{{ $message }}</p>
        @enderror
    <div>
        <textarea 
            wire:model.defer="description" 
            name="idea" 
            id="idea" 
            cols="30" 
            rows="4" 
            class="w-full bg-gray-100 rounded-xl placeholder-gray-500 text-sm px-4 py-2 border-none text-gray-900"
            placeholder="Describe the idea"
            required></textarea>
                @error('description')
                    <p class="text-red text-xs mt-1">{{ $message }}</p>
                @enderror
    </div>
    <div class="flex items-center justify-between space-x-3">
        <button 
            type="button"
            class="flex items-center justify-center w-1/2 h-11 text-xs bg-gray-200
            font-semibold rounded-xl border border-gray-200 px-6 py-3 text-black
            hover:border-gray-400 transition duration-150 ease-in"
        >
            <svg class="text-gray-500 w-4 transform -rotate-45" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13" />
            </svg>
            <span class="ml-1">Attach</span>
        </button>
        <button 
            type="submit"
            class="flex items-center justify-center w-1/2 h-11 text-xs bg-blue
            font-semibold rounded-xl border border-blue px-6 py-3
            hover:bg-blue-hover transition duration-150 ease-in"
        >
            <span class="ml-1">Submit</span>
        </button>
    </div>

    <div>
        @if (session('success_message'))
            <div 
                x-data="{ isVisible: true }"
                x-init="
                    setTimeout(() => {
                        isVisible = false
                    }, 5000)
                "
                x-show.transition.duration.1000ms="isVisible"
                class="text-gray-900 p-2 text-center mt-4">
                {{ session('success_message') }}
            </div>
        @endif
    </div>

</form>
0 likes
11 replies
Snapey's avatar

server error? Look in Laravel log file

warpig's avatar
Level 12

@Snapey yea I get this:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`heroku_74d4fb19f97e161`.`ideas`, CONSTRAINT `ideas_status_id_foreign` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`)) (SQL: insert into `ideas` (`user_id`, `category_id`, `status_id`, `title`, `description`, `slug`, `updated_at`, `created_at`) values (305, 5, 2, new idea, asdfdsfdsa, new-idea, 2021-10-16 23:49:12, 2021-10-16 23:49:12))
Snapey's avatar

so you have not seeded your statuses table?

warpig's avatar
Level 12

@Snapey yeah everytime i seed my table actually I don't get the correct numbers and I don't know why though, probably how Heroku works or something, i get this instead:

=> Illuminate\Database\Eloquent\Collection {#4312
     all: [
       App\Models\Status {#3906
         id: 5,
         name: "Closed",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
       App\Models\Status {#4274
         id: 15,
         name: "Open",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
       App\Models\Status {#4521
         id: 25,
         name: "Considering",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
       App\Models\Status {#4522
         id: 35,
         name: "In Progress",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
       App\Models\Status {#4523
         id: 45,
         name: "Implemented",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
     ],
   }
>>> App\Models\Category::get();
=> Illuminate\Database\Eloquent\Collection {#4527
     all: [
       App\Models\Category {#4524
         id: 5,
         name: "Category 1",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
       App\Models\Category {#252
         id: 15,
         name: "Category 2",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
       App\Models\Category {#4467
         id: 25,
         name: "Category 3",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
       App\Models\Category {#4469
         id: 35,
         name: "Category 4",
         created_at: "2021-10-16 23:58:28",
         updated_at: "2021-10-16 23:58:28",
       },
     ],
   }
Snapey's avatar

it shouldn't matter what the ids are, your code should not know them

nothing to do with heroku this is how mysql works

Snapey's avatar

how did your task get assigned status code of 2?

warpig's avatar
Level 12

@Snapey uh status code of 2 is actually the value for the "Open" status, anytime i create a new task from scratch it should have that label on the card

so changing that number from 2 to 15 gets rid of that but now it complains about the category_id, and i fixed the category issue too by just changing the numer to 5, omg is there any way to manage here?

Snapey's avatar

changing the code to 15 is not the answer - now your code will have the same issue locally.

If you are going to hard-code the value, why even have a database table?

you should find the status with the name 'open' and then use its id. Usually, for instance this would be via reading back the selected id from a form select input

you shouldn't have 'magic numbers' in your code

Please or to participate in this conversation.