Laravel Notifications Mark One Notification as Read - laravel

I'm new to laravel notification
I want when to click on the notification the link takes me to the invoice and the notification should be marked as read
I don't know how to mark one notification as read.
I know that I should take the notification id to mark the specific notification as read but I don't know how to is it in a function.
blade :
<div id="unreadNotifications">
#foreach (auth()->user()->unreadNotifications as $notification)
<div class="main-notification-list Notification-scroll mark-as-read" >
<a class="d-flex p-3 border-bottom"
href="{{ url('InvoicesDetails') }}/{{ $notification->data['id'] }}" data-id="{{$notification->id}}" >
<div class="notifyimg ">
<i class="la la-file-alt text-pink text-center"></i>
</div>
<div class="ml-3">
<h5 class="notification-label mb-1">
{{ $notification->data['title'] }}
{{ $notification->data['user'] }}
</h5>
<div class="notification-subtext">{{ $notification->created_at }}
</div>
</div>
</a>
</div>
#endforeach
</div>
Controller:
public function MarkAsRead_all (Request $request)
{
$userUnreadNotification= auth()->user()->unreadNotifications;
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
return back();
}
}
public function unreadNotifications_count()
{
return auth()->user()->unreadNotifications->count();
}
public function unreadNotifications()
{
foreach (auth()->user()->unreadNotifications as $notification){
return $notification->data['title'];
}

Create a link in the blade
<a class="d-flex p-3 border-bottom" href="{{ url('ReadNotification') }}/{{ $notification->data['id'] }}" data-id="{{$notification->id}}" >
Define a route for it
Route::get('ReadNotification/{id}','BlahBlahController#ReadNotification')->name('ReadNotification');
In Controller
public function ReadNotification($id)
{
$userUnreadNotification = auth()->user()
->unreadNotifications
->where('id', $id)
->first();
if($userUnreadNotification) {
$userUnreadNotification->markAsRead();
}
return back();
}

You just need to send the id of the notification to your controller and make it as read it is the same as you did for all read

Related

LaravelShoppingcart package, rowId property not working

Hi I'm using the LaravelShoppingcart package(https://github.com/bumbummen99/LaravelShoppingcart) and When I output {{ $item->rowId }} on the blade template I get the rowId on the browser when the blade is rendered but when I pass it in the qtyIncreased({{ $item->rowId}}) method and try to dump and die it in the livewire component it doesn't work. Mark you when I pass the $item->id and dump and die it, it works. The only error I'm getting is on the console and is:
Error handling response: TypeError: Cannot destructure property 'yt' of 'undefined' as it is undefined.
at chrome-extension://cmedhionkhpnakcndndgjdbohmhepckk/scripts/contentscript.js:535:6
Any assistance is highly appreciated.
Live wire blade component
<div class="cart-overlay {{ $active ? 'transparent' : '' }}">
<div class="cart {{ $active ? 'showCart' : '' }}">
<span class="close-cart" wire:click="$emit('closeCart')">
<i class="fas fa-window-close"></i>
</span>
<h2>Cart</h2>
<div class="cart-content">
#foreach ($cartContent as $item)
<div class="cart-item">
<img src="{{ $item->options->image }}" alt="product" />
<div>
<h4>{{ $item->name }}</h4>
<h5>${{ $item->price }}.99</h5>
<span class="remove-item">remove</span>
</div>
<div>
<i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item-
>rowId }})"></i>
<p class="item-amount">{{ $item->qty }}</p>
<i class="fas fa-chevron-down"></i>
</div>
</div>
#endforeach
</div>
<div class="cart-footer">
<h3>your total : $ <span class="cart-total">{{ $cartTotal }}</span></h3>
<button class="clear-cart banner-btn">clear cart</button>
</div>
</div>
</div>
Livewire Class component
<?php
namespace App\Http\Livewire;
use Gloudemans\Shoppingcart\Facades\Cart;
use Livewire\Component;
class CartOverlay extends Component
{
public $active = false;
public function render()
{
$cartContent = Cart::content();
$cartTotal = Cart::total();
return view('livewire.cart-overlay', compact(['cartContent', 'cartTotal']));
}
public $listeners = [
'showCart',
'closeCart'
];
public function qtyIncreased($rowId)
{
dd($rowId);
}
public function showCart()
{
$this->active = true;
}
public function closeCart()
{
$this->active = false;
}
}
I managed to fixed it, the parameter in the function should be passed in as a string. So, I added quotes around the parameter. Changed it from <i class="fas fa-chevron-up" wire:click="qtyIncreased({{ $item->rowId }})"></i>
TO
<i class="fas fa-chevron-up" wire:click="qtyIncreased('{{ $item->rowId }}')"></i>

