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
Related
I wonder in my application can't show flash message. I have tried many solutions on stackoverflow but my problem is not solved.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class DebugController extends Controller
{
public function get()
{
// Visit direct page flash session is working
// Eg: localhost/debug/get
// But if I send request all flash sessions are not working
return Redirect::route('home')->with('success', 'Working session on visit direct page!');
}
public function post(Request $request)
{
// All flash sessions are not working
session()->flash('anything', 'Session not working!');
// Working session
session()->put('message', 'Working session!');
// Session not working, is 'success' key reserved?
session()->put('success', 'Session not working!');
return Redirect::route('home')->with('anything', 'Session not working!'); // Session not working
}
}
Route:
Route::get('debug/get', 'DebugController#get');
Route::post('debug/post', 'DebugController#post');
View:
#if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
#endif
// Working session
#if(Session::has('message'))
<div class="alert alert-success">
{{ Session::get('message') }}
</div>
{{ session()->forget('message') }}
#endif
I have tried to modify middle ware in Kernel from this solution but still not working
Laravel Version: 8.x.x
PHP Version: 7.4.x
You will need to do Session::has('anything') in your view instead of Session::has('success').
The first parameter is the key that can be accessed with has or get.
If the above is already done, you might need to use the Facade: Session::flash('anything','content');
Take a look at this.
In case you want to remove the Session data
you have to use flush instead of flash().
Laravel documentation
I would like to use SweetAlert to display my data.
I did my function
public function registration()
{
Gate::authorize('admin-level');
$url = URL::signedRoute(
'register',
now()->addMinutes(20));
return redirect('users')->with('success','$url');
}
and route that goes with it
Route::get('registration', [App\Http\Controllers\UserController::class, 'registration'])->name('registration');
The problem is with message, since I downloaded SweetAlert with composer I should probably got everything working, but then when I try to execute my class with button threw the route:
<button type="button" class="btn btn-outline-primary">{{ __('Registration link') }}</button>
#if(session('succes_message'))
<div class= "alert alert-succes">
{{session('succes_message')}}
</div>
#endif
Nothing pops up(when it should)
What might be wrong with it?
When you use ->with() it means, store items in the session for the next request.
return redirect('users')->with('success', '$url');
Here comes the question. What do you do after this?
Create a notification information or an alert (popup with SweetAlert)?
If it will be used as a notification, your code has no problem. If you want to make alert (popup with SweetAlert), your understanding is wrong.
Just because the class you are using uses the name alert, doesn't mean it make an alert with SweetAlert.
To use SweetAlert, you can add JavaScript in the header or before the </body> tag:
<script>
#if($message = session('succes_message'))
swal("{{ $message }}");
#endif
</script>
Or to use SweetAlert2 :
<script>
#if($message = session('succes_message'))
Swal.fire(
'Good job!',
'{{ $message }}',
'success'
)
#endif
</script>
If you are confused about placing the script in a specific blade view, please read my answer here.
I'm trying to save data into db but its not saving and says that object not found, can anyone suggest me solution, i am following this tutorial: https://laracasts.com/series/laravel-from-scratch-2018/episodes/10
controller:
public function index()
{
$projects = Project::all();
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store()
{
$project = new Project();
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
routes:
Route::get('/projects','ProjectsController#index');
Route::post('/projects','ProjectsController#store');
Route::get('/projects/create','ProjectsController#create');
create.blade.php:
<form method="POST" action="/projects">
{{ csrf_field() }}
<div>
<input type="text" name="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Project description"></textarea>
</div>
<div>
<button type="submit">Create Project</button>
</div>
</form>
index.blade.php:
#foreach($projects as $project)
<li>{{ $project->title }}</li>
#endforeach
You have missed out passing request parameter in the controller store()
public function store(Request $request)
{
$project = new Project();
$project->title = $request->title;
$project->description = $request->description;
$project->save();
return redirect('/projects');
}
And also don't forget to include use Illuminate\Http\Request; above(outside) controller class.
The Laravel code you've posted is correct under a properly configured website. The error from your comments:
Object not found! The requested URL was not found on this server. The
link on the referring page seems to be wrong or outdated. Please
inform the author of that page about the error. If you think this is a
server error, please contact the webmaster. Error 404 localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7
is an Apache error page, which means it's not requesting a page from your laravel project at all. The data is probably saving in your database, but then you redirect away to a page that is outside your project, and Apache can't find it.
Your website is located at http://localhost/laravel/public, which means you need to access the projects page at http://localhost/laravel/public/projects. However, redirect('/projects') gives you an absolute path instead of a relative path, sending you to http://localhost/projects, which does not exist.
Solutions
Since this is a local development project, I'm going to skip the issues with the improper Apache configuration and focus on other ways to avoid the error.
Option 1
Use a named route:
Route::get('/projects','ProjectsController#index')->name('projects.index');
and use the name of the route for the redirect:
return redirect()->route('projects.index');
This should generate correct urls within your project.
Option 2
Use serve for development instead of Apache.
Open a terminal in your Laravel project directory and run this command:
php artisan serve
This will start PHP's built-in webserver at http://localhost:8000, skipping Apache entirely. During development this is perfectly fine.
How can i redirect with success or error message without using sessions in laravel
Currently I am using the following code for redirect :
return redirect('dashboard')->with('status', 'Profile updated!');
But this code Need session to display the success message.
You can set another variable status type along with the status like below,
return redirect('dashboard')->with(['status'=>'Profile updated!','status_type'=>'success']);
And in your dashboard blade file use
#if(isset($status))
<p class="alert alert-{{ $status_type }}" >{{ $status }}</p>
#endif
I have application where I show list of all items from specified table. Then I have in this page link to create new record in database, where it redirect me to form page. After submit the form I want to redirect back to list of items with success message like f.e."Record was inserted". How to do it in laravel? I tried something like this , but this doesnt work:
Redirect::to("Homepage#list")->to('message', 'Record was inserted')
also I tried (but also it doesnt work):
Session::flash('message', 'Record was inserted')
and in blade:
Session::get('message')
You need to update your code to
Redirect::to("Homepage#list")->with('message', 'Record was inserted');
and add the code for the flash message in your View
#if(Session::has('message'))
<div class="alert alert-success"><em> {!! session('message') !!}</em></div>
#endif