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

Cronix's avatar

It looks to me like an Event hasOne category (instead of belongsTo). A Category can have many events.

Snapey's avatar
Snapey
Best Answer
Level 122

So, when you write Event::with('categories') you are saying to use the categories() method on the Event model as the definition for the relationship.

Its that that is broken, you can ignore everything else.

By testing in reverse, you have proven that your category_id column is correct on your events table.

So, my assumption is that it is something to do with the pluralisation.

Now then since Event has the category_id column, it means that Event BelongsTo the Category, and only ONE category, but your relationship is called categories. I believe this messes with laravel magic Foo

rename the relationship category to reflect its singular nature, and all should be well.

Snapey's avatar

As an extra tip

    <div>
        <select name="category_id" id="category_id" class="form-control" required>
            @foreach ($categories as $category)
                <option value="{{ $category->id}}"
                    @if($category->id == $event->category_id) selected @endif >
                    {{ $category->categoryName}}</option>
            @endforeach
      </select>
    </div>
chrisgrim's avatar

Hi Snapey,

I believe that did it! When I changed my event.php to

public function category() 
    {
        return $this->belongsTo(Category::class);
    }

When I tinker

$e = App\Event::with('category')->find(14);

I get

App\Event {#2956
     id: 14,
     user_id: 11,
     category_id: 9,
     organizer_id: 11,
     eventTitle: null,
     slug: "468432130",
     eventDescription: null,
     eventWebsite: null,
     eventPrice: null,
     eventTicketUrl: null,
     eventExpectations: null,
     eventStreetNumber: null,
     eventStreetAddress: null,
     eventCity: "Cotati",
     eventState: null,
     eventCountry: null,
     eventZipcode: null,
     eventLong: null,
     eventLat: null,
     overallRating: "0",
     eventImagePath: null,
     thumbImagePath: null,
     approved: 0,
     visitors: 0,
     created_at: "2019-03-13 22:25:09",
     updated_at: "2019-03-14 17:20:19",
     category: App\Category {#2962
       id: 9,
       event_id: null,
       categoryName: "Fahey-Daugherty",
       slug: "fahey-daugherty",
       categoryDescription: "Architecto autem dolor esse maxime. Iure dignissimos id magnam quae dolorem sint sed deleniti. Et modi natus debitis voluptatum. Tempore soluta expedita ea nostrum eius.",
       categoryImagePath: "category/992f662810a8c156a2971ea7d1a548ba.jpg",
       created_at: "2019-03-12 00:25:28",
       updated_at: "2019-03-12 00:25:28",
     },
   }

Thank you @snapey and @cronix so much for all your help!! You not only helped solve my problem but you helped me understand why it was erroring out too.

Cronix's avatar

You're welcome. Sorry for adding to confusion. There were a lot of moving parts here and some errors were just because of tinker, which muddied the water a bit. It sounds like it's working, so please close the thread and mark Snapey's post as the solution.

Previous

Please or to participate in this conversation.