@if at Script TAG laravel
How to make this work, If statement not working at all
<script>
@if(Session::has('messege'))
var type="{{Session::get('alert-type','info')}}"
switch(type){
case 'info':
toastr.info("{{ Session::get('messege') }}");
break;
case 'success':
toastr.success("{{ Session::get('messege') }}");
break;
case 'warning':
toastr.warning("{{ Session::get('messege') }}");
break;
case 'error':
toastr.error("{{ Session::get('messege') }}");
break;
}
@endif
</script>
hope someone help me :)
laravel
|
show 12 more comments
How to make this work, If statement not working at all
<script>
@if(Session::has('messege'))
var type="{{Session::get('alert-type','info')}}"
switch(type){
case 'info':
toastr.info("{{ Session::get('messege') }}");
break;
case 'success':
toastr.success("{{ Session::get('messege') }}");
break;
case 'warning':
toastr.warning("{{ Session::get('messege') }}");
break;
case 'error':
toastr.error("{{ Session::get('messege') }}");
break;
}
@endif
</script>
hope someone help me :)
laravel
Please provide more relevant information, for example where do you set the session, as well as any other relevant code
– Sven Hakvoort
Nov 15 '18 at 14:55
the Session is work, if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 14:57
it only show code if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 15:05
You don't set the session with Laravel.
– Universal Link
Nov 15 '18 at 15:13
1
You're trying to do a blade@ifinside a script tag. That wont work.
– John Halsey
Nov 15 '18 at 15:15
|
show 12 more comments
How to make this work, If statement not working at all
<script>
@if(Session::has('messege'))
var type="{{Session::get('alert-type','info')}}"
switch(type){
case 'info':
toastr.info("{{ Session::get('messege') }}");
break;
case 'success':
toastr.success("{{ Session::get('messege') }}");
break;
case 'warning':
toastr.warning("{{ Session::get('messege') }}");
break;
case 'error':
toastr.error("{{ Session::get('messege') }}");
break;
}
@endif
</script>
hope someone help me :)
laravel
How to make this work, If statement not working at all
<script>
@if(Session::has('messege'))
var type="{{Session::get('alert-type','info')}}"
switch(type){
case 'info':
toastr.info("{{ Session::get('messege') }}");
break;
case 'success':
toastr.success("{{ Session::get('messege') }}");
break;
case 'warning':
toastr.warning("{{ Session::get('messege') }}");
break;
case 'error':
toastr.error("{{ Session::get('messege') }}");
break;
}
@endif
</script>
hope someone help me :)
laravel
laravel
asked Nov 15 '18 at 14:50
riski Ferdiriski Ferdi
32
32
Please provide more relevant information, for example where do you set the session, as well as any other relevant code
– Sven Hakvoort
Nov 15 '18 at 14:55
the Session is work, if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 14:57
it only show code if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 15:05
You don't set the session with Laravel.
– Universal Link
Nov 15 '18 at 15:13
1
You're trying to do a blade@ifinside a script tag. That wont work.
– John Halsey
Nov 15 '18 at 15:15
|
show 12 more comments
Please provide more relevant information, for example where do you set the session, as well as any other relevant code
– Sven Hakvoort
Nov 15 '18 at 14:55
the Session is work, if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 14:57
it only show code if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 15:05
You don't set the session with Laravel.
– Universal Link
Nov 15 '18 at 15:13
1
You're trying to do a blade@ifinside a script tag. That wont work.
– John Halsey
Nov 15 '18 at 15:15
Please provide more relevant information, for example where do you set the session, as well as any other relevant code
– Sven Hakvoort
Nov 15 '18 at 14:55
Please provide more relevant information, for example where do you set the session, as well as any other relevant code
– Sven Hakvoort
Nov 15 '18 at 14:55
the Session is work, if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 14:57
the Session is work, if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 14:57
it only show code if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 15:05
it only show code if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 15:05
You don't set the session with Laravel.
– Universal Link
Nov 15 '18 at 15:13
You don't set the session with Laravel.
– Universal Link
Nov 15 '18 at 15:13
1
1
You're trying to do a blade
@if inside a script tag. That wont work.– John Halsey
Nov 15 '18 at 15:15
You're trying to do a blade
@if inside a script tag. That wont work.– John Halsey
Nov 15 '18 at 15:15
|
show 12 more comments
2 Answers
2
active
oldest
votes
Right, I see a few issues with the code you have provided.
So, you don't need to wrap the @if in a <script> tag. Laravel uses blade to parse it and execute it automatically.
Also you've spelt message wrong, this might be the reason for why the statement isn't executing at all. Double checking your syntax is a must when the code isn't working the way it's intended.
You can also set the variable in PHP, since you haven't provided any information to where the code is stored or what it's meant to show, don't use JavaScript when you can just use PHP. Set the variable type using the $ symbol in PHP. Also by removing the <script> tags, you'll probably get a Laravel exception with more information as to why your code isn't working.
Try that, and if you're still having issues, post the Laravel error and I'll try to assist you again.
If you're new to Laravel, PHP or HTML, check our Laracasts.com
– Universal Link
Nov 15 '18 at 15:17
how i can make the switch work?
– riski Ferdi
Nov 15 '18 at 15:29
if there is no script tag?
– riski Ferdi
Nov 15 '18 at 15:29
He's trying to usetoastr, which is a JS library, based on the existence of a session value from PHP. Removing the<script>makes no sense.
– Tim Lewis
Nov 15 '18 at 15:34
yes i want to use it for notifications
– riski Ferdi
Nov 15 '18 at 15:36
|
show 2 more comments
Let's simplify this a little bit. Don't rely on PHP to execute JS code beyond what you need to rely on, and by that I mean convert Session::get("messege") into a JS variable and use that in your JS code:
<script type="text/javascript">
let message = "{{ Session::get('messege') }}";
let type = "{{ Session::get('alert-type', 'info') }}";
console.log(message, type);
if(message){
switch (type) {
case "info":
toastr.info(message);
break;
case "success":
toastr.success(message);
break;
case "warning":
toastr.warning(message);
break;
case "error":
toastr.error(message);
break;
}
}
</script>
As long as this <script> is in a .blade.php file, it'll translate Session::get() variables into JS, which you can then use in your JS code without relying on blade @ syntax. Also, added in a console.log() statement so you can see what your variables contain, to confirm whether or not your code is executing. Also, open you Developer Tools (F12 in browsers) and view your Console tab; should tell you if anything is going wrong.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322074%2fif-at-script-tag-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Right, I see a few issues with the code you have provided.
So, you don't need to wrap the @if in a <script> tag. Laravel uses blade to parse it and execute it automatically.
Also you've spelt message wrong, this might be the reason for why the statement isn't executing at all. Double checking your syntax is a must when the code isn't working the way it's intended.
You can also set the variable in PHP, since you haven't provided any information to where the code is stored or what it's meant to show, don't use JavaScript when you can just use PHP. Set the variable type using the $ symbol in PHP. Also by removing the <script> tags, you'll probably get a Laravel exception with more information as to why your code isn't working.
Try that, and if you're still having issues, post the Laravel error and I'll try to assist you again.
If you're new to Laravel, PHP or HTML, check our Laracasts.com
– Universal Link
Nov 15 '18 at 15:17
how i can make the switch work?
– riski Ferdi
Nov 15 '18 at 15:29
if there is no script tag?
– riski Ferdi
Nov 15 '18 at 15:29
He's trying to usetoastr, which is a JS library, based on the existence of a session value from PHP. Removing the<script>makes no sense.
– Tim Lewis
Nov 15 '18 at 15:34
yes i want to use it for notifications
– riski Ferdi
Nov 15 '18 at 15:36
|
show 2 more comments
Right, I see a few issues with the code you have provided.
So, you don't need to wrap the @if in a <script> tag. Laravel uses blade to parse it and execute it automatically.
Also you've spelt message wrong, this might be the reason for why the statement isn't executing at all. Double checking your syntax is a must when the code isn't working the way it's intended.
You can also set the variable in PHP, since you haven't provided any information to where the code is stored or what it's meant to show, don't use JavaScript when you can just use PHP. Set the variable type using the $ symbol in PHP. Also by removing the <script> tags, you'll probably get a Laravel exception with more information as to why your code isn't working.
Try that, and if you're still having issues, post the Laravel error and I'll try to assist you again.
If you're new to Laravel, PHP or HTML, check our Laracasts.com
– Universal Link
Nov 15 '18 at 15:17
how i can make the switch work?
– riski Ferdi
Nov 15 '18 at 15:29
if there is no script tag?
– riski Ferdi
Nov 15 '18 at 15:29
He's trying to usetoastr, which is a JS library, based on the existence of a session value from PHP. Removing the<script>makes no sense.
– Tim Lewis
Nov 15 '18 at 15:34
yes i want to use it for notifications
– riski Ferdi
Nov 15 '18 at 15:36
|
show 2 more comments
Right, I see a few issues with the code you have provided.
So, you don't need to wrap the @if in a <script> tag. Laravel uses blade to parse it and execute it automatically.
Also you've spelt message wrong, this might be the reason for why the statement isn't executing at all. Double checking your syntax is a must when the code isn't working the way it's intended.
You can also set the variable in PHP, since you haven't provided any information to where the code is stored or what it's meant to show, don't use JavaScript when you can just use PHP. Set the variable type using the $ symbol in PHP. Also by removing the <script> tags, you'll probably get a Laravel exception with more information as to why your code isn't working.
Try that, and if you're still having issues, post the Laravel error and I'll try to assist you again.
Right, I see a few issues with the code you have provided.
So, you don't need to wrap the @if in a <script> tag. Laravel uses blade to parse it and execute it automatically.
Also you've spelt message wrong, this might be the reason for why the statement isn't executing at all. Double checking your syntax is a must when the code isn't working the way it's intended.
You can also set the variable in PHP, since you haven't provided any information to where the code is stored or what it's meant to show, don't use JavaScript when you can just use PHP. Set the variable type using the $ symbol in PHP. Also by removing the <script> tags, you'll probably get a Laravel exception with more information as to why your code isn't working.
Try that, and if you're still having issues, post the Laravel error and I'll try to assist you again.
answered Nov 15 '18 at 15:17
Universal LinkUniversal Link
13913
13913
If you're new to Laravel, PHP or HTML, check our Laracasts.com
– Universal Link
Nov 15 '18 at 15:17
how i can make the switch work?
– riski Ferdi
Nov 15 '18 at 15:29
if there is no script tag?
– riski Ferdi
Nov 15 '18 at 15:29
He's trying to usetoastr, which is a JS library, based on the existence of a session value from PHP. Removing the<script>makes no sense.
– Tim Lewis
Nov 15 '18 at 15:34
yes i want to use it for notifications
– riski Ferdi
Nov 15 '18 at 15:36
|
show 2 more comments
If you're new to Laravel, PHP or HTML, check our Laracasts.com
– Universal Link
Nov 15 '18 at 15:17
how i can make the switch work?
– riski Ferdi
Nov 15 '18 at 15:29
if there is no script tag?
– riski Ferdi
Nov 15 '18 at 15:29
He's trying to usetoastr, which is a JS library, based on the existence of a session value from PHP. Removing the<script>makes no sense.
– Tim Lewis
Nov 15 '18 at 15:34
yes i want to use it for notifications
– riski Ferdi
Nov 15 '18 at 15:36
If you're new to Laravel, PHP or HTML, check our Laracasts.com
– Universal Link
Nov 15 '18 at 15:17
If you're new to Laravel, PHP or HTML, check our Laracasts.com
– Universal Link
Nov 15 '18 at 15:17
how i can make the switch work?
– riski Ferdi
Nov 15 '18 at 15:29
how i can make the switch work?
– riski Ferdi
Nov 15 '18 at 15:29
if there is no script tag?
– riski Ferdi
Nov 15 '18 at 15:29
if there is no script tag?
– riski Ferdi
Nov 15 '18 at 15:29
He's trying to use
toastr, which is a JS library, based on the existence of a session value from PHP. Removing the <script> makes no sense.– Tim Lewis
Nov 15 '18 at 15:34
He's trying to use
toastr, which is a JS library, based on the existence of a session value from PHP. Removing the <script> makes no sense.– Tim Lewis
Nov 15 '18 at 15:34
yes i want to use it for notifications
– riski Ferdi
Nov 15 '18 at 15:36
yes i want to use it for notifications
– riski Ferdi
Nov 15 '18 at 15:36
|
show 2 more comments
Let's simplify this a little bit. Don't rely on PHP to execute JS code beyond what you need to rely on, and by that I mean convert Session::get("messege") into a JS variable and use that in your JS code:
<script type="text/javascript">
let message = "{{ Session::get('messege') }}";
let type = "{{ Session::get('alert-type', 'info') }}";
console.log(message, type);
if(message){
switch (type) {
case "info":
toastr.info(message);
break;
case "success":
toastr.success(message);
break;
case "warning":
toastr.warning(message);
break;
case "error":
toastr.error(message);
break;
}
}
</script>
As long as this <script> is in a .blade.php file, it'll translate Session::get() variables into JS, which you can then use in your JS code without relying on blade @ syntax. Also, added in a console.log() statement so you can see what your variables contain, to confirm whether or not your code is executing. Also, open you Developer Tools (F12 in browsers) and view your Console tab; should tell you if anything is going wrong.
add a comment |
Let's simplify this a little bit. Don't rely on PHP to execute JS code beyond what you need to rely on, and by that I mean convert Session::get("messege") into a JS variable and use that in your JS code:
<script type="text/javascript">
let message = "{{ Session::get('messege') }}";
let type = "{{ Session::get('alert-type', 'info') }}";
console.log(message, type);
if(message){
switch (type) {
case "info":
toastr.info(message);
break;
case "success":
toastr.success(message);
break;
case "warning":
toastr.warning(message);
break;
case "error":
toastr.error(message);
break;
}
}
</script>
As long as this <script> is in a .blade.php file, it'll translate Session::get() variables into JS, which you can then use in your JS code without relying on blade @ syntax. Also, added in a console.log() statement so you can see what your variables contain, to confirm whether or not your code is executing. Also, open you Developer Tools (F12 in browsers) and view your Console tab; should tell you if anything is going wrong.
add a comment |
Let's simplify this a little bit. Don't rely on PHP to execute JS code beyond what you need to rely on, and by that I mean convert Session::get("messege") into a JS variable and use that in your JS code:
<script type="text/javascript">
let message = "{{ Session::get('messege') }}";
let type = "{{ Session::get('alert-type', 'info') }}";
console.log(message, type);
if(message){
switch (type) {
case "info":
toastr.info(message);
break;
case "success":
toastr.success(message);
break;
case "warning":
toastr.warning(message);
break;
case "error":
toastr.error(message);
break;
}
}
</script>
As long as this <script> is in a .blade.php file, it'll translate Session::get() variables into JS, which you can then use in your JS code without relying on blade @ syntax. Also, added in a console.log() statement so you can see what your variables contain, to confirm whether or not your code is executing. Also, open you Developer Tools (F12 in browsers) and view your Console tab; should tell you if anything is going wrong.
Let's simplify this a little bit. Don't rely on PHP to execute JS code beyond what you need to rely on, and by that I mean convert Session::get("messege") into a JS variable and use that in your JS code:
<script type="text/javascript">
let message = "{{ Session::get('messege') }}";
let type = "{{ Session::get('alert-type', 'info') }}";
console.log(message, type);
if(message){
switch (type) {
case "info":
toastr.info(message);
break;
case "success":
toastr.success(message);
break;
case "warning":
toastr.warning(message);
break;
case "error":
toastr.error(message);
break;
}
}
</script>
As long as this <script> is in a .blade.php file, it'll translate Session::get() variables into JS, which you can then use in your JS code without relying on blade @ syntax. Also, added in a console.log() statement so you can see what your variables contain, to confirm whether or not your code is executing. Also, open you Developer Tools (F12 in browsers) and view your Console tab; should tell you if anything is going wrong.
answered Nov 15 '18 at 16:00
Tim LewisTim Lewis
11k63664
11k63664
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322074%2fif-at-script-tag-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please provide more relevant information, for example where do you set the session, as well as any other relevant code
– Sven Hakvoort
Nov 15 '18 at 14:55
the Session is work, if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 14:57
it only show code if there is no Script Tag
– riski Ferdi
Nov 15 '18 at 15:05
You don't set the session with Laravel.
– Universal Link
Nov 15 '18 at 15:13
1
You're trying to do a blade
@ifinside a script tag. That wont work.– John Halsey
Nov 15 '18 at 15:15