0

I tried to get the data on database using stored procedure - can I use the eloquent?

This is my Eloquent

TriedHard.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TriedHard extends Model
{
    //
    protected $connection = 'sqlsrv2';
}

and I try to call this eloquent on my query like this so I can use a stored procedure.

Controller.php:

$dataprogram =  TriedHard::select('sp_MstShow_CrewProgram_Select');
dump(collect($dataprogram);

but nothing happened, also I can't get my data, how can I solve that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Appem
  • 295
  • 1
  • 3
  • 16
  • Try this [answer](https://stackoverflow.com/questions/44354611/how-to-call-stored-procedure-with-eloquent-laravel) – JsMoreno Apr 10 '20 at 05:44
  • 2
    Does this answer your question? [How to call Stored Procedure with Eloquent (Laravel)?](https://stackoverflow.com/questions/44354611/how-to-call-stored-procedure-with-eloquent-laravel) – Luuklag Apr 10 '20 at 08:09
  • Side note: you should **not** use the `sp_` prefix for your stored procedures. Microsoft has [reserved that prefix for its own use (see *Naming Stored Procedures*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx), and you do run the risk of a name clash sometime in the future. [It's also bad for your stored procedure performance](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix). It's best to just simply avoid `sp_` and use something else as a prefix - or no prefix at all! – marc_s Apr 13 '20 at 10:28

1 Answers1

0

I don't see any use of calling a stored procedure with Eloquent.
Rather just use DB connection to connect specific 'DB' like below:-

$db = DB::connection($DBconnectionName)

and use

 $db = DB::connection($DBconnectionName)->select('sp_MstShow_CrewProgram_Select')
phpdroid
  • 1,642
  • 1
  • 18
  • 36