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

jrdavidson's avatar

presenter not showing right value

I'm trying to figure out how to work with this so that I can put together a name together which uses the event name and a number that goes with it.

Inside of my events table the second field is event_name_id which represents the name I have the relationship set up in the Entity class.

Right now event event gets returned a 0 for some reason.

public function status()
{
    return $this->belongsTo('Backstage\Entities\EventStatus', 'event_status_id');
}
<?php namespace Backstage\Presenters;

class Event extends Presenter{

    public function name()
    {
        return ucfirst($this->entity->event_name) . ' ' . $this->entity->label;
    }
}
0 likes
28 replies
pmall's avatar

Your Status belongs to EventStatus but you are showing Event model ?

jrdavidson's avatar
<?php namespace Backstage\Entities;

use Backstage\Presenters\Contracts\PresentableInterface;
use Backstage\Presenters\PresentableTrait;

class EventStatus extends \Eloquent  implements PresentableInterface {

    use PresentableTrait;

    protected $presenter = 'Backstage\Presenters\EventStatus';

    protected $fillable = [];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'event_statuses';

}
nolros's avatar

xtereme a little confusing to piece your code together, as you are showing a presenter for Event but you are then reference EvenStatus in the model you showing, but let me take a shot here:

  1. In events status you would use your EventStatus, whichis what you have now.
class EventStatus extends \Eloquent  implements PresentableInterface {

    use PresentableTrait;

    protected $presenter = 'Backstage\Presenters\EventStatus';
  1. Inside of event you would have Event presenter
class Event extends \Eloquent  implements PresentableInterface {

    use PresentableTrait;
   // needs to be Event
    protected $presenter = 'Backstage\Presenters\Event';
  1. Inside or your Event model you have the following relationship:
public function status()
{
    return $this->belongsTo('Backstage\Entities\EventStatus', 'event_status_id');
}
  1. You want a presenter to present the event_name and label. I'm assuming label is event status?
class Event extends Presenter{

 public function name()
    {
    // this will work as $this is referencing the event model with the column event_name 
        $upperCaseEventName = ucfirst($this->entity->event_name) ;
 
       // the problem is here $this->entity->label; 
    // if label doesnt exist in Events table it wont find it.
    // if you want to use the relationship, which I assume is why you posted it you will need to reference it
    $getLabelFromRelationship = $this->name()->entity->label;
    // btw, I dont know if you can extend entity through a relationship, cannot see why not, but have not tested it
    // Jarek would know
    
    // verbose
    return $upperCaseEventName . ' ' . getLabelFromRelationship;

    // summary
    return ucfirst($this->entity->event_name) . ' ' . $this->name()->entity->label;

        // alternatively you could setup a relationship so you don't need use a relationship presenter on event status for the        
    return $this->belongsTo('Backstage\Entities\EventStatus', 'event_status_id')->select(['label']);

    // which would be even simpler 
    return ucfirst($this->entity->event_name) . ' ' . $this->name();

    }
JarekTkaczyk's avatar

@xtremer360 It's still unclear. Where does event_name come from? The same goes for the label. The model itself doesn't say a word. It's hard to guess. Show all relevant info and I can help you.

jrdavidson's avatar

I'll provide my database schema with some sample data and provide some dummy data.

Table: events
id  event_name_id   label 
1       1               1
2   5               1
3   1               2

Table: event_names
id     name
1       My Event
2      Your Event
3      Bob's Event

For each event it contains the incremental id and an id for the name of the event which is the foreign key for the event_names table. The label is just a number that represents the next number in the series.

JarekTkaczyk's avatar

@xtremer360 You're not making it easy to help you :)

You can add this accessor to your event model, then the presenter should work as expected:

// Event model
public function getNameAttribute()
{
  // let eventName be the belongsTo relation to event_names table
  return ($relation = $this->eventName) ? $relation->name : null;
}
jrdavidson's avatar

I'm very sorry @JarekTkaczyk you've been very willing to help but I've been on this issue for days now and getting frustrated because in addition to trying to receive help I'm also trying to get this on my own but am not succeeding.

With the the suggested code snipplet you supplied above I now have the error of this saying its coming from my table partial.

Undefined property: Backstage\Entities\Event::$eventName
@foreach ($events as $event)
            <tr>
                <td>{{ $event->id }}</td>
                <td>{{ $event->present()->name }}</td>
                <td>{{ $event->present()->booking_date }}</td>
                <td>{{ $event->arena->name }}</td>
                <td>{{ $event->arena->location }}</td>
                <td>{{ $event->status->name }}</td>
                <td>Action Buttons Here</td>
            </tr>
        @endforeach
jrdavidson's avatar

If I use the JSONFormatter extention in Chrome and return the variable to see what the array of objects look like then I have also provided the following output.

<?php

use Backstage\Services\EventCreatorService;
use Backstage\Repositories\Events\EventRepositoryInterface;
use Backstage\Repositories\EventNames\EventNameRepositoryInterface;
use Backstage\Repositories\Arenas\ArenaRepositoryInterface;

class EventsController extends \BaseController {

