1

Hi it is possible pass variable from controller to JS script. I must do change color event when user join to event.

I need pass variable $colorrr to my js becouse i use fullcalendar there is option eventColor: and i want send there variable $colorrr from controller. Now only i do checking if event exist in pivot table. And its working, my problem is i dont know how pass variable to option eventColor

Controller:

public function index()
    { 
       $eventscolo=DB::table('save_events')->select('events_id')->get();
       $eventsss=DB::table('events')->select('id','title')->get(); 
             
        foreach ($eventsss as $eventss) 
        {
             
            if(DB::table('save_events')->where('events_id','=',$eventss->id)->exists())
            {
                    
                  $colorrr = 'grey';
                    
            }
            else
                { 
                    if ($eventss->title=="Wydzial 1") 
                        {
                            
                           $colorrr = 'red';
                            
                        }
                            elseif($eventss->title == "Wydzial 2")
                            {
                               
                             $colorrr = 'blue';
                               
                            }
                            elseif ($eventss->title == "Wydzial 3") 
                            {
                               
                             $colorrr = 'green';
                               
                            }
                            elseif ($eventss->title == "Wydzial 4") 
                            {
                                
                               $colorrr = 'yellow';
                                
                            }
                } 

        }
      return view('home',['eventscolo'=>$eventscolo,'eventsss'=>$eventsss,'colorrr'=>$colorrr]);

JS script:

<script src="{{ asset('js/fullcalendar') }}/fullcalendar.js"></script>
<script src="{{ asset('js/bootstrap.js') }}"></script>
<script type="text/javascript">

  $(document).ready(function() {
    
    var base_url = '{{ url('/') }}';
    
      
    
    $('#bootstrapModalFullCalendar').fullCalendar({
      weekends: true,
      header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek'
      },
       eventClick: function(event, jsEvent, view) {
          $('#modalTitle').html(event.title);
          $('#modalBody').html(event.name);
          $('#eventUrl').attr('href','/home/zapis/'+event.id);
          $("#startTime").html(moment(event.start).format('HH:mm '));
          if (event.end) $("#endTime").html(moment(event.end).format('HH:mm '));
          else $("#endTime").html('');              
          $('#fullCalModal').modal();
          return false;
        },
      
      eventLimit: true, // allow "more" link when too many events
      FirstDay: 1,
              contentheight : 650,
              editable : true,
              allDay : false,
              aspectRatio : 2,
              slotLabelFormat : 'HH:mm:ss',
              timeFormat : 'HH:mm',
              displayEventEnd : true,
              events: {
        url: base_url + '/api',
        error: function() {
          alert("cannot load json");
        }
      },
       eventColor: 'colorrr',
    
      
    });
  });
</script>

3
  • yes you can do that, pass your variable to view and on your view create a js variable and put that value there Commented Apr 6, 2017 at 16:08
  • But how i trying do eventColor: {{$colorrr}} and its not working Commented Apr 6, 2017 at 16:10
  • and i trying that var colorrr = {{$colorrr}} then eventColor: colorrr, and still dont work Commented Apr 6, 2017 at 16:12

1 Answer 1

2

Controller (Pass value like following to your view)

//..........................
//............................
$data['myVal'] = "abc";
return view('myViewPage',['data'=>$data]);

Blade-myViewPage(JS)

<script>
    //Global variable on you blade
    var url = "{{url('/manufacturers-json')}}";
    var custom_val = "{{$data['myVal']}}"; //What ever you are getting from controller ($colorr)
</script>

and then you can use this JS variable into your JS. I am doing this way using angularJS. If in your case the above will not work then try creating an input field.

<input type="hidden" value="<?php echo $colorrr; ?>" id="my_color" />

Where $colorrr (or what ever you are passing from your controller to the view, make sure this variable contains value.
and in your JS.

var color = $("#my_color").val();
Sign up to request clarification or add additional context in comments.

6 Comments

i made like u write and change all event to red and its not good
Okay, Can you just print your varibale on your blade (view). If yes then create an input field with type hidden and put value as {{$colorrr}} (whatever your variable is) and then get that value using ID of the input Filed using JQUERY
i made it and its all red when i go to view-source in chrome i saw somethink like that <input type="hidden" value="red" id="my_color" />
i made that: <input type="hidden" value="{{ $colorrr }}" id="my_color" /> next i copy var color = $("#my_color").val(); to my script then i write eventColor: color, and all event is red
thx i trying do this 2 days;p and its reallly anoying just today i figure how check if event exist in table but i cant pass color each records becouse i must checking if event exist so i dont know how i fix it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.