mardi 2 mai 2017

Laravel Belongs To Not working

this should be quite simple but i don't know what wrong . i have the following migrations . create_players_table

public function up()
    {
        Schema::create('players', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('club_id');
            $table->string('first_name');
            $table->string('last_name');
            $table->double('price', 3, 2);
            $table->enum('position', ['goalkeeper','defender','Midfielder','Striker']);
            $table->timestamps();
        });
    }

create_clubs_table

public function up()
    {
        Schema::create('clubs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('location');
            $table->timestamps();
        });
    }

The relationship is straight forward as one player has one team (One to one) and one team can have many players (One to Many ) . The problem is i want to list all the players with their respective teams but somehow i end up with errors . This is my player model .

class Player extends Model
{
  public function club()
    {
        return $this->belongsTo('App\Club');
    }
}

And here is the controller :

public function index()
{
    $players=Player::find(1);
    echo $players->club()->location;
}

I get this error

ErrorException in TeamController.php line 15: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name

Any help will be appreciated...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire