Post notification redirect user to post id - laravel

i have database notification, and i was trying to redirect the user whenever they click one of the notification link it will go to the exact POST id,then mark as read that particular notification being selected. but i couldn't make it work
Here is my controller
public function readbyid($id){
if(isset($notification->data['id'])){
return redirect()->action(
'PostController#show', ['id' => $notification->data['id']]
);
}else{
return redirect()->back();
}
}
Notification Model
public function toArray($notifiable)
{
return [
'id' => $this->post->id,
];
}
Blade View:
<li class="nav-item dropdown"> <a id="messages" rel="nofollow" data-target="#" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link"><i class="fa fa-bell"></i>#if (auth()->user()->unreadnotifications->count())<span class="badge badge-warning">{{auth()->user()->unreadnotifications->count()}}</span>#endif</a>
<ul aria-labelledby="notifications" class="dropdown-menu">
<li><a rel="nofollow" href="{{Url('markread')}}" class="dropdown-item all-notifications text-center"> <strong> <i class="fa fa-bell"></i>Mark all as Read</strong></a></li>
#foreach(auth()->user()->unreadnotifications as $notify)
<li><a rel="nofollow" href="{{Url('markread/{id}')}}" class="dropdown-item d-flex">
<div class="msg-body">
<h3 class="h5">{{$notify->data['title']}}</h3>
</div> </a></li>#endforeach
</ul>
</li>
Route:
Route::get('markread/{id}', 'NotifyController#readbyid' );

Related

SignInManager.IsSignedIn(User) returns false on identity scaffolding

