Search code examples
phplaravelwidthlaravel-blade

How to get client DOM width in Laravel PHP


I don't know if this is even possible, but I'd appreciate any help.

  • How do I get the client's DOM width in Laravel?
  • Basically, is there a way to extract the client's screen width from the $request?
  • Or are there any functions in Laravel or Blade that will give me a screen width value?

I know that I can get the DOM width with jQuery $(document).width(), but I need the width value on server side; before any js is executed.


Solution

  • @ArturGrigio You can try set JavaScript cookie and access it in Laravel. When user visits your website, get the screen size with JS (window.width) and store it in a cookie screen=WxH In Laravel $screen = Cookie::get('screen');

    $screen = explode("x", $screen);
    $width = $screen[0]
    $height = $screen[0];
    

    This is 1 solution to do it.

    Unfortunately the is no way to get screen size info from PHP. You can try to play around with User Agent and detect if it is a mobile or desktop or tablet. and make fix sizes for those 3 types.

    A good User Agent package for Laravel