Trying to get property 'expiry_date' of non-object in laravel - php

Please help to solve my this issue
my controller code is
public function index()
{
$callrecordings = DB::table('callrecordings')
->join('users', 'users.id', '=', 'callrecordings.user_id')
->leftJoin('disableapp', 'callrecordings.user_id', '=', 'disableapp.user_id')
->select('callrecordings.*', 'users.expiry_date')
->where('callrecordings.user_id', '=', Auth::id())
->where(function($q){
$q->where('disableapp.status', '=', 1)
->orWhereNull('disableapp.status');
})
->paginate(5);
dd($callrecordings);
if($callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString()){
$result = 'That email belongs to an existing referrer.';
return view('frontend.views.package-expire', compact('result'));
}
else{
return view('frontend.views.callrecordings', compact('callrecordings'));
}
}
my blade file is
<table id="example" class="table table-bordered dt-responsive nowrap dataTable no-footer dtr-inline" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Duration</th>
<th>Size</th>
<th>Direction</th>
<th>Audio</th>
<th>Download</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
#foreach ($callrecordings as $callrecording)
<tr>
<td>{{ $callrecording->name }}</td>
<td>{{ $callrecording->phone}}</td>
<td>{{ $callrecording->duration}}</td>
<td>{{ $callrecording->size}}</td>
<td>
#if ($callrecording->direction === 'Incoming')
<span class="label label-success">Incoming</span>
#elseif ($callrecording->direction === 'Outgoing')
<span class="label label-info">Outgoing</span>
#elseif ($callrecording->direction === 'Rejected')
<span class="label label-warning">Rejected</span>
#else
<span class="label label-danger">Missed</span>
#endif
</td>
<td>
<audio id='sound1'>
<source src="{{ URL::to('/') }}/uploads/audio/{{ $callrecording->audio }}" type="audio/ogg">
<source src="{{ URL::to('/') }}/uploads/audio/{{ $callrecording->audio }}" type="audio/mpeg">
</audio>
<div class="play btn btn-success btn-xs" id="btn1">play</div>
</td>
<td>
<i class="icon-download-alt"> </i> Download
<!---button class="btn btn-primary btn-xs" data-download="{{ URL::to('/') }}/uploads/audio/{{ $callrecording->audio }}"><i class="fa fa-fw fa-download"></i> Download</button---->
</td>
<td>{{ $callrecording->created_at }}</td>
</tr>
#endforeach
</tbody>
</table>
this condition is working but if I do table status 0 or no value-added in a table it gives this error message Trying to get property 'expiry_date' of non-object in.
I attached my dd($callrecordings);
Please tell me how Should I overcome from this issue
thanks in advance.

