1

I have a Laravel 5.5 project In the app service providers I have shared some variables like this:

public function boot()
{
    View::share('path', 'laravel/public');
}

I can access it in every view blade very easily.

But I have ajax search and it comes from a separated JS file included in the master layout.

<script src="{{ asset('style//js/searchajax.js')}}"></script>

This is the searchajax.js code

var path = '/laravel/public/';
$("#userSearchAjax").bind('change keyup',function(){
var userSearchAjax = $(this).val().trim();
if(userSearchAjax != '')
{
    $.ajax({
        type:'GET',
        url: '/laravel/public/userssearchajax',
        data: {userSearchAjax:userSearchAjax},
        success:function(data){
            var result = "<tr class='info'><td>اسم المستخدم</td><td>الصورة</td></td><td>البريد الالكتروني</td></td><td>رقم الهاتف</td></td><td>الفرع</td></td><td>اعدادات</td></tr>";
            for(i = 0;i<data.length;i++)
            {
                result+= "<tr class='warning'><td>"+data[i]['user_name']+"</td>";
                result+= "<td>";
                    result += "<a data-title='"+data[i]['user_name']+"'  data-lightbox='image-1' target='_blank' href='"+path+"uploads/users/"+data[i]['id']+"/"+data[i]['id']+".jpg'><img alt='"+data[i]['user_name']+"' title='"+data[i]['user_name']+"' src='"+path+"/uploads/users/"+data[i]['id']+"/"+data[i]['id']+".jpg' class='thumb' /><br /></a>";
                result+= "</td>";
                result+= "<td>"+data[i]['user_email']+"</td>";
                result+= "<td>"+data[i]['user_phone']+"</td>";
                result+= "<td>"+data[i]['user_department']+"</td>";
                result+= "<td><a class='btn btn-success'>عرض</a>";
                result+= "<a class='btn btn-info'>تعديل</a></td></tr>";
            }
            result += "";
            $("#search-result").html(result);
        }

    });
}
else
{
    $("#search-result").html("");
}
})

My problem is that I can't access the shared variable $path inside js file even shared model like Auth and other default models in Laravel I cant access them inside the js file.

Thanks

Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35
Awar Pulldozer
  • 1,071
  • 6
  • 23
  • 40
  • 1
    Laravel is a server side code and js is a client side code. You can not share server side codes written in PHP syntax with JS code on client ... they are two completely separate entities. You can inject the code in your layout file JS script using PHP from server side or using Blade template language if you want. – Andy Sep 26 '17 at 22:29

2 Answers2

1
<script>    
    var path= {!! $path !!};
</script>

I would also recommend changing the variable name to "public_path". There are other paths you could need.

This question was also similar to: laravel-5 passing variable to JavaScript

Jon Ohliger
  • 68
  • 1
  • 8
1

You can set the path in your JS code by injecting the code using Blade like this {{$path}} or {{session('path')}} ... provided you have set the session('path') somewhere at right time and right place.

Andy
  • 2,493
  • 6
  • 37
  • 63