What is the name of the blade file that contains the JavaScript code?
Nov 23, 2023
14
Level 4
Undefined variable $tasks
Im working on a todo list for my application but i keep getting the error at subject "Undefined variable $tasks"
here is my code
CONTROLLER
public function list_task() {
// Get the currently authenticated user
$user = Auth::user();
// Initialize an empty array for tasks
$tasks = [];
// Check if the user is authenticated
if ($user) {
// Retrieve tasks for the authenticated user
$tasks = todolist::where('posted_by', $user->id)->get();
} else {
// Handle the case where the user is not authenticated
session()->flash('notification', ['type' => 'error', 'message' => 'User not authenticated']);
return redirect()->route('login'); // Redirect to the login page or handle as needed
}
return view('dashboard', ['tasks' => $tasks]);
}
Javascript
$(document).ready(function() {
// Add CSRF token to all AJAX requests
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
// Handle Enter key press on the input field
$('.tdl-new').keypress(function(event) {
if (event.which === 13) { // Check if the key pressed is Enter (key code 13)
event.preventDefault(); // Prevent the default behavior of the Enter key (form submission)
// Get the task from the input field
var task = $(this).val().trim();
if (task !== '') {
// Make an Ajax request to save the task
$.ajax({
url: '{{ route("save-todo") }}', // Use the correct route name
method: 'POST',
data: { todo: task },
success: function(response) {
// Rest of the code...
},
error: function(error) {
// Rest of the code...
}
});
}
}
});
// Handle checkbox change event
$('input[type="checkbox"]').change(function() {
var taskId = $(this).data('task-id');
var status = $(this).is(':checked') ? 1 : 0;
// Make an Ajax request to update the task status
$.ajax({
url: '{{ route("update-todo-status", ["id" => ":id"]) }}'.replace(':id', taskId),
method: 'POST',
data: { status: status },
success: function(response) {
},
error: function(error) {
}
});
});
$.ajax({
url: '{{ route("tasks.index") }}', // Use the correct route name for displaying tasks
method: 'GET',
success: function(response) {
console.log(response); // Log the response to the console
// Update the task list based on the response
$('#todo_list').html(response);
},
error: function(error) {
console.log(error); // Log the error to the console
// Rest of the code...
}
});
});
Routes
// Route to display tasks Route::get('/tasks', [ToDoListController::class, 'list_task'])->name('tasks.index');
HTML
<div class="col-xl-4 col-xxl-6 col-lg-6 col-md-12 col-sm-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Todo</h4>
</div>
<div class="card-body px-0">
<div class="todo-list">
<div class="tdl-holder">
<div class="tdl-content widget-todo2 mr-4">
<ul id="todo_list">
@foreach ($tasks as $task)
</ul>
</div>
<div class="px-4">
<input type="text" name="todo" class="tdl-new form-control" placeholder="Write new item and hit 'Enter'...">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
the error point to this section of the html
@foreach ($tasks as $task)
my view name is dashboard.blade.php
any help would be appricated
Level 122
@Danny971 you are totally missing the point
yes there are other parts of the application I return the dashboard view
In each of these other parts, you must also pass tasks
Please or to participate in this conversation.