Laravel 7: Full Calendar not showing events - laravel

Please see my code first:
Controller:
public function getHolidays($days, $start_date) {
$date = date('Y-m-d',strtotime($start_date));
$i = 0;
$response = array();
while ($i <= 42) {
$tsDate = strtotime($date. ' ' .'+ '.$i.' days');
$day = date('D', $tsDate);
if(in_array($day, $days)) {
$data = array();
$data['title'] = 'Weekend';
$data['start'] = date('Y-m-d',$tsDate);
$data['end'] = date('Y-m-d',$tsDate);
$response[] = $data;
}
$i++;
}
return $response;
}
public function index(request $request){
if($request->ajax()) {
$weekends = DB::table('working_days')
->where('status', 0)
->get(['day'])
->map(function($item) {
return $item->day;
})->toArray();
$holidays = DB::table('holidays')
->get()
->map(function($item) {
return [
'title' => 'Holiday',
'start' => Carbon::parse($item->date)->format('Y-m-d'),
'end' => Carbon::parse($item->date)->format('Y-m-d'),
];
})->toArray();
$attendances = DB::table('attendances as a')
// ->where('leave_cat_id', '!=', null)
->where('user_id', Auth::user()->id)
->get()
->map(function($item) {
return [
'title' => 'Leave',
'start' => Carbon::parse($item->date)->format('Y-m-d'),
'end' => Carbon::parse($item->date)->format('Y-m-d'),
'color' => 'red',
];
})->toArray();
$data = [...$this->getHolidays($weekends, $request->start), ...$holidays, ...$attendances];
return response()->json($data);
}
return view('admin.dashboard');
}
Blade File script:
<script>
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var calendar = $('#full-calendar').fullCalendar({
editable: true,
editable: true,
events: '/dashboard',
eventRender: function (event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
},
});
});
</script>
Tables:
attendances
id
user_id
date
status
leave_cat_id
1
2
2022-07-13
1
null
2
2
2022-07-12
0
1
holidays
id
date
1
2022-07-13
2
2022-07-20
working_days
id
day
working_status
1
Fri
0
2
Sat
1
3
Sun
1
I want to show attendances on Full Calendar [https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.2/fullcalendar.min.js]
First, weekly holiday will show when the working_status = 0 and day is matched with calendar.
Second, holidays will show when it will match the dates with calendar date
Third, attendance will show on other days. If the status is 1 it will show present and if 0 it will show absent. Also, if the leave_cat_id column is not equal to null, then it will show On leave, others it will show the attendance status.
Can you please modify my code for this?
Thanks

public function getHolidays($days, $start_date) {
$date = date('Y-m-d',strtotime($start_date));
$i = 0;
$response = array();
while ($i <= 42) {
$tsDate = strtotime($date. ' ' .'+ '.$i.' days');
$day = date('D', $tsDate);
if(in_array($day, $days)) {
$data = array();
$data['title'] = 'Weekend';
$data['start'] = date('Y-m-d',$tsDate);
$data['end'] = date('Y-m-d',$tsDate);
$response[] = $data;
}
$i++;
}
return $response;
}
public function index(request $request){
if($request->ajax()) {
$weekends = DB::table('working_days')
->where('working_status''status', 0)
->get(['day'])
->map(function($item) {
return $item->day;
})->toArray();
$holiday$holidays = DB::table('holidays')
->get()
->map(function($item) {
return [
'title' => 'Holiday',
'start' => Carbon::parse($item->date)->format('Y-m-d'),
'end' => Carbon::parse($item->date)->format('Y-m-d'),
];
})->toArray();
$attendances = DB::table('attendances''attendances as a')
// ->where('leave_cat_id', '!=', null)
->where('user_id', Auth::user()->id)
->get()
->map(function($item) {
return [
'title' => $item->leave_cat_id ? 'Leave' : ($item->attendance_status == 1 ? 'Present' : 'Absent'),
'start' => Carbon::parse($item->attendance_date>date)->format('Y-m-d'),
'end' => Carbon::parse($item->attendance_date>date)->format('Y-m-d'),
'color' => $item->attendance_status == 1 ? 'green' : 'red',
];
})->toArray();
$data = [...$this->getHolidays($weekends, $request->start), ...$holiday$holidays, ...$attendances];
return response()->json($data);
}
return view('admin.dashboard');
}
<script type="text/javascript"><script>
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var calendar = $('#full-calendar').fullCalendar({
editable: true,
editable: true,
events: '/dashboard',
eventRender: function (event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
},
});
});
</script>