you missed something in your code which check 0 element for data if data not exist ,so we need to make check of existance for record
if(isset($callrecordings[0]->expiry_date) && $callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString()){

Related

Undefined Variable on fetching data in view

I'm trying to print values in from database in table. However the error shows undefined variable outlets in app.blade.php. I'm a beginner in laravel I've copied the function from index.blade.php where it is working. Any help will be highly appreciative.
App.blade.php:
<div class="col-md-12">
<div class="card">
<div class="card-header">
<form method="GET" action="" accept-charset="UTF-8" class="form-inline">
<input type="submit" value="{{ __('outlet.search') }}" class="btn btn-secondary">
{{ __('app.reset') }}
</form>
</div>
<table class="table table-sm table-responsive-sm">
<thead>
<tr>
<th class="text-center">{{ __('app.table_no') }}</th>
<th>{{ __('outlet.name') }}</th>
<th class="text-center">{{ __('app.action') }}</th>
</tr>
</thead>
<tbody>
#foreach($outlets as $key => $outlet)
<tr>
<td class="text-center">{{ $outlets->firstItem() + $key }}</td>
<td>{!! $outlet->name_link !!}</td>
<td>{{ $outlet->address }}</td>
<td>{{ $outlet->latitude }}</td>
<td>{{ $outlet->longitude }}</td>
<td class="text-center">
{{ __('app.show') }}
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="card-body">{{ $outlets->appends(Request::except('page'))->render() }}</div>
OutletController.php
public function index()
{
$this->authorize('manage_outlet');
$outletQuery = Outlet::query();
$outletQuery->where('name', 'like', '%'.request('q').'%');
$outlets = $outletQuery->paginate(25);
return view('outlets.index', compact('outlets'));
}
public function app()
{
$outlets = "this is outlet";
return view('outlets.app', compact('outlets'));
}
Error:
ErrorException (E_ERROR)
Undefined variable: outlets (View: /home/converse/laravel_leaflet_1/resources/views/layouts/app.blade.php) (View: /home/converse/laravel_leaflet_1/resources/views/layouts/app.blade.php)

Laravel Blade not showing data from controller

I want to show data in tabular form in Laravel Blade View. Now, data is not showing up in blade, but when I check in controller using print_r it shows up Following is the code for Controller:-
public function index(){
$pickup = PickupLocation::select('*')
->whereNull('deleted_at')
->get()
->toArray();
$page = 'pickup_locations';
return view('backEnd.pickupManagement.index',compact('page'));
}
Following is the code in web.php:-
Route::match(['get','post'],'pickup-locations','backEnd\pickupManagement\PickupController#index');
And following is the code I am using View:-
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Address</th>
<th width="13%">Action</th>
</tr>
</thead>
<tbody>
#if(!empty($pickup))
#foreach($pickup as $key=>$value)
<tr>
<td>{{ ucfirst($value['name']) }}</td>
<td>{{ ucfirst($value['contact_no']) }}</td>
<td>{{ $value['location'] }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
add $pickup variable as second argument in your compact method.
Try like this
return view('backEnd.pickupManagement.index',compact('page', 'pickup')); }
Happy codding

Show compare attributes for every product

I have a code:
<table class="table table-bordered">
<tr>
<td class="align-middle">
</td>
#forelse($products as $product)
<td>
<div class="comparison-item">
<a class="comparison-item-thumb" href="shop-single.html">
<img src="{{ Voyager::image($product->image) }}" alt="Apple iPhone X">
</a>
<a class="comparison-item-title" href="shop-single.html">{{ $product->title }}</a>
</div>
</td>
#empty
Нет товаров для сравнения
#endforelse
</tr>
#foreach($products as $keyp => $product)
#foreach($product->prodChar as $key => $char)
<tr>
<th>{{ $char->prodCharacter->title }}</th>
<td>{{ $char->value }}</td>
<td>-</td>
<td>-</td>
</tr>
#endforeach
#endforeach
<tr>
<th></th>
<td><a class="btn btn-outline-primary btn-sm btn-block" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-check-circle" data-toast-title="Product" data-toast-message="successfuly added to cart!">Add to Cart</a></td>
<td><a class="btn btn-outline-primary btn-sm btn-block" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-check-circle" data-toast-title="Product" data-toast-message="successfuly added to cart!">Add to Cart</a></td>
<td><a class="btn btn-outline-primary btn-sm btn-block" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-check-circle" data-toast-title="Product" data-toast-message="successfuly added to cart!">Add to Cart</a></td>
</tr>
</table>
Image how this show:
I need write: "-". For product where attribute is none. Now I show all attributes in one column for every product. How I can fix this problem? I need compare keys of arrays?
If I understand correctly, you're talking about this small section here:
#foreach($product->prodChar as $key => $char)
<tr>
<th>{{ $char->prodCharacter->title }}</th>
<td>{{ $char->value }}</td>
<td>-</td>
<td>-</td>
</tr>
#endforeach
So you have two options here:
Option #1: You could compare right in the view as follows:
<td>{{ $char->value?: "-" }}</td>
This would return a - whenever your value is null.
Option #2: You could create a mutator within the ProductChar model as follows:
// ...your product model code
public function getValueAttribute() {
return $this->attributes['value']?: "-";
}
This would make it so that when you call $char->value it will use the value (if not null) or return a - if it is null.

Blade Engine Elements Render Sorting

I have this code :
<div class="table-responsive">
<table class="table table-condensed table-hover">
<tr>
<th>#</th>
<th>Name</th>
<th>username</th>
<th>Email</th>
<th>Rank</th>
<th>Join</th>
</tr>
#if($count)
#foreach($users as $i => $user)
<tr>
<td>{{ $i+1 }}</td>
<td>{{ $user->first_name }} {{ $user->last_name }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>
#if($user->rank == 0)
Admin
#else
User
#endif
</td>
<td>{{ $user->created_at }}</td>
{{ Form::hidden('id', $user->id,['class'=>'id']) }}
{{ Form::hidden('username', $user->username,['class'=>'username']) }}
<td><button id="submit" type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">
Delete
</button></td>
</tr>
#endforeach
#else
<div class="alert alert-success" style="text-align:center">
There is no users in the Site
</div>
#endif
which used Blade Template engine, but in case that's count is false the alert div appears before Heading of table
How can this problem be fixed ?
I may be missing something here, the code above seems incomplete (table is not closing). But why not move your alert outside of the other if so you can place it where you need it:
#if(!$count)
<div class="alert alert-success" style="text-align:center">
There is no users in the Site
</div>
#endif

Searching in db using Laravel 4.2

I want to search and display specific data from db and the method that I have made is not retrieving data from database, if i print it just gives a empty array.
My code is as follow:
Controller:
public function search() {
$search = Input::get('search');
if($search){
$query = DB::table('volunteer');
$results = $query ->where('bloodt', 'LIKE', $search) ->get();
//print_r($results); exit;
return View::make('search')->with("volunteer",$results);
}
}
View:
{{Form::open(array('method'=>'POST'))}}
<div class="pull-left col-xs-6 col-xs-offset-0">
{{Form::text('search','',array('placeholder'=>'Search by blood type/zip code/address....','class'=>'form-control','required autofocus'))}}
</div>
<input type="submit" name="search" value="Search" class="btn btn-primary">
<br><br>
{{Form::close()}}
<?php if (isset($results)) { ?>
#foreach($volunteer as $results)
<table class="table table-bordered" style="width: 100%; background-color: lightsteelblue">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>IC/Passport no.</th>
<th>Blood Type</th>
<th>Address</th>
<th>Zip Code</th>
<th>e-mail</th>
<th>Phone no.</th>
<th>Status</th>
</tr>
<tr>
<td>{{ $results->fname }}</td>
<td>{{ $results->lname }}</td>
<td>{{ $results->ic }}</td>
<td>{{ $results->bloodt }}</td>
<td>{{ $results->address }}</td>
<td>{{ $results->zipcode }}</td>
<td>{{ $results->email }}</td>
<td>{{ $results->phone }}</td>
<td>
<div class="btn-group" role="group">
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
{{ $results->status }}
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>Eligible</li>
<li>Not Eligible</li>
</ul>
</div>
</td>
</tr>
</table>
#endforeach
<?php;
} ?>
Route:
Route::post('search', 'HomeController#search');

Categories