See table row by column name in Laravel?

Asked

Viewed 453 times

3

At Laravel I’m looking for a record in the database by id table, but the code below only consults if the nomenclature in the column is id

$prod = $this->produto->find(3)

If the column name is prod_id for example, how can I return the record?

1 answer

3


Produto::where('prod_id',123)->get();

or

Produto::where('prod_id',123)->first();

Where Produto is the class of the Model

Updating

Laravel by default uses the column id as id of your Models, to change this and use prod_id in function find() you need change your Model as follows:

class Produto extends Eloquent {

    protected $primaryKey = 'prod_id';

}
  • 2

    Vlw, that’s what I needed

  • @Ivanalves pf put as correct answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.