1

I'm building Notification using Pusher, and I receive 'route' to some page as a JavaScript variable

Now I want to inject this variable to the Notification bar as a link to some page determined in the variable, I try to make href and put the the route in it using laravel url method to access it from any page. here's the code

channel.bind('App\\Events\\Councilcreated', function (data) {
    var a = document.createElement('a');
    a.setAttribute("href", "{{ url(".data.page.") }}");
});

the Problem is that printed (data.page) as string not as the value of the (data.page) variable

3
  • Please can you show where you're setting page in your event class? Commented May 20, 2019 at 5:55
  • ` public $councilname; public $id; public $title; public $d; public $message; public $page; public $icon; public function __construct($councilname,$id,$title,$message,$page,$icon) { $d=0; $this->councilname = $councilname; $this->id = $id; $this->title = $title; $this->message = $message; $this->page = $page; $this->icon = $icon; }` Commented May 20, 2019 at 5:58
  • it printed successfully in console Commented May 20, 2019 at 6:00

3 Answers 3

3

I suggest you to first store your URL into a javascript variable then add your data into it and then set it to you href. something like below.

var url = "{{ url() }}";
a.setAttribute("href", url+'/'+data.page);

If you getting htmlspecialchars () error like you said and then use it like below

    var url = "{!! url() !!}";
a.setAttribute("href", url+'/'+data.page);

Why you not passing full url from php side and directly use it here , something like below.

//php
$this->page = url($page);

//javascipt
a.setAttribute("href", data.page);
Sign up to request clarification or add additional context in comments.

3 Comments

the frst sugget return laravel exception htmlspecialchars() expects parameter 1 to be string, object given
I save the page in notification table, Is that correct to save whole url ?
don't save it in table just give retrun form your event whole url like i suggest $this->page = url($page);
0

This won't work as you're mixing javascript with php i.e. when this function executes you won't have access to the url function as it will be executing in the browser.

I would suggest making page a fully qualified url in your event class i.e. using the url() function in there:

public function __construct($councilname, $id, $title, $message, $page, $icon)
{
    $d = 0;
    $this->councilname = $councilname;
    $this->id = $id;
    $this->title = $title;
    $this->message = $message;
    $this->page = url($page); // <-- This line
    $this->icon = $icon;
}

Then in your JS event listener you would just need:

channel.bind('App\\Events\\Councilcreated', function (data) {
    var a = document.createElement('a');
    a.setAttribute("href", data.page);
});

2 Comments

I save the page in notification table in DB, Is that correct to save whole url ?
@SamehMohamedOmar Yeah, there's nothing wrong with saving the whole URL for the notifications.
0

In your footer declare a variable like this

const url = "{{ url() }}";

In case any special character is being returned, use this:

const url = "{!! url() !!}";

and then keep using this variable at all the required places like this:

a.setAttribute("href", url+'/'+data.page);

2 Comments

return laravel exception htmlspecialchars() expects parameter 1 to be string, object given
Must be some special character which your url() method is returning. I've updated the answer.

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.