Related

Doing live search in codeigniter via ajax , mongodb

This is my ajax code which fetches all data from data and display in the table, but when I input an alphabet in search-box, it again fetches complete data inside the table
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "<?php echo base_url() ?>Appconfig/get_masteradmin_data?query="+query,true);
xhttp.onload= function()
{
if (xhttp.status >=200 && xhttp.status <400)
{
var data= JSON.parse(xhttp.responseText);
var html = '';
var i;
for(i=0; i<data.length; i++){
html +='<tr>'+
'<td>'+data[i].full_name+'</td>'+
'<td>'+data[i].username+'</td>'+
'<td>'+data[i].designation+'</td>'+
'<td>'+data[i].department+'</td>'+
'<td>'+data[i].official_mobile_no+'</td>'+
'<td>'+data[i].official_email_id+'</td>'+
'<td>'+data[i].select_user_type+'</td>'+
'<td>'+data[i].permission+'</td>'+
'</tr>';
}
showdata.insertAdjacentHTML('beforeend',html);
}
else
{
console.log("Try again after some time");
}
};
xhttp.send();
}
$('#search').keyup(function(){
var search = $(this).val();
//console.log(search);
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
This is my model for fetching data from my mongodb collection.
public function get_masteradmin_data($query)
{
$mongo = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
$query= '';
//$filter = ['full_name' => 'www'];
$regex = new MongoDB\BSON\Regex ($query);
$filter = ['full_name'=>$regex,];
$options =[
'projection' => [
'_id' => 0,
'full_name' => 1,
'username' => 1,
'designation'=> 1,
'department'=> 1,
'official_mobile_no'=> 1,
'official_email_id'=> 1,
'select_user_type'=> 1,
'permission'=> 1,
],
'sort' => [
'_id' => -1
],
];
$query = new MongoDB\Driver\Query($filter, $options);
//$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$result = $mongo->executeQuery('justrack_db.master_admin', $query);
$res = array();
foreach($result as $r)
{
$res[] = $r;
}
return json_encode($res,true);
//return $res;
}
This is my controller for displaying data. I am not sure, but I think there is some issue in my controller, as I tried to echo $query but it shows nothing. I am not able to understand how to fix this.
public function get_masteradmin_data()
{
$query = '';
$this->load->model('AppconfigModel');
$this->master_admin();
if($this->input->post('query'))
{
$query = $this->input->post('query');
}
$result= $this->AppconfigModel->get_masteradmin_data($query);
echo ($result);
}

Ajax with laravel Post returning

I am working with laravel and ajax. But when I register I see this error 302.
I know this may be a trivial question but I am just not able to get this ajax call to work.
Auth/RegisterController.php
protected function create(array $data, Request $request)
{
if ($request->hasFile('image')) {
$fileNameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extention;
$path = $request->file('image')->storeAs('public/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
return User::create([
'firstname' => $data['firstـname'],
'lastname' => $data['lastـname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'mobile' => $data['mobile'],
'nasional_code' => $data['national_code'],
'birthdate' => $data['birthـdate'],
'document' => $data['document'],
'educational' => $data['educational'],
'gender' => $data['gender'],
'side' => $data['side'],
$fileNameToStore => $data['image']
]);
}
My ajax is register.js file
How do I pass the "Accept'=>'application/json" in request when testing:
I want to add 'accept'=>'application/json' to my request header.
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
function scroll_to_class(element_class, removed_height) {
var scroll_to = $(element_class).offset().top - removed_height;
if($(window).scrollTop() != scroll_to) {
$('html, body').stop().animate({scrollTop: scroll_to}, 0);
}
}
function bar_progress(progress_line_object, direction) {
var number_of_steps = progress_line_object.data('number-of-steps');
var now_value = progress_line_object.data('now-value');
var new_value = 0;
if(direction == 'right') {
new_value = now_value + ( 100 / number_of_steps );
}
else if(direction == 'left') {
new_value = now_value - ( 100 / number_of_steps );
}
progress_line_object.attr('style', 'width: ' + new_value + '%;').data('now-value', new_value);
}
jQuery(document).ready(function() {
$('form fieldset:first').fadeIn('slow');
$('form input[type="text"], form input[type="password"], form textarea').on('focus', function() {
$(this).removeClass('input-error');
});
$('form .btn-next').on('click', function() {
var parent_fieldset = $(this).parents('fieldset');
var next_step = true;
var current_active_step = $(this).parents('form').find('.form-wizard.active');
var progress_line = $(this).parents('form').find('.progress-line');
parent_fieldset.find('input[type="text"], input[type="password"], input[type="email"], input[type="radio"]').each(function() {
if( $(this).val() == "" ) {
$(this).addClass('input-error');
next_step = false;
}
else {
$(this).removeClass('input-error');
}
});
parent_fieldset.find('input[type="checkbox"]').each(function() {
if( $(this).prop("checked") == false ) {
$('.form-check-label').css("color","red");
next_step = false;
}
else {
$('.form-check-label').css("color","black");
}
});
if( next_step ) {
parent_fieldset.fadeOut(400, function() {
current_active_step.removeClass('active').addClass('activated').next().addClass('active');
bar_progress(progress_line, 'right');
$(this).next().fadeIn();
scroll_to_class( $('form'), 20 );
});
}
});
// previous step
$('form .btn-previous').on('click', function() {
var current_active_step = $(this).parents('form').find('.form-wizard.active');
var progress_line = $(this).parents('form').find('.progress-line');
$(this).parents('fieldset').fadeOut(400, function() {
current_active_step.removeClass('active').prev().removeClass('activated').addClass('active');
bar_progress(progress_line, 'left');
$(this).prev().fadeIn();
scroll_to_class( $('form'), 20 );
});
});
$('form').on('submit', function(e) {
$(this).find('input[type="text"], input[type="password"], input[type="username"], input[type="email"], input[type="tel"], input[type="url"], textarea').each(function() {
if( $(this).val() == "" ) {
e.preventDefault();
$(this).addClass('input-error');
}
else {
$(this).removeClass('input-error');
}
});
});
});
Update your ajax header, according to this:
$.ajaxSetup({
headers: {
accepts: "application/json; charset=utf-8",
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
The possibilities of this issue is CSRF token and missing route.

How to change error 302 in laravel with ajax

I am working with laravel and ajax. But when I register I see this error 302.
I know this may be a trivial question but I am just not able to get this ajax call to work.
Auth/RegisterController.php
protected function create(array $data, Request $request)
{
if ($request->hasFile('image')) {
$fileNameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extention;
$path = $request->file('image')->storeAs('public/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
return User::create([
'firstname' => $data['firstـname'],
'lastname' => $data['lastـname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'mobile' => $data['mobile'],
'nasional_code' => $data['national_code'],
'birthdate' => $data['birthـdate'],
'document' => $data['document'],
'educational' => $data['educational'],
'gender' => $data['gender'],
'side' => $data['side'],
$fileNameToStore => $data['image']
]);
}
My ajax is register.js file
How do I pass the "Accept'=>'application/json" in request when testing:
I want to add 'accept'=>'application/json' to my request header.
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
function scroll_to_class(element_class, removed_height) {
var scroll_to = $(element_class).offset().top - removed_height;
if($(window).scrollTop() != scroll_to) {
$('html, body').stop().animate({scrollTop: scroll_to}, 0);
}
}
function bar_progress(progress_line_object, direction) {
var number_of_steps = progress_line_object.data('number-of-steps');
var now_value = progress_line_object.data('now-value');
var new_value = 0;
if(direction == 'right') {
new_value = now_value + ( 100 / number_of_steps );
}
else if(direction == 'left') {
new_value = now_value - ( 100 / number_of_steps );
}
progress_line_object.attr('style', 'width: ' + new_value + '%;').data('now-value', new_value);
}
jQuery(document).ready(function() {
$('form fieldset:first').fadeIn('slow');
$('form input[type="text"], form input[type="password"], form textarea').on('focus', function() {
$(this).removeClass('input-error');
});
$('form .btn-next').on('click', function() {
var parent_fieldset = $(this).parents('fieldset');
var next_step = true;
var current_active_step = $(this).parents('form').find('.form-wizard.active');
var progress_line = $(this).parents('form').find('.progress-line');
parent_fieldset.find('input[type="text"], input[type="password"], input[type="email"], input[type="radio"]').each(function() {
if( $(this).val() == "" ) {
$(this).addClass('input-error');
next_step = false;
}
else {
$(this).removeClass('input-error');
}
});
parent_fieldset.find('input[type="checkbox"]').each(function() {
if( $(this).prop("checked") == false ) {
$('.form-check-label').css("color","red");
next_step = false;
}
else {
$('.form-check-label').css("color","black");
}
});
if( next_step ) {
parent_fieldset.fadeOut(400, function() {
current_active_step.removeClass('active').addClass('activated').next().addClass('active');
bar_progress(progress_line, 'right');
$(this).next().fadeIn();
scroll_to_class( $('form'), 20 );
});
}
});
// previous step
$('form .btn-previous').on('click', function() {
var current_active_step = $(this).parents('form').find('.form-wizard.active');
var progress_line = $(this).parents('form').find('.progress-line');
$(this).parents('fieldset').fadeOut(400, function() {
current_active_step.removeClass('active').prev().removeClass('activated').addClass('active');
bar_progress(progress_line, 'left');
$(this).prev().fadeIn();
scroll_to_class( $('form'), 20 );
});
});
$('form').on('submit', function(e) {
$(this).find('input[type="text"], input[type="password"], input[type="username"], input[type="email"], input[type="tel"], input[type="url"], textarea').each(function() {
if( $(this).val() == "" ) {
e.preventDefault();
$(this).addClass('input-error');
}
else {
$(this).removeClass('input-error');
}
});
});
});

Dropdownlist selected value not showing while updating the form

Here i like to explain my problem
i have dropdownlist called companytype, it contains value 1+1, 1+2, 1+3, 1+4, 1+5, 1+6, 1+7
while creating form i have a select a value eg:1+4 and store, but the same while updating the value getting change as select companytype [prompt]
<?= $form->field($model, 'companytype')->dropDownList([ '1' => '1+1', '2' => '1+2', '3' => '1+3', '4' => '1+4', '5' => '1+5', '6' => '1+6', '7' => '1+7', ], ['prompt' => 'Select Company Type', ]) ?>
here i have added two images you can easily understand my question
gridview of created form
updating the same form
updated:
mycontroller code:
public function actionCreate()
{
if(Yii::$app->user->can( 'create-company' ) )
{
$model = new Company();
if ($model->load(Yii::$app->request->post()) ) {
$model->createdat = date('Y-m-d');
$ro = $model->relationoption;
if($ro == 'fixed')
{
$commaList = implode(', ', $model->relation);
$model->relation = $commaList;
}
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
else
{
throw new ForbiddenHttpException;
}
}
controller code for update
public function actionUpdate($id)
{
if(Yii::$app->user->can('update-company'))
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) )
{
$model->updatedat = date('Y-m-d h:m:s');
$ro = $model->relationoption;
if($ro == 'fixed')
{
$commaList = implode(', ', $model->relation);
$model->relation = $commaList;
}
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
else
{
throw new ForbiddenHttpException;
}
}
Is there anyone to answer, pls answer me
I dont see $model->companytype = 4 in your update action. Can you add it to your update action and check. So your update action should look like:
public function actionUpdate($id)
{
if(Yii::$app->user->can('update-company'))
{
$model = $this->findModel($id);
$model->companytype = 4;
if ($model->load(Yii::$app->request->post()) )
{
$model->updatedat = date('Y-m-d h:m:s');
$ro = $model->relationoption;
if($ro == 'fixed')
{
$commaList = implode(', ', $model->relation);
$model->relation = $commaList;
}
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
else
{
throw new ForbiddenHttpException;
}
}

Dynamic Select Dropdowns with CodeIgniter

I'm using CodeIgniter to develop a web application and wondered if anyone could help with a problem that has arose.
I have two dropdowns - Division and Teams. I'm creating a fixture so want to be able to allow the user to choose a division from the first dropdown, then within the second dropdown will only contain the teams from the selected division.
The Code I have so far:
Controller:
function create() {
$data['division_list'] = $this->add_fixture_model->get_divisions();
$data['team_list'] = $this->add_fixture_model->get_teams();
$data['referee_list'] = $this->add_fixture_model->get_referee();
// field name, error message, validation rules
$this->form_validation->set_rules('team_name', 'Team Name', 'trim|required');
$this->form_validation->set_rules('home_team', 'Home Team Name', 'trim|required');
$this->form_validation->set_rules('away_team', 'Away Team Name', 'required');
$this->form_validation->set_rules('division_name', 'Division', 'trim|required');
$this->form_validation->set_rules('referee', 'Referee', 'trim|required');
$this->form_validation->set_rules('fixture_week', 'Fixture Week', 'trim|required');
$this->form_validation->set_rules('fixture_day', 'Fixture Day', 'trim|required');
$this->form_validation->set_rules('fixture_month', 'Fixture Month', 'trim|required');
$this->form_validation->set_rules('fixture_year', 'Fixture Year', 'trim|required');
$this->form_validation->set_rules('confirm_day', 'Fixture Confirm Day', 'trim|required');
$this->form_validation->set_rules('confirm_month', 'Fixture Confirm Month', 'trim|required');
$this->form_validation->set_rules('confirm_year', 'Fixture Confirm Year', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/add_fixture', $data);
$this->load->view('includes/footer');
}
else
{
if($query = $this->add_fixture_model->add_fixture())
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/fixture_added');
$this->load->view('includes/footer');
}
else
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/fixture_added', $data);
$this->load->view('includes/footer');
}
}
}
Model:
class Add_fixture_model extends Model {
function add_fixture()
{
$add_new_fixture = array(
'team_name' => $this->input->post('team_name'),
'home_team' => $this->input->post('home_team'),
'away_team' => $this->input->post('away_team'),
'division' => $this->input->post('division_name'),
'ground' => $this->input->post('ground'),
'ground_postcode' => $this->input->post('ground_postcode'),
'referee' => $this->input->post('referee'),
'fixture_week' => $this->input->post('fixture_week'),
'fixture_day' => $this->input->post('fixture_day'),
'fixture_month' => $this->input->post('fixture_month'),
'fixture_year' => $this->input->post('fixture_year'),
'confirm_day' => $this->input->post('confirm_day'),
'confirm_month' => $this->input->post('confirm_month'),
'confirm_year' => $this->input->post('confirm_year')
);
$insert = $this->db->insert('fixtures', $add_new_fixture);
return $insert;
}
function get_divisions()
{
$this->db->from('divisions');
$this->db->order_by('division_name');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[''] = 'Select a division';
$return[$row['id']] = $row['division_name'];
}
}
return $return;
}
function get_teams()
{
$this->db->from('teams');
$this->db->order_by('team_name');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[''] = 'Select a team';
$return[$row['id']] = $row['team_name'];
}
}
return $return;
}
function list_teams() {
$this->db->from('teams');
$this->db->where('division_id', $this->input->post('id'));
$this->db->order_by('team_name');
$result = $this->db->get();
return $result;
}
function get_referee() {
$this->db->from('referees');
$this->db->order_by('id');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[''] = 'Select a referee';
$return[$row['id']] = $row['first_name'].' '.$row['surname'];
}
}
return $return;
}
}
View:
<?php echo form_open('add_fixture/create') ;?>
<label for="division" class="label">Division:</label><?php $js = 'id="division"'; echo form_dropdown('division', $division_list, set_value('division'), $js); ?>
JavaScript:
<script type="text/javascript">
$(document).ready(function() {
$('select#division').change(function() {
var div = $(this).val();
if(div != '') {
$.post("/add_fixture/list_teams/",{division_id: div}, function(data){
$("select#team_name").html(data);
});
}
});
});
</script>
Any help would very much be appreciated :)

Resources