list of products in an foreach with an foreach

For school I got a project to make an webshop so I try to make an admin dashboard where if u wanna see the users informatie, u also will see the orders the user placed.
Right now i try this
#foreach($user->order as $order)
<div class='col-sm-12 col-md-6 col-lg-6'>
<div class="card">
<div class="card-header">
<b>Order ID:</b> {{ $order->id }}
</div>
<div class="card-body">
<ul class="list-group list-group-flush">
<li class="list-group-item list-group-item-secondary text-center text-danger"><b>Info:</b></li>
<li class="list-group-item"><b>Datum:</b> {{ $order->date }}</li>
<li class="list-group-item"><b>Prijs:</b> € 200</li>
<li class="list-group-item list-group-item-secondary text-center text-danger"><b>Producten:</b></li>
#foreach($user->order->orderrow as $orderrow)
<li class="list-group-item">{{ $orderrow->product->productname }}</li>
#endforeach
</ul>
</div>
</div>
</div>
#endforeach
but how can i make it that when i want to see the orderrow from an other table then the order table. is able to be worked in an foreach on the users.show.blade.php
Product model
public function orderrows()
{
return $this->hasMany(Orderrow::class)->orderBy('date', 'desc');
}
Orderrow model
public function product()
{
return $this->belongsTo(Product::class);
}
Order model
public function user()
{
return $this->belongsTo(User::class);
}
public function orderrow()
{
return $this->hasMany(Orderrow::class);
}
User model
public function order()
{
return $this->hasMany(Order::class);
}
Try using this code for inner foreach loop.
#foreach($order->orderrow as $orderrow)
<li class="list-group-item">{{ $orderrow->product->productname }}</li>
#endforeach

How would you handle a Livewire popover?

I am looking for a way to make a popover/tooltip in Livewire, preferably without using Vue.
As of now I have created a UserPopover Livewire component with:
namespace App\Http\Livewire;
use App\User;
use Livewire\Component;
class UserPopover extends Component
{
public $isOpen = false;
public $user = null;
protected $listeners = [
'closeUserPopover',
'showUserPopover' => 'open',
];
public function open($username)
{
$this->user = User::whereName($username)->first();
$this->isOpen = true;
}
public function closeUserPopover()
{
$this->isOpen = false;
}
public function render()
{
return view('livewire.user-popover');
}
}
And rendered component:
<div>
#if($isOpen)
<div class="user-popover fixed top-0 shadow bg-white p-6 text-sm">
<h3>{{ $user->name }}</h3>
<div>{{ $user->profile->bio }}</div>
</div>
#endif
</div>
In my app.blade.php file I have included it with #livewire('user-popover') and I am calling the component with:
<a href="{{ route('users.show', $user) }}"
wire:mouseover="$emit('showUserPopover', {{ $user->name }})"
wire:mouseout="$emit('closeUserPopover')"
>
{{ $user->name }}
</a>
I don't know what to do next. Any ideas on positioning, re-usability and overall architecture?
For Example: you can use this tooltip :
Now, you can conditionally render class name (css class contains tooltip styles ) when isOpen is true.
<a class="{{ $isOpen ? 'tooltip' : '' }}" href="{{ route('users.show', $user) }}"
wire:mouseover="$emit('showUserPopover', {{ $user->name }})"
wire:mouseout="$emit('closeUserPopover')"
>
<span class="{{ $isOpen ? 'tooltiptext' : '' }}"> {{ $user->name }} </span>
</a>

How to retrieve specific column data from model bind show method?

