How to sole htmlentities() error in Laravel - laravel-5

I'm trying to display sum of some columns in laravel using query builder but getting this error:
htmlentities() expects parameter 1 to be string, object given
This is the query:
$purchase = DB::table('purchases')
->join('currencies', 'currencies.cur_id', '=', 'purchases.currency_id')
->selectRaw('purchases.*, currencies.currency ,SUM(purchases.quantity*unit_price) as total, SUM(purchases.c_price*quantity) as usd_total')
->first();
And view:
<div class="col-xs-8">
<p class="text-elg text-strong mb-0">
{{ number_format($purchase->usd_total,2) }}
</p>
<span>Purchases</span>
</div>

dd($purchase->usd_total) an you will find the reason.

Related

How to check date validation with Carbon in Laravel 8.*

I've got index page on which I'm displaying events.
#foreach($events as $event)
<div class="row">
<div class="d-flex justify-content-center" style="margin: auto">
<a href="{!! route('eventView', ['id' => $event->id]) !!}" style="text-decoration: none;">
<div class="row" style="margin-top: 10 px">
<p style="display:block">{{$event->name}}</p>
</div>
#if($event->photo_patch)
<img src="{{ asset('storage/' . $event->photo_patch) }}">
#else
<img src="placeholder" alt="...">
#endif
</a>
</div>
</div>
</div>
#endforeach
But the problem is that its displaying all events, even those which took place in the past.
I would like to display events only with today's or future date
So I'm stuck now and I dont know what to do next.
I tried to do loop which shows me events that are today/in the future:
public function index()
{
$date = Carbon::today();
$events = Event::where('date_of_event' >= $date)->get();
foreach($events as $event){
if($event->date_of_event >= $date){
dump('Valid');
}
else{
dump('Invalid');
}
}
dump($events);
dump($date);
return view('frontend/index',['events'=>$events]);
}
But I dont know how to display them later inside of my view.
My second shot was to do it with this line:
$events = Event::where('date_of_event' >= $date)->get();
But then I've got error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from `events` where `1` is null)
which isnt really accurate since column name should be valid.
How can I implement that properly?
The default operator for where is =. If you want to change this you'll use the second argument for the operator and the third for your value.
Event::where('date_of_event', '>=', $date)->get();
See Where Clauses for more details.
To make that code short you may use this code
Event::whereDateOfEvent('>=', $date)->get();

Undefined index: id Laravel 5.8

My Tables:
kategoris table
id | kode_kategori | kategori_name |
items table
id | kategori_id | item_name
In items table the kategori_id column has foreignkey.
My Controller:
public function edit($id)
{
// $item = Item::findOrFail($id);
$item = DB::table('items')
->join('kategoris', 'items.kategori_id', '=', 'kategoris.id')
->where('items.id', '=', $id)
->select('items.*', 'kategoris.*', 'items.id', 'items.kategori_id')
->get();
// dd($item);
return view('master-dev/item/edit', compact('item'));
}
My View:
<div class="card card-default">
{{ Form::model($item,['route'=>['item.update',$item['id']], 'files'=>true,'method'=>'PUT']) }}
<div class="card-header">
<h3 class="card-title"><b>Edit Data Item</b></h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
#if(!empty($errors->all()))
<div class="alert alert-danger">
{{ Html::ul($errors->all())}}
</div>
#endif
<div class="row">
<div class="col-md-6">
<div class="form-group">
{{ Form::label('kode_kategori', 'Kode Kategori') }}
<select name="kode_kategori" id="kode_kategori" class="form-control">
#foreach ($item as $i)
<option valu="{{ $i['kode_kategori'] }}">{{ $i['kode_kategori'] }}</option>
#endforeach
</select>
</div>
</div>
..........
..........
{{ Form::close() }}
I've tried any solutions in stackoverflow such as adding (ifempty...) and other solution but still the result Undefined index: id in my edit blade. When I was trying using dd and vardump the results was shown. I need to loop the foreach in my dropdown menu to show the list of data from my categories table. And I need to join my items table and my categories table to get the name of the categories.
you are calling same id from items and kategoris try this
public function edit($id)
{
// $item = Item::findOrFail($id);
$item = DB::table('items')
->join('kategoris', 'items.kategori_id', '=', 'kategoris.id')
->where('items.id', '=', $id)
->select('items.*', 'kategoris.id as kategory_id', 'kategoris.kode_kategori', 'kategoris.kategori_name')
->get();
// dd($item);
return view('master-dev/item/edit', compact('item'));
}
if this answer doesnot work show your database relation i will give you solution
$item = ....->get() will return a Collection to only have one item you need to use $item = ....->first() instead
But since you have #foreach ($item as $i) I believe, you still want to have a collection, but in that case, your issue is here
{{ Form::model($item,['route'=>['item.update',$item['id']], 'files'=>true,'method'=>'PUT']) }}
Since you have a collection, we don't know what $item['id'] it's referring to. Perhaps $item->first()['id'] ?
I solved the problem, there's a conflict fetching data from items and kategoris tables. There are differences calling a value with array and object, mostly if its data looped. So in the controller I must declared one by one, the selected value id from kategoris table, and I have to join both tables to get the name of the kategoris, and then I have to declare once more to get the list of the kategoris data. So there are three (3) variables to declare each one of them. For this long time I was looking for the short code in my Controller but I cannot find it.
Thank you for all of you guys helping me this problem. Cheers.

