1

I'm getting an error :

"Call to undefined method Illuminate\Database\SqlServerConnection::find()"

I'm trying to update a table that was executed using stored procedure.

public function update(Request $request)
{
    $cont = DB::find($request->ContainerId);

    $cont->update($request->all());

    return back();
}

I wanted to update a record in SQL Server but no luck at all.

Rwd
  • 34,180
  • 6
  • 64
  • 78
koko_dump
  • 35
  • 9

2 Answers2

1


There is 2 way for find( select one row ) from SQL :

  1. Using Query Builder ( DB facade )

You need define table first :

$cont = DB::table("your_table_name_here")->find($request->ContainerId);
  1. Using Eloquent

You need to use your Model :

$cont = YourModelHere::find($request->ContainerId);

or for handling exceptions :

$cont = YourModelHere::findOrFail($request->ContainerId);

I hope this helps you !

Edit : 2019/05/26

Which is better ?

For Find or work with few records :

i prefer to use Eloquent because its simple , easy , more readable and its for all types of SQL ! ( Thanks to Martin Henriksen comment)

In General :

Why we should use Query Builders for lots of records instead of Eloquent ?

Royal_MGH
  • 766
  • 4
  • 8
  • 1
    You are correct, but in general i think it is a bad habit to utilize DB table logic in your app, it goes against the way Laravel is intended. – mrhn May 26 '19 at 08:26
  • Nice , Eloquent is another way ! But we cant recommend always eloquent ! I'm agree with you in this situation Eloquent is better because result is only one row ! But in deeper comparison Eloquent is not good for lots of records . Edited ! Thank you ! – Royal_MGH May 26 '19 at 17:51
0

DB is in general an anti pattern in Laravel applications and the DB does not have a find method either, it has methods for raw SQL queries.

Instead utilize your model, use the static methods there (like find or findOrFail). The table name is generated from a pluralized version on the class name, so the table name should be containers

public class Container extends Model {
}

Container::find($request->ContainerId);
mrhn
  • 17,961
  • 4
  • 27
  • 46