I'm setting up a new project and want to retrieve data from passed model binding controller show method but output in show view is null
model :
class Content extends Model
{
protected $fillable = ['title','body'];
}
this is my controller :
public function show(Content $content)
{
return view('content.show',compact('content'));
}
this is my index :
#foreach ($content as $item)
<td>
<a class="btn btn-primary" href="{{ route('Content.show',$item) }}">View</a>
</td>
#endforeach
and this is show method that result is null:
<div class="card-header">
{{ $content->title }}
</div>
<div class="card-body">
{{ $content->body }}
</div>
<div class="card-footer">
{{ $content->created_at }}
/div>
i expect have result in show method because everything is okay but there isn't where is my problem ?
Updated :
dd($content)
because you try to get $content while it is $item now :)
<div class="card-header">
{{ $item->title }}
</div>
<div class="card-body">
{{ $item->body }}
</div>
<div class="card-footer">
{{ $item->created_at }}
</div>
I found the answer
I changed the route resource from resource('Content','ContentController') to
resource('content','ContentController') and the problem has been fixed !
i dont know why !
//used to show a list, like what you are trying to do
public function index()
{
$contents = Content::get();
return view('content.index',compact('contents'));
}
//used to show one item
public function show(Content $content)
{
return view('content.show',compact('content'));
}
What you have for you view works if you are looping over multiple content, otherwise you want:
{{ $content->title }}
</div>
<div class="card-body">
{{ $content->body }}
</div>
<div class="card-footer">
{{ $content->created_at }}
</div>```

Display data in a treeviewDisplay data in a treeview

I'm Working on Laravel.
I'm trying to display my data in a treeview. But i success only the first step. Someone of you can help please ?
DataBase Structure
My achievement
Code:
<button class=" btn-primary" type="button" data-toggle="collapse" data-target=".multi-collapse" aria-expanded="false" aria-controls=".multi-collapse" >
<span class="glyphicon glyphicon-eye-open"></span></button>
#foreach(App\Account::where('nomencloture', 'LIKE', '_')->get() as $item)
<div class="collapse multi-collapse" id="{{$item->nomencloture}}" >
<div class="list-group-item " >
<div class="col-md-2"> {{ $item->account_id }}</div>
<div class="col-md-2"> {{ $item->account_name }}</div>
<button class=" btn-primary" type="button" data-toggle="collapse" data-target="#PUT SOMETHINGHERE" aria-expanded="false" aria-controls="multiCollapseExample1" >
<span class="glyphicon glyphicon-eye-open"></span></button>
<br>
<br>
#foreach(App\Account::where('nomencloture', 'LIKE', '_______')->get() as $subitem)
<div class="collapse" id="{{ $subitem->nomencloture }}" >
<br>
<div class="list-group-item ">
<div class="col-md-2"> {{ $subitem->account_id }}</div>
<div class="col-md-2"> {{ $subitem->account_name }}</div>
</div>
</div>
#endforeach
</div>
</div>
#endforeach
You need to make some changes in your code, please find below step to change code
Step 1: Please create relations with your foreign key in your model file like
namespace App;
use Illuminate\Database\Eloquent\Model;
class Treeview extends Model
{
protected $table = 'treeview';
public $fillable = ['title','parent_id'];
/**
* Get the index name for the model.
*
* #return string
*/
public function childs() {
return $this->hasMany('App\Treeview','parent_id','id') ;
}
}
Step 2: Add below function in your controller and make some changes according to your table and model object
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function manageCategory()
{
$categories = Treeview::where('parent_id', '=', 0)->get();
return view('treeview/categoryTreeview',compact('categories')); // set the path of you templates file.
}
Step 3 : Add below code in your categoryTreeview templates file
<h3>Category List</h3>
<ul id="tree1">
#foreach($categories as $category)
<li>
{{ $category->title }}
#if(count($category->childs))
#include('treeview/manageChild',['childs' => $category->childs]) // Please create another templates file
#endif
</li>
#endforeach
</ul>
Step 4: Please Create another templates file manageChild add below code.
<ul>
#foreach($childs as $child)
<li>
{{ $child->title }}
#if(count($child->childs))
#include('treeview/manageChild',['childs' => $child->childs])
#endif
</li>
#endforeach
</ul>
Output displayed Like:
Category List
Cat_1
Cat_1_par_1
Cat_1_par_2
Cat_1_par_3
Cat_2
Cat_2_par_1
Cat_2_par_2
Cat_2_par_3
Cat_3
More information I found one link: https://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html

Resources