When i wrote this in command line composer require brian2694/laravel-toastr. I've got message like [InvalidArgumentException]
Could not find package brian2694/laravel-toastr. It was however found via repository search, which indicates a consistency issue with the repository.
Any tips to solve this problem ?
First thing, you need to include the Toastr library to Laravel blade views. You can achieve this by below methods.
1.Include css and js file from a CDN
2.Install the library using NPM
3.Download the css and js file from github Toastr Repository
However you choose the grab the library, you have to add the location to the 2 files in the head tag of your master layout file like so:
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css">
After including the library, whenever you need to redirect to a page most probably from a controller, you can pass the notification information like so:
$notification = array(
'message' => 'Post created successfully!',
'alert-type' => 'success'
);
return Redirect::to('/')->with($notification);
After adding redirect notification to your controller, add following javascript at the bottom of your layout file (app.blade.php or master.blade.php – depends on how you name it).
#if(Session::has('message'))
var type = "{{ Session::get('alert-type', 'info') }}";
switch(type){
case 'info':
toastr.info("{{ Session::get('message') }}");
break;
case 'warning':
toastr.warning("{{ Session::get('message') }}");
break;
case 'success':
toastr.success("{{ Session::get('message') }}");
break;
case 'error':
toastr.error("{{ Session::get('message') }}");
break;
}
#endif
Note* Please open and close script tag before and after the above code snippet respectively.
That’s it. Now if you visit or perform the action you have targeted, you will be shown a nice notification using toastr. You can set the alert type from info, warning, success or error and correct colored notification will be shown.
Related
In my code, I am returning a zip file as a streamed response:
return response()->stream(function() use ($zip){
$zip->finish();
});
I would like to also return a status message saying "Your zip download has started" along with the response, but I can't find a way of doing it properly in Laravel.
I am using Laravel 5.2
Try using the Session facade:
//don't forget to "use Session;" at the top
return response()->stream(function() use ($zip){
$zip->finish();
Session::flash('message', 'Download successful');
});
In your view do something like:
#if (Session::has('message'))
<li>{!! session('message') !!}</li>
#endif
Link to the Docs
try this
return response()->stream(function() use ($zip){
$zip->finish()->with('message','Your zip download has started');
});
view file add this
#if(Session::has('message'))
<div class="alert alert-success">{{Session::get('message')}}</div>
#endif
Is there a way to check in a blade view if an image is really there or not?
I need to show results from a search box.
The results are many boxes with infos and a picture for each box.
The point is in my DB I store links to images that are on remote servers and also name of images that are stored locally.
So what I am doing is check if the file exists locally and if so use it and if not look on the remote server (if the picture data is not NULL it's either there or in a remote server).
I was trying to check if file exists using curl and it works but for big collections it takes too much time to finally spit the data to the view (every link has to be checked).
So what I want to do, if possible, is check directly in the blade view if the picture is not broken (404) and if so replace with an "image-not-found.png" I store locally. How can I do that?
I usually handle this with JavaScript using the img tag's onerror event. Typically I add a few more bells and whistles to the solution but this is it in a nutshell.
Plan JavaScript
function loadNextImage(id,uri){
document.getElementById(id).src = uri;
}
Plain HTML
<img src="http://local/image.jpg"
onerror="loadNextImage('image1', 'http://remote/imae.jpg'));"
id='image1' />
VueJS and Webpack
<template>
<img :src="local_url" #error="imgOnError()" ref="image"/>
</template>
<script>
export default{
name: 'Thumbnail',
props: {
local_url: String,
remote_url: String
},
methods: {
imgOnError: function (e) {
this.$refs.image.src = this.remote_url
}
}
}
</script>
You can use the func "file_get_contents" inside a try-catch block. I know i not the best way, but i could work for you.
Tray this (no the best way):
<?php
try{
$img = 'myproject.dev/image.jpg';
$test_img = file_get_contents($img);
echo "<img src='{$img}'>";
}catch(Exception $e){
echo "<img src='img/no-img.jpg'>";
}
?>
I have problem because im using angular with laravel.I added this in routes.php
Blade::setContentTags('<%', '%>'); // for variables and all things Blade
Blade::setEscapedContentTags('<%%', '%%>'); // for escaped data
But its not working because im getting an error when i display data from angular.
Any suggestion how can i fix this?
For example if i say:
{{'test'}} it works but if i say {{response.test}} where response is from angular i get an error because laravel thinks that is his.
Blade will ignore anything preceded by the # character. Try that.
first confing laravel blade and angular js
<script type="text/javascript">
var app = angular.module('myApp', [])
.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('<%=');
$interpolateProvider.endSymbol('%>');
});
</script>
write your angular code like this
<%= cs.Category_name %>
I have the following code inside my app/start/global.php file:
App::missing(function($exception){
return Response::view('missing', array('url' => Request::url()), 404);
});
And I have a file called missing.blade.php at app/view. However, the above code gives me a FatalErrorException, telling me "Call to a member function getPath() on a non-object". The formatting of the otherwise pretty error page that I got the above information from is also messed up.
But everything works fine when I change my code in global.php to the following:
App::missing(function($exception){
return Response::make("Page not found", 404);
});
I don't get what's going on. How can I display a specific view when a route is not found?
EDIT:
Here is my missing.blade.php file:
#extends('master')
#section('header')
<h2>404 Error</h2>
#stop
#section('content')
<p>
Unable to locate page.
</p>
#stop
EDIT 2:
Also inside my app/views folder, I have a master.blade.php file with the following structure:
...
#yield('header')
...
#yield('content')
...
The following should work for you and gives you more control on other errors as well:
App::error(function(Exception $exception, $code)
{
Log::error($exception);
switch ($code)
{
case 403:
return 'Your 403 message';//or load any custom view
break;
case 404:
return Redirect::to('/');//or load any custom view
break;
case 500:
return 'Your 500 Error Message';//or load any custom view
break;
default:
return Redirect::to('/');//or load any custom view
}
});
I'm trying to set up a basic web page, and it has a small music player on it (niftyPlayer). The people I'm doing this for want the player in the footer, and to continue playing through a song when the user navigates to a different part of the site.
Is there anyway I can do this without using frames? There are some tutorials around on changing part of a page using ajax and innerHTML, but I'm having trouble wrapping my head aroung getting everything BUT the music player to reload.
Thank you in advance,
--Adam
Wrap the content in a div, and wrap the player in a separate div. Load the content into the content div.
You'd have something like this:
<div id='content'>
</div>
<div id='player'>
</div>
If you're using a framework, this is easy: $('#content').html(newContent).
EDIT:
This syntax works with jQuery and ender.js. I prefer ender, but to each his own. I think MooTools is similar, but it's been a while since I used it.
Code for the ajax:
$.ajax({
'method': 'get',
'url': '/newContentUrl',
'success': function (data) {
// do something with the data here
}
});
You might need to declare what type of data you're expecting. I usually send json and then create the DOM elements in the browser.
EDIT:
You didn't mention your webserver/server-side scripting language, so I can't give any code examples for the server-side stuff. It's pretty simple most of time. You just need to decide on a format (again, I highly recommend JSON, as it's native to JS).
I suppose what you could do is have to div's.. one for your footer with the player in it and one with everything else; lets call it the 'container', both of course within your body. Then upon navigating in the site, just have the click reload the page's content within the container with a ajax call:
$('a').click(function(){
var page = $(this).attr('page');
// Using the href attribute will make the page reload, so just make a custom one named 'page'
$('#container').load(page);
});
HTML
<a page="page.php">Test</a>
The problem you then face though, is that you wouldnt really be reloading a page, so the URL also doesnt get update; but you can also fix this with some javascript, and use hashtags to load specific content in the container.
Use jQuery like this:
<script>
$("#generate").click(function(){
$("#content").load("script.php");
});
</script>
<div id="content">Content</div>
<input type="submit" id="generate" value="Generate!">
<div id="player">...player code...</div>
What you're looking for is called the 'single page interface' pattern. It's pretty common among sites like Facebook, where things like chat are required to be persistent across various pages. To be honest, it's kind of hard to program something like this yourself - so I would recommend standing on top of an existing framework that does some of the leg work for you. I've had success using backbone.js with this pattern:
http://andyet.net/blog/2010/oct/29/building-a-single-page-app-with-backbonejs-undersc/
You can reload desired DIVs via jQuery.ajax() and JSON:
For example:
index.php
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<a href='one.php' class='ajax'>Page 1</a>
<a href='two.php' class='ajax'>Page 2</a>
<div id='player'>Player Code</div>
<div id='workspace'>workspace</div>
one.php
<?php
$arr = array ( "workspace" => "This is Page 1" );
echo json_encode($arr);
?>
two.php
<?php
$arr = array( 'workspace' => "This is Page 2" );
echo json_encode($arr);
?>
ajax.js
jQuery(document).ready(function(){
jQuery('.ajax').click(function(event) {
event.preventDefault();
// load the href attribute of the link that was clicked
jQuery.getJSON(this.href, function(snippets) {
for(var id in snippets) {
// updated to deal with any type of HTML
jQuery('#' + id).html(snippets[id]);
}
});
});
});