    protected $eventCreator;
    protected $event;
    protected $eventName;
    protected $arena;

    public function __construct(EventCreatorService $eventCreator, EventRepositoryInterface $event, EventNameRepositoryInterface $eventName, ArenaRepositoryInterface $arena)
    {
        $this->eventCreator = $eventCreator;
        $this->event = $event;
        $this->eventName = $eventName;
        $this->arena = $arena;
    }
    
    /**
    * Display a listing of the resource.
    * GET /events
    *
    * @return Response
    */
    public function index()
    {
            $events = $this->event->getAllWithStatusAndArena();
            return $events;
            //return View::make('events.index', compact('events'));
    }
}
{
    id: "1",
    event_name_id: "3",
    label: "0",
    event_status_id: "3",
    booking_date: "1991-11-14 06:25:24",
    arena_id: "8",
    created_at: "2015-01-18 18:18:11",
    updated_at: "2015-01-18 18:18:11",
    deleted_at: null,
    status: {
        id: "3",
        name: "Editing",
        created_at: "2015-01-18 18:18:11",
        updated_at: "2015-01-18 18:18:11",
        deleted_at: null
    },
    arena: {
        id: "8",
        name: "Arena 8",
        location: "Port Nelsonstad, North Carolina",
        created_at: "2015-01-18 18:18:11",
        updated_at: "2015-01-18 18:18:11",
        deleted_at: null
    }
},
@foreach ($events as $event)
<tr>
    <td>{{ $event->id }}</td>
    <td>{{ $event->present()->name }}</td>
</tr>
@endforeach
<?php namespace Backstage\Entities;

use Backstage\Presenters\Contracts\PresentableInterface;
use Backstage\Presenters\PresentableTrait;

class Event extends \Eloquent implements PresentableInterface {

    use PresentableTrait;

    protected $presenter = 'Backstage\Presenters\Event';

    protected $fillable = [];

    protected $dates = ['booking_date'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'events';

    public function getNameAttribute()
    {
        // let eventName be the belongsTo relation to event_names table
        return ($relation = $this->eventName) ? $relation->name : null;
    }
}
<?php namespace Backstage\Presenters;

class Event extends Presenter{

    public function name()
    {
        return ucfirst($this->entity->name) . ' ' . $this->entity->label;
    }

    public function booking_date()
    {
        return $this->entity->booking_date->format('F d, Y');
    }
}
JarekTkaczyk's avatar

@xtremer360 I have no idea why you have name as separate table. Anyway, change the accessor to getNameAttribute - like in the answer above, and call it in your presenter as $entity->name.

jrdavidson's avatar

@JarekTkaczyk I have edited my above reply and have included all information. I hope this helps you because I unfortunately am still receiving a 0 as the output for the name.

JarekTkaczyk's avatar

@xtremer360 You don't have relation setup, do you?

I assume you do have EventName model for the event_names table.

    public function eventName()
    {
        return $this->belongsTo('Backstage\Entities\EventName');
    }
jrdavidson's avatar

I'm sorry I do have that in their set up I was taking out unnecessary code and must have forgotten to keep that in there. This is from the Event Eloquent model.

public function name()
{
        return $this->hasOne('Backstage\Entities\EventName', 'event_name_id');
}
JarekTkaczyk's avatar

@xtremer360 But this is not the relation I showed. It's wrong, it is belongsTo not hasOne since you have foreign key on the events table, not the other way around.

jrdavidson's avatar

Oh hmm. So I change it to the following but still receiving the same output.

public function name()
{
        return $this->belongsTo('Backstage\Entities\EventName', 'event_name_id');
}
JarekTkaczyk's avatar
Level 53

@xtremer360 Mate, if you don't read what I write. It's really hard to reach your goal...

Watch this:

public function getNameAttribute() {...}
$entity->name; // calls the accessor above

public function name() { // relation }
// never called, because of the accessor...

So use the eventName for the relation like I showed. getNameAttribute for the accessor and $entity->name in the presenter and it must work.

1 like
jrdavidson's avatar

Oh wow halfway home. Now when I changed that it changes the output to [correct event name here] 0. So it just adds in the name however it doesn't attach the label.

jrdavidson's avatar

It is just a value straight from the events table. So there is no relation or accessor.

Please or to participate in this conversation.