Laravel orderBy not correctly ordering - php

I have a table called users and I have row called ELO_points.
Some users have "-10" ELO_points and some have "25".
Sometimes orderBy not working correctly.
My controller:
$users = User::orderBy('ELO_points', 'DESC')->take(5)->get();
My view:
#foreach ($users as $user)
#if($loop->iteration == 1)
<div class="right-points">{!! $user->ELO_points !!}</div>
</li>
#endif
#if($loop->iteration == 2)
<div class="right-points">{!! $user->ELO_points !!}</div>
</li>
#endif
#endforeach
Any help why my ordering not showing like normal from -10 to 10?

Try it with the following query.
$query = "CAST(ELO_points AS INT) DESC";
$users = User::orderByRaw($query)->take(5)->get();

Related

Error Undefined variable even if the variable is passed

I have the following function in the Controller:
public function index($id)
{
$client_name_tasks = Client::select('clients.name')
->join('projects', 'clients.id', '=', 'projects.client_id')
->join('tasks', 'projects.id', '=', 'tasks.project_id')
->where('tasks.project_id', $id)
->distinct()
->get();
return view('task.index', compact('client_name_tasks'));
}
where $client_name_tasks is collection.
Since the following view is called from different parts of my project, the title h1 is different based on where it is called (are 3 different).
<div class="col-12 col-md-10 d-flex justify-content-center">
#isset($client_name)
<h1>List of overall tasks of {{$client_name}}</h1>
#endisset
#isset($client_name_tasks)
#foreach($client_name_tasks as $client_name_task)
<h1>List of {{$client_name_task->name}} project tasks</h1>
#endforeach
#else
<h1>List all task</h1>
#endisset
</div>
I get error compact(): Error $client_name_tasks.
The problem is in the view, isset.
Can anyone kindly help me?
You can check the count value of that collection.
<div class="col-12 col-md-10 d-flex justify-content-center">
#isset($client_name)
<h1>List of overall tasks of {{$client_name}}</h1>
#endisset
#if(count($client_name_tasks) > 0)
#foreach($client_name_tasks as $client_name_task)
<h1>List of {{$client_name_task->name}} project tasks</h1>
#endforeach
#else
<h1>List all task</h1>
#endif
</div>
why don't you do a foreach in your controller in order to bring putting it in the compact back only the variable in the isset?
foreach ($client_name_tasks as $client_name_task){
$name_client=$client_name_task->name;
}
and in the view write this way (attention that name_client is different from client_name):
#if(isset($name_client))
<h1>List of {{$client_name_task->name}} project tasks</h1>
#elseif(isset($client_name))
<h1>List of overall tasks of {{$client_name}}</h1>
#else
<h1>All task</h1>
#endif

Laravel How to add orderBy on categorized posts

I have a laravel-application where I have a Blog section. The posts are split up into categories so that the user can switch between each categories. So far so good but since I added the categorization and I click the "All"-tab, the orderBy()-method does not work anymore. Instead, the posts appear under their respective category, ignoring the orderBy-method.
Here is my controller:
$blogs = QueryBuilder::for(Blog::class)
->allowedFilters([
AllowedFilter::exact('category', 'blog_category_id'),
])
->orderBy('publishes_on', 'desc')
->where("publishes_on", "<=", Carbon::now())
->where('blog_status_id', '=', '2')
->with('blog_category', 'employee')
->get();
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blogs', 'blog_categories'));
then in my view:
// the tab menu - click events handled with Javascript
<ul>
<li data-tab-id="all" class="tab all active">All</li>
#foreach ($blog_categories as $category)
<li data-tab-id="{{ strtolower($category->name) }}" class="tab {{ strtolower($category->name) }}">{{ $category->name }}</li>
#endforeach
</ul>
// the posts loop
#foreach($blog_categories as $category)
#foreach($category->blog_post as $blog)
<div class="col-md-4 post {{ strtolower($category->name) }}">
<p>{!! $blog->text !!}</p>
</div>
#endforeach
#endforeach
so, how can I achieve that the orderBy works again as intented?
thanks in advance!

How to get number and sum of values of specific id (average) inside blade?

Template:
<div class="item-wrapper" style="text-align:justify; top: 100px;">
<legend>Reviews</legend>
#if (count($reviews)>0)
#forelse($reviews as $review)
#if( $review->counselor_id == $cprofile->id)
{{$review->review}}
{{$review->rating}}
<star-rating :rating="{{$review->rating}}">
</star-rating>
#endif #empty
#endforelse
#endif
</div>
Controller:
$relations = [
'viewCprofile' => counselor::all(),
'reviews' => DB::table('counselor_reviews')
->select('*')
->get()
];
return view('viewCprofile', $relations);
Route:
Route::get('/viewCprofile', 'ProfilesController#getCprofiles');
Should I pass id to controller to get data?
you can use the following and complete your code
#if (count($reviews)>0)
#php $total_ratings = 0 #endphp
#forelse($reviews as $review)
#if( $review->counselor_id == $cprofile->id)
#php $total_ratings += $review->rating #endphp
{{$review->review}} {{$review->rating}}
<star-rating :rating="{{$review->rating}}"> </star-rating>
{{$total_ratings}}
#endif

Object of class Illuminate\Database\Eloquent\Collection could not be converted to int Laravel 5.4

I have an old database filled with info and now i want to display Category names from that DB and getting this error.
Here is my controller
public function forums(){
$cats = Forum_cats::all();
return view ('lapas.forums.index')->with('cats', $cats);
}
}
here is my view
#if(count($cats >1))
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
and here is screen of DB structure
http://prntscr.com/mg5nk1
Ask for more info if needed!
It looks like your operator is misplaced:
#if(count($cats) > 1)
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
It looks like you're trying to loop through these $cats in a Blade template. You might also try forelse:
#forelse($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#empty
{{-- Action if there are none --}}
#endforelse
Edit: Docs here: https://laravel.com/docs/5.4/blade#loops
Your if is wrong. Try :
#if($cats->count() > 1)

Laravel 5 after 3 posts show an add

Hello guys i have declared my posts with variable $lastItems
#if($lastItems->total() > 0)
#foreach($lastItems as $item)
#include('._posting.items')
#endforeach
#else
#include('errors.emptycontent')
#endif
and i want to add ads after each 3 post, i use something like this
#if($lastItems->total() > 0)
#foreach($lastItems as $item)
#include('._posting.items')
#endforeach
#if($item = 3)
<img src="link" alt="ads">
#endif
#else
#include('errors.emptycontent')
#endif
but the problem is, when i try like this it shows the ads after every post not after 3 posts.
What i'm doing wrong?
You may try something like:
#if($lastItems->total() > 0)
#foreach($lastItems as $key => $item)
#include('._posting.items')
#if (($key + 1) % 3 == 0)
<img src="link" alt="ads">
#endif
#endforeach
#else
#include('errors.emptycontent')
#endif
I had almost same problem, in my case I had to change div's class. So I used sth like this and it helped me:
<?php $var = -1; ?>
#foreach($lastItems as $item)
<?php $var ++;?>
#if($var%3 == 0 && $var!=0)
#include('errors.emptycontent')
else
#include('._posting.items')
#endif
#endforeach
I hope it helps :)

Categories