0

Do you know why this code "<a href="{{route('posts.show', ['id' => 'post.id', 'slug' => post.slug])}}" class="btn btn-primary text-white">More</a>" in the code below shows Use of undefined constant post - assumed 'post'?

    $.each(result, function(index, post) {
            newPosts += '<div class="col-12 col-sm-6 col-lg-4 col-xl-3 mb-4">\n' +
'                        <div class="card box-shaddow">\n' +
+'                            <div class="card-body">\n' +
+'                                <h5 class="card-title h6 font-weight-bold text-heading-blue">'+post.name+'</h5>\n' +
+'                            </div>\n' +
'                            <div class="card-footer d-flex justify-content-between align-items-center">\n' +
'                                 <a href="{{route('posts.show', ['id' => 'post.id', 'slug' => post.slug])}}" class="btn btn-primary text-white">More</a>

'       <span class="font-weight-bold font-size-sm text-heading-blue"> </span>\n' +
'                            </div>\n' +
'                        </div>\n' +
'                    </div>';
        });
johnW
  • 309
  • 1
  • 8
  • 26
  • 1
    Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Patrick Q Apr 04 '18 at 20:54
  • What happens when you `{{ dd(route('posts.show', ['id' => 'post.id', 'slug' => post.slug])) }}` – senty Apr 04 '18 at 20:54
  • Wait a minute. You've marked this as PHP, but that looks like Javascript/jQuery. – Patrick Q Apr 04 '18 at 20:57
  • @PatrickQ He is probably doing `$('body').append(newPosts)` kind of thing – senty Apr 04 '18 at 21:10
  • 2
    You're attempting to use JavaScript in a PHP block. The PHP block is rendered before the JavaScript ever runs, so it's not possible to do it the way you have it now. You can try putting dummy values in the id and slug variables and doing a replace on the string in the JavaScript before you use the newPosts variable. – Shane Apr 04 '18 at 21:13
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – rickdenhaan Apr 04 '18 at 21:32

1 Answers1

1

You cannot use your Javascript post object as a variable in a call to Blade's route() function. Blade will render the template on the server before sending it to the browser, whereas your Javascript code gets executed by your visitor on their computer much later.

One option is to let Blade fill a variable that you can use in Javascript, using dummy values:

var placeholder = "{{route('posts.show', ['id' => 123, 'slug' => 'demo-slug'])}}";

$.each(result, function(index, post) {
    var url = placeholder.replace(123, post.id).replace('demo-slug', post.slug);

    newPosts += '[...]'
             +  '<a href="' + url + '" class="btn btn-primary text-white">More</a>'
             +  '[...]';
});

Which values you use as placeholders depends on what your route looks like and which validations are used (if any). Just make sure you use a placeholder text that is not already part of your URI.

rickdenhaan
  • 10,857
  • 28
  • 37