Diego Alves
Diego Alves

Reputation: 2677

How to map exting table to a model in laravel

I have mysql table named casa that have a primary key named casa_id. I setted up a model:

 class Casa extends Model
{
 protected $table = 'casa';
public $casa_id;
public $anuncio_id;
public $banheiros;
public $capacidade_garagem;
public $dormitorios;
public $piscina;
public $preco;
public $dir_id;
public $cep;
public $numero_fotos;
public $visualizacoes;
public $ativo;
public $descricao;

public $timestamps = false;

protected $connection = 'eloquent_db';


}

Executing this code

$casa = \App\Casa::find($id);

I get his error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'casa.id' in 'where clause' (SQL: select * from casa where casa.id = 238 limit 1)

I don't know Eloquent but I assume that some conventioin is missing. Can you give the directives to map my existing table to a model so that I can use Eloquent. I am more of database first guy but I'm learning web api and don't want to deviate from my path and start writing SQL commands. My table columns have the exact names of my model.

code for the operation I want to perform:

$casa = \App\Casa::find($id);
$casa->dormitorios = 5;
$casa->save();

Upvotes: 2

Views: 1236

Answers (1)

voodoo417
voodoo417

Reputation: 12111

Change it to:

class Casa extends Model{
  protected $table = 'casa';
  protected $primaryKey = 'casa_id';
  protected $connection = 'eloquent_db';
  public    $timestamps = false;
} 

$casa = \App\Casa::find($id);

Upvotes: 1

Related Questions