Search using model

0

I would like to use my model to do research in Laravel.

I would like, if possible, someone could explain me:

I have my model

class minhaModel extends Model
{
    protected $table = "minha_model";
    public $timestamps = false;
    protected $primaryKey = 'ID_CAMPO';
    public $incrementing = false;
    public $sequence = 'MINHA_SEQUENCIA';
}

What I want to know is why this works:

    use Illuminate\Support\Facades\DB;
    /* ... */
    $model = DB::select( 'SELECT * FROM minha_model WHERE campo1 = ?', array( $codigo ) )

And this does not work here:

   use App\HamOcorrenciasAnvisa;
   /* ... */
   $model = MinhaModel::where( 'campo1', $codigo );

The table is:

id_campo | campo1 | campo2
  1      |  123   |  321
  2      |  222   |  333

I'm starting with Laravel.

    
asked by anonymous 20.10.2017 / 14:13

1 answer

1

In fact, everything is fine but incomplete.

At this moment:

$model = MinhaModel::where( 'campo1', $codigo );

Just get Builder , you are not yet extracting the results, so you only need to add ->get(); :

$model = MinhaModel::where( 'campo1', $codigo )->get();
    
20.10.2017 / 14:26