I have a function where:
After I click the link named "hapus", the related data will be deleted and I want to have a popup alert to show that the data has been deleted.
*sorry for my bad english
*hapus technically means destroy
this the code:
public function hapus(Request $request, $id)
{
DB::table('kelompok')
->where('id', $id)
->delete();
return redirect()->back();
}
Use with() in your controller
function hapus(Request $request, $id)
{
DB::table('kelompok')
->where('id', $id)
->delete();
return redirect()->back()->with('alert', 'Deleted!');
}
In your blade template, retrieve the session after being redirected from controller:
#if (session('alert'))
<div class="alert alert-success">
{{ session('alert') }}
</div>
#endif
Extending #pbwned's answer,
you can also use javascript alert box in your blade view to display the flash session/message.
For example:
<script>
var msg = '{{Session::get('alert')}}';
var exist = '{{Session::has('alert')}}';
if(exist){
alert(msg);
}
</script>
Hope it helps =)
Related
I want to do a function that returns a view, but if the item you're looking for isn't found, just return a hidden message in the view object.
public function getBannerById(string $banner_id): View
$banner = Banner::find($banner_id);
if (!$banner) {
return view()->append('<!-- Banner not found! -->'); // this not work
}
// more code ....
return view('banner', ['banner' => $banner]);
}
You can use the laravel Session for this, if the items isn't found so return to your view with the session message and then in the view you verify if exists the session and display the message.
In controller:
if (!$banner) {
Session::flash('error', "Banner not found!");
return view('view.name'); //
}
In View
#if (session('error'))
<div class="alert alert-danger" role="alert">
{{ session('error') }}
</div>
#endif
You can simply return Banner not found OR attach if-else statements in your blade file as below:
Controller code:
<?php
public function getBannerById(string $banner_id): View
$banner = Banner::find($banner_id);
return view('banner', compact('banner'));
}
View blade file:
#if(!$banner)
<p> Banner not found!</p>
#else
// Your view you wish to display
#endif
Update:
You can also use findOrFail method to automatically send 404 response back to the client if the banner is not found.
$banner = Banner::findOrFail($banner_id);
I am trying to pass the ID value from the blade file into ajax so it the data table will be routed and will call the ProductActivities function in ProductController.
Here's my snippet code for show() function in ProductController:
public function show($id)
{
$product = Product::find($id);
$data = Product::with(['user_modify'], 'id', $product->user_modified)
->where('product_id', '=', $id)->first();
$category = Category::select('category_name')
->where('category_id', '=', $data->product_type)
->pluck('category_name')
->first();
if($data->count() > 0){
return view('product.view', compact('data', 'category'));
}else{
Toastr::error('Product cannot be retrieved.', 'Error');
return view('product.index');
}
}
And here's the snippet code of JavaScript for the DataTable initialization in view.blade.php file:
#push('js')
<script>
$(function () {
$("#prod_log_tbl").DataTable({
responsive:true,
stateSave:false,
scrollY:true,
autoWidth: false,
ajax: {{ url('product/activities', [Request::segment(3)]) }},
order:[0, 'desc'],
searchable: false,
sortable:false,
fixedColumns: true
});
});
</script>
#endpush
line of code for route in web.php:
Route::get('product/activities/{id}', 'Master\ProductController#ProductActivities')->name('product/activities/{id}');
snippet code for the ProductActivities() function in ProductController:
public function ProductActivities($id)
{
$dataAct = Activity::all()->where('subject_id', '=', $id);
return Datatables::of($dataAct)->make(true);
}
And here's the result of my current progress:
In the screenshot of result the URL that ajax shows has additional values after the ID which I think causes of the DataTable error.
I don't know How I am getting this error? How can I implement the passing of ID from the view blade file through DataTable ajax into the ProductController?
P.S. I use Yajra/DataTable package for Laravel.
I think you do not need php echo in you Ajax url, route helper syntax is
{{ route('routeName', ['id' => 1]) }}
you need route name and parameter, another way is using url helper
{{ url('product/activities/', [Request::segment(3)]) }}
Beside this if you want to use model refer documentation, using first() will give you only one object, you need a collection, better considering get().
I have modified default auth method in controller which redirects user after custom login to set_password page. The problem is I can redirect it well to the desired page but I need to simultaneously pass two dynamic variables which are returned through querying database, which I am unable to pass with redirect.
My modified controller method is as follows:-
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
I know that to pass a variable to an view, I need to use the compact method like follows:-
return view('set_password', compact('activated_up', 'showuser'); but it cant be done with redirect.
The way I have redirected means I am passing parameters to route in the controller method, but I need to pass variables to the redirected view instead of parameters. How to achieve that?
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
you can use With
return redirect()->route('set_password')->with('data', ['some kind of
data']);
in your view
#if (session::has('data'))
The data is {{ session::get('data') }}
#endif
Tried as described in the answer by Kuldeep Mishra but could not achieve it though, anyways I found a workaround to achieve my desired output. What I did is changed my authenticated method to this:-
protected function authenticated(Request $request, $user)
{
return redirect()->route('set_password');
}
I only redirected to the set_password route from the above method and made new method in the controller to show the view with the compacted variables like this:-
public function setPasswordForm(Request $request)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return view('set_password', compact('activated_up', 'showuser'));
}
And a small change in route web.php file:-
Route::get('/set_password', 'Controller#setPasswordForm')->name('set_password');
So finally I was able to redirect to desired page with the desired view loaded with dynamic variables.
need print column data in my laravel app. My controller is TaskController.php
public function index()
{
$tasks = Task::all();
return view('tasks.index')->with('tasks', $tasks);
}
index.blade.php file is
#if(isset($tasks))
#foreach ($tasks as $task)
<h1>{{ $task->task_name }}</h1>
#endforeach
#endif
I am going include this index view file in show.blade.php file as following
#include('tasks.index')
but unable to print table data no any errors occurred. how can I print task_name in show view file?
If you are including the index blade inside the show blade, then you need to change your controller and pass data to the final blade (show instead of index) like this
return view('tasks.show')->with('tasks', $tasks);
If you want to show data on task.index then use this code.
public function index()
{
$tasks = Task::all();
//this line will pass $task data to this view
return view('tasks.index',compact('tasks'));
}
When the user save the object in the blade view, a function into my controller execute with post request. If the save was success I want to reload the page with the changes.
So I need to call index() again to get the refreshed data. Also, I want to show a success message. So the steps are:
index() function execute so the user can show his items.
the user choose an item and save it
save(Request $request) is called
everything save fine
save function call index() with sending a parameter with the flash success message?
I have this structure in my controller:
public function index(){
...
return view('agenda')->with(['agenda'=>$response]);
}
public function save(Request $request){
...
// here where I need to return index function with success message
}
How can I solve my problem?
Assuming the save action was called from the index page, simply redirect back
public function save(Request $request){
...
return redirect()->back()->with('message', 'Item saved');
}
If it was called from a different page, redirect to index page. Assuming your index() function matches a route /:
return redirect()->to('/')->with('message', 'Item saved');
Then display the flash message in your index view.
#if(Session::get('message'))
{{ Session::get('message') }}
#endif
For styling, if you are using bootstrap you can do
#if(Session::get('message'))
<div class="alert alert-success">
{{ Session::get('message') }}
</div>
#endif