Displaying data on view blade from database Laravel Notifications - php

I am new with working laravel. I have faced with a problem, when I am trying to display data on website using laravel notification.
This is my code:
GameBiddedNotification.php:
public function toDatabase($notifiable)
{
return[
'title' => $this->details['title'],
'text' => $this->details['text']
];
}
This is database template, in data column:
{"title":"hg","text":"\u10db\u10dd\u10d7\u10d0\u10db\u10d0\u10e8\u10d4 \u10e8\u10d4\u10db\u10dd\u10d5\u10d8\u10d3\u10d0"}
And this is my blade:
#foreach(auth()->user()->unreadNotifications as $notification)
#php
$data = json_decode($notification,true);
$test = $data['title'] ['text'];
#endphp
<a class="dropdown-item preview-item">
<div class="preview-thumbnail">
<div class="preview-icon bg-dark rounded-circle">
<i class="mdi mdi-xbox-controller text-success"></i>
</div>
</div>
<div class="preview-item-content">
{{-- <p class="preview-subject mb-1">{{ $notification->data['title'] }}</p> --}}
<p class="text-muted ellipsis mb-0">{{ $test }}</p>
</div>
</a>
#endforeach
I have tried multiple methods for example:
{{ $notification->data['title'] }}
, but result is the same. I am getting always an error saying
ErrorException (E_ERROR)
Undefined index: title

Based on your question, title and text is stored in 'data' column,
#foreach(auth()->user()->unreadNotifications as $notification)
<a class="dropdown-item preview-item">
<div class="preview-thumbnail">
<div class="preview-icon bg-dark rounded-circle">
<i class="mdi mdi-xbox-controller text-success"></i>
</div>
</div>
<div class="preview-item-content">
<p class="preview-subject mb-1">{{ $notification->data['title'] }}</p>
<p class="text-muted ellipsis mb-0">{{ $notification->data['text'] }}</p>
</div>
</a>
#endforeach

If you set up $casts properly on Notification-model to cast data-column as array, there is no need to use json_decode on blade.
$casts = [ 'data' => 'array' ];
What does your model look like?
btw: You should generally avoid #php — it usually points out code that belongs in the controller. I suppose this is test-only code? Instead, better dd($notification); to check data-attribute.

Related

Passing data from controller to modal

I'm am trying to get data from a database table and passing it to the modal but it is saying the array I am passing in undefined. Here is my Controller:
public function displayLocNotesForModal() {
$notesLoc = Note::all();
return view('/components/callCenter/modalLocNotes', ['notesLoc' => $notesLoc]);
}
Here is my Route:
Route::get('/components/callCenter/modalLocNotes', 'App\Http\Controllers\CallCenter\NoteController#displayLocNotesForModal');
Here is my modal:
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-headline">
Location #{{ $title }} Notes
</h3>
<div class="mt-2">
<p class="text-sm leading-5 text-gray-500">
{{-- {{ $slot }} --}}
#foreach($notesLoc as $notes)
#if($notes == $title)
works
#endif
#endforeach
</p>
</div>
</div>
I think it should be like below, assuming the components folder is in your resources/views folder
return view('components.callCenter.modalLocNotes', ['notesLoc' => $notesLoc]);
Also providing a link to docs Laravel nested view directories

Get movie title from TMDb with Laravel

I'm trying to get a movie title from TMDb API but:
ErrorException
Trying to access array offset on value of type int
Controller
$getPopuler = Http::withToken(config('services.tmdb.token'))
-> get('http://api.themoviedb.org/3/movie/popular')
-> json();
dump($getPopuler);
return view('pages.index', [
'getPopuler' => $getPopuler
]);
The result of dump($getPopuler) gives this.
Blade
#foreach ($getPopuler as $populer)
<div class="card text-center">
<div class="container">
<img class="img-fluid img-thumbnail" src="{{ asset('assets/image/poster01.jpg') }}" alt="Poster-01">
</div>
<div class="card-body">
<h6 class="card-title">{{ $populer['title'] }}</h6>
<i class="fas fa-star"><span class="ml-1">85%</span></i>
<p class="tahun"><small>Mar 20, 2020</small></p>
Read More
</div>
</div>
#endforeach
Thanks in advance.
Assuming your API call is working, I think you need to access the results array of the API response, as per the documentation.
#foreach ($getPopuler['results'] as $populer)
Side note: 'populer' is spelled 'popular'.

I can't get the route with full slug/post

I am trying to create a blog, when trying to bring the previous record and the next record in sight this returns the "post" but not the full path that should be / single / {slug}
CONTROLLER
public function single($slug)
{
$post = Post::where('slug', $slug)->first();
$previous = Post::where('id', '<', $post->id)->orderBy('id', 'asc')->where('status', 'PUBLISHED')->first();
$next = Post::where('id', '>', $post->id)->orderBy('id')->where('status', 'PUBLISHED')->first();
return view('web.single')->with(compact('post', 'previous', 'next'));
}
view
#if (isset($previous))
<div class="alert alert-success">
<a href="{{ url($previous->slug) }}">
<div class="btn-content">
<div class="btn-content-title"><i class="fa fa-arrow-left"></i> Previous Post</div>
<p class="btn-content-subtitle">{{ $previous->title }}</p>
</div>
</a>
</div>
#endif
</div>
<div class="col-md-6">
#if (isset($next))
<div class="alert alert-success">
<a href="{{ url($next->slug) }}">
<div class="btn-content">
<div class="btn-content-title">Next Post <i class="fa fa-arrow-right"></i></div>
<p class="btn-content-subtitle">{{ $next->title }}</p>
</div>
</a>
</div>
#endif
Route
Route::get('/single/{slug}', 'Web\WorkController#single')->name('single');
When clicking on previous or next, the route shown by the browser is http://127.0.0.1:8000/et-autem-tempora
and should be
http://127.0.0.1:8000/single/et-autem-tempora
You are creating your url doing this:
<a href="{{ url($next->slug) }}">
...
</a>
The url('path') helper returns something like this: http://{base_url}/{path}.
Try to use your named route instead:
<a href="{{ route('single', ['slug' => $next->slug]) }}">
...
</a>
I think this should also work:
<a href="{{ route('single', $next->slug) }}">
...
</a>

