How to Print a Laravel Variable in Jquery?
Teams applying “How to Print a Laravel Variable in Jquery?” in a production project can use relocation bonus to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Sep 09, 2022
Hi Dev,
This tutorial will provide example of how to print a laravel variable in jquery?. This article will give you simple example of laravel print variable in jquery. you will learn laravel variable in jquery blade. let’s discuss about laravel blade variable in javascript. Let's get started with use laravel blade variable in javascript.
You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.
Sometimes, we need to print the laravel variable in the blade file jquery or javascript section. Then I will give you quick tips to access the laravel variable to the jquery code. we will take three variables string value, integer value, and array value. so let's see both simple examples below:
Example 1:
Blade File Code:
<!DOCTYPE html>
<html>
<head>
</head>
<title>How to Print a Laravel Variable in Jquery? - NiceSnippets.com</title>
<body>
</body>
@php
$name = "Jaydeep Pathar";
$number = 13;
@endphp
<script type="text/javascript">
var name = "{{ $name }}";
console.log(name);
var name = "<?php echo $name ?>";
console.log(name);
var number = "<?php echo $number ?>";
console.log(number);
</script>
</html>
Output:
Jaydeep Pathar
Jaydeep Pathar
13
Example 2:
Controller File Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::take(5)->get();
return view('posts', compact('posts'));
}
}
Blade File Code:
<!DOCTYPE html>
<html>
<head>
<title>How to Print a Laravel Variable in Jquery? - NiceSnippets.com</title>
</head>
<body>
</body>
<script type="text/javascript">
var posts = "{{ Js::from($posts) }}";
console.log(posts);
var posts = ;
console.log(posts);
</script>
</html>
Output:
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial