In this tutorial, you will learn How to get json_decode without quotes in PHP Laravel
In this section, you will know how to get json_decode without quotes in PHP Laravel.
If you created an array with a laravel controller and assign that array to the jquery variable then it looks like below quotes. it seems it's not a JSON array
<script type="text/javascript">
var student = "{{ json_encode($student) }}";
console.log(student);
</script>
Let's see a better solution here:
Controller Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StudentController extends Controller
{
/**
* Generate Image upload View
*
* @return void
*/
public function dropzone()
{
$students = [
[ 'id' => 1, 'name' => 'Dharmik' ],
[ 'id' => 2, 'name' => 'Mehul' ],
[ 'id' => 3, 'name' => 'Keval' ]
];
return view('student-view', compact('students'));
}
}
Solution 1: Blade File Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var users = <?php echo json_encode($students) ?> ;
console.log(students);
</script>
</body>
</html>
Solution 2: Blade File Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var students = @json($students);
console.log(students);
</script>
</body>
</html>
May this example help you.