Errors using alaouy/Youtube package with Laravel 5.4 for fetching videos from Youtube

I am using alaouy/Youtube package on Laravel 5.4. It's fetching videos from Youtube but I am getting errors.
// view
#extends('welcome')
#section('content')
<ul>
#foreach($videos as $data)
<div class="well">
{{ $data->id->videoId}}
</div>
#endforeach
</ul>
#endsection
// controller
$videos = Youtube::search($search);
return view('search',compact('videos'));
I am able to get access to all the data in the object except {{$data->id->videoId}}
// Error
Undefined property: stdClass::$videoId (View:
C:\Users\derrick\testyoutubeapi\resources\views\search.blade.php)
You are not receiving the required data so you are getting the error message of not defined property.
in other words, you are requesting videoId field which is not there for some of kinds like playlist (playlistId), or maybe a channel (channelId)
so your problem solution should be just making your code in the view to check if there is a field that is called videoId or no before the query:
#foreach($videos as $data)
#if(isset($data->id->videoId)) // field available?
<div class="well">
{{ $data->id->videoId}}
#endif // end if
</div>
#endforeach

Pass two variable in one array and show it on view page in Laravel

I am trying to merge two arrays into a single one and return it (the merged array) into a view in order to format and display it.
Below is the method in my controller intended for that purpose;
public function showJobCategoryContent($id)
{
$jobsInfo = Job::where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
$userInfo = Employee::all();
$array = array_merge($jobsInfo->toArray(), $userInfo->toArray());
return view('front.category-content.job-category-content', [
'jobsInfosById'=> $array
]);
}
Here, the content of my view;
#forelse($jobsInfosById as $jobInfoById)
<li>
<div class="well">
<h4>{{ $jobInfoById['company_name'] }}</h4>
<h4>{{ $jobInfoById['full_name'] }}</h4>
</div>
</li>
#endforelse
I get the following error:
Undefined index: company_name
What am I doing wrong and how can I resolve it?
use
$jobInfoById->company_name
instead of
$jobInfoById['company_name']

Retrieve unique entries from DB using Eloquent Laravel 4

I am using Laravel-4 to build an application. I am trying to make the best use of Eloquent ORM. Currently I am displaying a list of db entries which all have a number of tags associated with them
To the right I display a grid of all the tags however it is showing duplicate tags. I would only like to show unique tags
This is my code:
<div class="tags-panel">
<div class="tags-title"><h1>Tags</h1></div>
<div class="tags-cloud">
#foreach(Tag::distinct()->get() as $tag)
<span class="num"> {{ Tag::getAmount($tag->id) }} </span> {{$tag->name}}
#endforeach
</div>
</div>
Does Eloquent provide a way of only retrieving distinct entries from the db?
Try using Tag::groupBy('field_name')->get()
<div class="tags-panel">
<div class="tags-title"><h1>Tags</h1></div>
<div class="tags-cloud">
#foreach(Tag::groupBy('field_name')->get() as $tag)
<span class="num"> {{ Tag::getAmount($tag->id) }} </span> {{$tag->name}}
#endforeach
</div>
</div>
To use the distinct()-function you have either have a select() in your chain, or use a field as an argument in your get()-function. For example:
$tags = Tag::distinct()->get(array('field'));
or
$tags = Tag::select('field')->distinct()->get();
You could also instead use groupBy('field') if you don't want to supply field names:
$tags = Tag::groupBy('field')->get();

Resources