Variable from a controller to a view without a foreach loop in Laravel

I want to show the content of an article created, I have a show method in my controller
public function show_capitulos($id)
{
$data=Capitulo::select('capitulos.titulo as capitulo','capitulos.descripcion','capitulo_secciones.contenido','capitulo_videos.video')
->join('capitulo_secciones','capitulos.id','=','capitulo_secciones.capitulo_id')
->join('capitulo_videos','capitulos.id','=','capitulo_secciones.capitulo_id')
->where('capitulos.id',$id);
return view('administrador.capitulos.show')->with(['data'=>$data]);
}
And I want to pass that data to my view in Laravel, without using a foreach loop, but it keeps showing the error of undefined variable
I've read that you use the get() function to retrieve a collection of data and that with first() you get only one.
**UPDATE:**After adding the get() in my code a new error is showing
error
This is what I have in my view
<div class="row" id="contenido-cursos">
<div class="justify-content-between flex-wrap align-items-center pb-2 mb-3 ">
<h2 class="title">Capítulo 1</h2>
<h1 class="nombre-capitulo"> {{ $data->capitulo }}</h1>
<div class="linea-capitulo"></div>
<p id="titulo-capitulo">{{$data->descripcion}}</p>
<h2 class="title sub">Contenido</h2>
<p class="contenidocap"><br>{{$data->contenido}}</p>
<a class="btn btn-theme btn-block title extras" href="#"><img src="{{asset('assets/img/recursos.png')}}"> {{ __('Descargar Cap1.pdf') }}</a>
<a class="btn btn-theme btn-block title extras" href="#"><img src="{{asset('assets/img/test.png')}}"> {{ __('Hacer Test 1') }}</a>
</div>
</div>
You are defining the query but not executing it.
After concatenating where() and join() you have to call get() to retrieve the results for the query.

Make a function containing blade syntax

I have this blade in my view. Right now, I have 6 blocks of them in my view because I'm not sure how to refactor it.
<div class="row filemanager">
<div class="col-sm-12">
#foreach ($devices as $device)
#if( $device->vlan_id == 100 AND $device->device_activity == 'ACTIVE' )
<div class="col-xs-6 col-sm-4 col-md-2 text-center">
<div class="thmb">
<div class="btn-group fm-group" style="display: none;">
<button type="button" class="btn btn-default dropdown-toggle fm-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu fm-menu" role="menu">
<li id="device-menu">
<a class="changeDeviceNameBtn" href="#"><i class="fa fa-pencil"></i> Change Device Name </a>
</li>
</ul>
</div>
<div class="thmb-prev">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if(isset($device->device_name))
{{-- Show base on device name --}}
<img src="/images/photos/devices/{{img($device->device_name)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- No Device Name Set --}}
#if($device->hostname != '')
{{-- Show base on hostname --}}
<img src="/images/photos/devices/{{img($device->hostname)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- Show default --}}
<img src="/images/photos/devices/no-img.jpg" class="draggable img-responsive" alt="">
#endif
#endif
</a>
</div>
<h5 class="fm-title device_name">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if($device->hostname == '')
No Devicename
#else
{{ $device->hostname}}
#endif
</a>
</h5>
<h5 class="text-muted device_ip">{{$device->ip_address}}</h5>
<h5 class="text-muted device_mac">{{$device->device_mac}}</h5>
<?php
$status = ucfirst(strtolower($device->device_activity));
if ($status == 'Active'){
$color = '#1CAF9A';
}else{
$color = '#D9534F';
}
?>
<h5>{{ $status }}
<i class="fa fa-circle" style="color:{{$color}}; margin-left: 7px;"></i>
</h5>
</div>
</div>
#endif
#endforeach
</div>
</div>
I want to make a function containing that blade, and only replace my
$device->vlan_id, and my $device->device_activity.
Example,
public static deviceRow(100,ACTIVE){
... my blade ...
}
Now, I just that function 6 times, rather than duplicate that block of code 6 times.
Is it even possible ?
Any hints / suggestions on this will be much appreciated !
You can make a partial with your blade and send a variable as a parameter:
In your parent view do something like this:
#foreach($somelist as $item)
#include('view.partial', ['name' => $item->name])
#endforeach
And in a file called partial.blade.php, do something like this:
{{ $device->$name }}
It's the main idea. Tell me if it helps...
You could create a new view and send some parameters with it while including:
#include('my.view', ['device' => $myDevice, 'activity' => 'ACTIVE'])
The keys of the array will be available as variables in your view.
The variable $myDevice would be available as $device in the view my.view

Categories