简体   繁体   中英

Select SQL Server Function with Laravel

How can I get the result of a SQL Server function in laravel?

Trying as such:

\DB::connection('database')->select(\DB::raw("SELECT dbo.function(1, 1, 'O', GETDATE(), 5)"));

But getting Cannot Access empty property.

I figured out I can get this to work if I specify an arbitrary table and select the first record. Is there a better way to do this? Seems unnecessarily complicated.

\DB::connection('database')->table('table')
                           ->select(\DB::raw("dbo.function(1, 1, 'O', GETDATE(), 5) as nextDocId"))
                           ->first();

You might be wondering: why not just write this function in php, and the answer to that is because I am connecting to a database backend of a proprietary software in which they've already written stored procedures and functions to handle the same functionality that I'm looking for. I'd like to stick with theirs for consistency.

Well it seems in your first answer that you're not telling laravel which table you want and what results you are looking for.

When using the query builder you will need to use one of the methods(first(), get() etc.) in order to access the data.

If you set your database config file (app/config/database.php) then you won't need to use the connection() method.

Have you tried using Eloquent? This makes things much easier!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM