Search using model

Asked

Viewed 53 times

0

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

I wish that, if possible, one could explain to 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 it doesn’t 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.

  • And if you do: $model = MinhaModel::where( 'campo1', $codigo )->get();?

  • 1

    That’s right. It worked. Thank you very much!

  • Can you add answer, please

  • 1

    Placed below (; obfuscated by the response requirement

1 answer

1


In reality it’s all right, but incomplete.

At this time:

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

You just get the Builder, you are not yet extracting the results, for that you just need to add ->get();:

$model = MinhaModel::where( 'campo1', $codigo )->get();
  • 1

    what show this answer

Browser other questions tagged

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