2

I have some difficulties in laravel query builder.

I have a syntax like this :

$data['tmasuk'] =TSediam::where('kd_lokasi', $kd_lokasi)->where('kd_brg', $kd_brg)->where('DATEPART(YYYY,tgldok)',$tgldok)->get();

it says error

An uncaught Exception was encountered Type: Illuminate\Database\QueryException Message: SQLSTATE[42S22]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'DATEPART(YYYY,tgldok)'. (SQL: select * from [t_sediam] where [kd_lokasi] = 023040500414964000KD and [kd_brg] = 1010302001000001 and [DATEPART(YYYY,tgldok)] = 2015) Filename: C:\wamp\www\sedia\vendor\illuminate\database\Connection.php Line Number: 651

**

But, if I check the query via sql server management studio it's work

select * from [t_sediam] where [kd_lokasi] = '023040500414964000KD' and [kd_brg] = '1010302001000001' and DATEPART(YYYY,tgldok) = '2015'

So, what is the solution? thanks

2 Answers 2

1

Try to use DB::raw:

$data['tmasuk'] = TSediam::where('kd_lokasi', $kd_lokasi)
                          ->where('kd_brg', $kd_brg)
                          ->where( \DB::raw( "DATEPART(YYYY,tgldok) = '" . $tgldok . "'"  ) )
                          ->get();
Sign up to request clarification or add additional context in comments.

5 Comments

It says eror Message: Class 'DB' not found
try with \DB::raw instead of DB::raw
Then try to specify this: use Illuminate\Support\Facades\DB; (or only use DB; in the top of your file, and use DB::raw
it says error Message: A facade root has not been set.
you have some problem accessing your Facades, does the other Facades work on your application ?
0

You can't query a where like that;

->where('DATEPART(YYYY,tgldok)',$tgldok)

means you are searching for a field by the name DATEPART(YYYY,tgldok)

try something like this:

$data['tmasuk'] = TSediam::select('DATEPART(YYYY,tgldok) AS datepart')
  ->where('kd_lokasi', $kd_lokasi)
  ->where('kd_brg', $kd_brg)
  ->where('datepart',$tgldok)
  ->get();

2 Comments

it says error Message: SQLSTATE[42S22]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'datepart'. (SQL: select [DATEPART(YYYY,tgldok)] as [datepart] from [t_sediam] where [kd_lokasi] = 023040500414964000KD and [kd_brg] = 1010302001000001 and [datepart] = 8)
i haven't used ODBC driver, check the reference for how to write a SELECT ... AS .. query..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.