using default code for Identity in Blazor I have seen that by using authentication cookies the user isn't set as logged, while the cookie exist in the browser and the link/action to be signed out wasn't callled
What could I possibly need to check what would be causing this behavior?
the only line of code I had used is a js code to call automatically the signout action on post which only exist in the logout view(which is only called in the lougout link, but for example calling the login page shows as if it wasnt logged in(which is checked at the loging partial view
<script>
(() =>
{
document.getElementById('logoutFormLocal').submit();
})();
</script>```
#using Microsoft.AspNetCore.Identity
#inject SignInManager<IdentityUser> SignInManager
#inject UserManager<IdentityUser> UserManager
<ul class="navbar-nav">
#if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a id="manage" class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello #UserManager.GetUserName(User)!</a>
</li>
<li class="nav-item">
<form id="logoutForm" class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="#Url.Page("/Index", new { area = "" })">
<button id="logout" type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" id="register" asp-area="Identity" asp-page="/Account/Register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" id="login" asp-area="Identity" asp-page="/Account/Login">Login</a>
</li>
}
</ul>

Laravel shows error displaying notification data

this is my code
in the app.blade.php, i added this code to the navbar. The count method is working fine. I just can't display the notification data value.
<a class="nav-link" data-toggle="dropdown" href="#">
<i class="fa fa-comments">Business Alerts</i>
<span class="badge badge-danger navbar-badge">{{ count(auth()->user()->unreadNotifications) }}</span>
</a>
<ul class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
#foreach (auth()->user()->unreadNotifications as $notification)
<li class="dropdown-divider"></li>
<a href="#" class="dropdown-item">
{{--<i class="fas fa-info mr-2 green"></i>{{ $notification->type }}--}}
#include('layouts.partials.notification.'.snake_case(class_basename( $notification->type )))
{{ $notification->data['user']['email'] }}//it works only if i remove this line
</a>
#endforeach
<div class="dropdown-divider"></div>
See All Messages
</ul>
</li>
#endforeach
In the browser i'm getting this
Undefined index: alert (View: C:\laragon\www\test\resources\views\layouts\partials\notification\test.blade.php)
when i try
dd( $notification->data['alert']['id'] )
it display 2
this is the controller code
public function store(Request $request)
{
$user = User::find($request['user_id']);
$alert = ProductAlert::findOrFail($request['alert_id']);
$alert->alert_qty -= $request['alert_qty'];
$alert->save();
$user->notify(new StockEpuise(json_decode($alert)));
return invest::create([
'alert_id' => $request['alert_id'],
'user_id' => $request['user_id'],
'alert_type' => $request['alert_type'],
'alert_qty' => $request['alert_qty'],
]);
}```
I don't know what is the problem

how can i show a notification when an admin asiggne an order to a deliverer LARAVEL?

when i store new order i want th deliverer with "deliverer_id" get notified
public function store(Request $request)
{
$order = Order::create([
'deliverer_id' => $request->input('deliverer_id'),
'product_id' => $request->input('product_id'),
'responsible_id'=>$request->input('responsible_id'),
'quantity'=> $request->input('quantity'),
'totalprice' => $request->input('totalprice'),
'client_name' => $request->input('client_name'),
'client_phone' => $request->input('client_phone'),
'client_city'=> $request->input('client_city'),
]);
return redirect()->route('orders.index');
}
i want to show a notification say there is a new order on this view
<li class="nav-item btn-rotate dropdown new-order">
<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="nc-icon nc-bell-55"></i>
<p>
<span class="badge badge-link text-danger notif-count" data-count="8">8</span>
</p>
</a>
<div class="dropdown-menu dropdown-menu-right newor">
<a class="dropdown-item" href="#">New order</a>
</div>
</li>
Simple wayout here is to set the flash session data in controller method, for example:
session()->flash('newOrderReceived', true);
Then on view file, you can check to see if the session has that key. If yes, then show the notification. For example, to show bootstrap4 alert message if session has given key:
#if(session('newOrderReceived'))
<div class="alert alert-primary" role="alert">
There is a new order
</div>
#endif

Laravel submit form from a tab and return to same tab in view

I have a view with multiple tabs. Each having different forms. When I submit a form from one tab, it returns to same page but primary tab. How could I get to return to same tab from which I was working.
Controller
public function recieve(Request $request)
{
$item = Item::find($request->input('item'));
$item->recieved_at = now();
$item->recieved_status = "1";
$item -> save();
return redirect('/files/'.$item->file)->with('success','Item Recieved Confirmed');
}
view - tabs
<ul class="nav nav-tabs" id="fileTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="profile-tab" data-toggle="tab" href="#profile" role="tab"
aria-controls="profile" aria-selected="true">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" id="job-tab" data-toggle="tab" href="#job" role="tab" aria-controls="job"
aria-selected="false">Job</a>
</li>
<li class="nav-item">
<a class="nav-link" id="items-tab" data-toggle="tab" href="#items" role="tab" aria-controls="items"
aria-selected="false">Items</a>
</li>
<li class="nav-item"><a class="nav-link" id="mechanic-tab" data-toggle="tab" href="#mechanic" role="tab"
aria-controls="mechanic" aria-selected="false">Labour Hours</a>
</li>
<li class="nav-item">
<a class="nav-link" id="accounts-tab" data-toggle="tab" href="#accounts" role="tab"
aria-controls="accounts" aria-selected="false">Accounts</a>
</li>
<li class="nav-item">
<a class="nav-link" id="accounts-tab" data-toggle="tab" href="#preview" role="tab"
aria-controls="accounts" aria-selected="false">Print Preview</a>
</li>
</ul>
Form
#if($file->job_status == "Closed")#else
{!! Form::open(['action' => 'FilesController#item','method' => 'POST']) !!}
<div class="row pt-3 pb-1">
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::text('part_number','',['class'=>'form-control','placeholder'=>'Part Number'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-6">
{{Form::text('description','',['class'=>'form-control','placeholder'=>'Part Name'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::text('qty','',['class'=>'form-control','placeholder'=>'Qty'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::select('recieved_by',$users,null,['class'=>'form-control'])}}
</div>
</div>
{{Form::hidden('file',$file->id)}}
{{Form::submit('Release Item',['class'=>'form-control btn-danger btn btn-sm'])}}
{!! Form::close() !!}
#endif
I tried using hashtag tab name in return redirect statement,
return redirect('/files/'.$item->file."#items-tab")->with('success','Item Recieved Confirmed');
it would simply highlight the name but would not select. How could I achieve it?
your tab navigation,
<ul class="nav nav-tabs" id="tabMenu" role="tablist">
<li><i class="fa fa-camera" aria-hidden="true"></i> Galleries</li>
</ul>
hidden input field for tab
<input type="hidden" name="tab" value="Galleries">
RedirectRoute
$tab = $request->get('tab');
return back()->withInput(['tab'=>$tab]);
Javascript,
<script>
//redirect to specific tab
$(document).ready(function () {
$('#tabMenu a[href="#{{ old('tab') }}"]').tab('show')
});
</script>
If you want to do it in laravel than all you can do is,
-Give an id to each tab and an write an active class.
-Put a hidden input with value of the parent tab id.
-So, laravel will receive the tab id in the request object.
-Now, you can do whatever and return the tab id with to the view.
-Give a if condition to each tab item and check if matches the id from laravel response.
The match the id set the active class to it.
example
<ul>
<li id='tab1 {{ session()->get('tabId') === 'tab1' ? 'active' : '' }}'></li>
<li id='tab2 {{ session()->get('tabId') === 'tab2' ? 'active' : '' }}'></li>
</ul>
// tab1 body
<form>
<input type="hidden" name="tabId" value="tab1">
...
</form>
// tab2 body
<form>
<input type="hidden" name="tabId" value="tab2">
...
</form>
// controller
public funciton(Request $request) {
//tabId
$tabId = $request->input('tabId')
....
....
return redirect()->back()->with('tabId' => $tabId);
}
You can do this:
Controller:
public function tab() {
$active_tab = "tab2";
return view('tabs.index', compact('active_tab'));
}
View:
<ul class="nav nav-tabs">
<li class="#if($active_tab=="tab1") active #endif"><a data-toggle="tab" href="#home">Home</a></li>
<li class="#if($active_tab=="tab2") active #endif"><a data-toggle="tab" href="#menu1">Menu 1</a></li>
<li class="#if($active_tab=="tab3") active #endif"><a data-toggle="tab" href="#menu2">Menu 2</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade #if($active_tab=="tab1") in active #endif">
<h3>HOME</h3>
<p>Some content.</p>
</div>
<div id="menu1" class="tab-pane fade #if($active_tab=="tab2") in active #endif">
<h3>Menu 1</h3>
<p>Some content in menu 1.</p>
</div>
<div id="menu2" class="tab-pane fade #if($active_tab=="tab3") in active #endif">
<h3>Menu 2</h3>
<p>Some content in menu 2.</p>
</div>
</div>
You can show a specific tab with this code:
function showTab(tab){
$('.nav-tabs a[href="#' + tab + '"]').tab('show');
};
// Verify location.hash
if (window.location.hash !== ''){
showTab(window.location.hash);
}
But I would pass a query string in the return, just to make sure there will be no collision with other eventual hashes. Something like:
return redirect('/files/'.$item->file."?openTab=your-tab-id")->with('success','Item Recieved Confirmed');
Of course, you would change the "location.hash" verification to a query string verification. Example: https://davidwalsh.name/query-string-javascript

VueJS get variable as text

I'm working on a Laravel project and i use VueJS for notifications system.
However, i'm a real noob in VueJS dev'. Consequently, i don't know how to print a variable as text.
Situation : System of notifications work and i would like to print the number of notifications in a bootsrap badge. So, i did this :
<i class="fa fa-bell-o" aria-hidden="true"></i> Notifications <span id='badge' class="badge badge-info">{{notifications.length}}</span> <span class="caret"></span>
With this code in my Notification.vue file :
<template>
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
<i class="fa fa-bell-o" aria-hidden="true"></i> Notifications <span id='badge' class="badge badge-info">{{notifications.length}}</span> <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li style="text-align:center" v-for="notification in notifications">
<a href="#" v-on:click="MarkAsRead(notification)">
{{notification.data.idea.name}} a été validée !<br>
</a>
</li>
<li style="text-align: center" v-if="notifications.length==0">
Aucune notification !
</li>
</ul>
</li>
</template>
<script>
export default {
props: ['notifications'],
methods: {
MarkAsRead: function (notification) {
var data = {
id: notification.id
};
axios.post('/notification/read', data).then(response => {
window.location.href = "/manif/";
});
}
}
}
</script>
Finally i only see {{notifications.length}} write in the badge instead of a number...
Can you help me about this ?
Thank's
The raw mustache tags are shown because you've used v-pre directive in the parent element, which tells Vue to not compile the element and its children:
<a id="navbarDropdown" ... v-pre> <!-- Don't use v-pre here -->
...
<span ...>{{notifications.length}}</span>
</a>
Simply removing that directive should resolve the issue: demo

Resources