1

Maybe anyone can help me?

This is my controller.

$takegolongan = Pekerjaan::select ('LEFT('golongan_jabatan', 1)')->where('cno', '00001222')->get();

echo $takegolongan;

This is the data that I want to take :

enter image description here

output on the screen (error):

enter image description here

Siti Rahmah
  • 267
  • 1
  • 3
  • 15
  • 1
    Just escape the apostrophe(for the life of me I can't remember what `'` is called in english.): `$takegolongan = Pekerjaan::select ('LEFT(\'golongan_jabatan\', 1)')->where('cno', '00001222')->get(); `. Other than that your query seems correct. – Andrei Feb 03 '17 at 10:06
  • Same thing. Escape the apostrophe. [See here](http://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php). And get a decent IDE, it will warn you about such small issues. – Andrei Feb 03 '17 at 10:13

2 Answers2

1

You can use DB::raw() like this:

use DB;

$takegolongan = Pekerjaan::select(DB::raw('LEFT(`golongan_jabatan`, 1)'))
    ->where('cno', '00001222')
    ->get();
Jeffrey
  • 476
  • 4
  • 13
Jin.C
  • 121
  • 5
  • You changed the query @Jin.C so it will no longer work. You've changed the apostrophe string delimiters around golongan_jabatan for backtick delimiters which is used to denote a schema name. – RichardAtHome Feb 03 '17 at 14:59
1

You should add single quote inside the double quote or you should use backslash, I can only see the issue of quotes. Like:

    $takegolongan = Pekerjaan::select ("LEFT('golongan_jabatan', 1)")->where('cno', '00001222')->get();
Ramesh Mhetre
  • 404
  • 2
  • 12