logo
down
shadow

PHP Laravel orderby not working if using `count`


PHP Laravel orderby not working if using `count`

By : Elango
Date : November 21 2020, 04:01 AM
it fixes the issue I want to sort/order my data by its release_date but I get an error I don't understand: , Try this code:
code :
public function index() 
{
    $releases = Release::orderBy('release_date', 'desc')->get();

    return view('pages.index')->with('releases', $releases);
}


Share : facebook icon twitter icon
Laravel OrderBy relationship count

Laravel OrderBy relationship count


By : ms2015
Date : March 29 2020, 07:55 AM
hop of those help? Edit: If using Laravel 5.2 or greater, use kJamesy's answer. It will likely perform a bit better because it's not going to need to load up all the participants and hackathons into memory, just the paginated hackathons and the count of participants for those hackathons.
You should be able to use the Collection's sortBy() and count() methods to do this fairly easily.
code :
$hackathons = Hackathon::with('participants')->get()->sortBy(function($hackathon)
{
    return $hackathon->participants->count();
});
Laravel MySQL orderBy count

Laravel MySQL orderBy count


By : Ryan
Date : March 29 2020, 07:55 AM
this one helps. You can do that as you showed but now you get all entries from database. If you will have 100 posts each with 100 comments, you will get 10000 rows from your database just to sort your posts (I assume you don't want to display those comments when sorting).
You could add to your Post model:
code :
public function commentsCountRelation()
{
    return $this->hasOne('Comment')->selectRaw('post_id, count(*) as count')
        ->groupBy('post_id');
}

public function getCommentsCountAttribute()
{

    return $this->commentsCountRelation ?
        $this->commentsCountRelation->count : 0;
}
$posts = Post::with('commentsCount')->get()->sortBy(function($post) {
    return $post->comments_count;
});
$posts = Post::with('commentsCount')->get()->sortBy(function($post) {
    return $post->comments_count;
}, SORT_REGULAR, true);
Laravel OrderBy Relationship count with pagination

Laravel OrderBy Relationship count with pagination


By : Alexander Chavarria
Date : March 29 2020, 07:55 AM
it helps some times I have many projects and each has many orders, some completed, some not. , Finally found a good solution for the problem:
code :
$products = $products->join('orders', function ($join) {
                        $join->on('orders.product_id', '=', 'products.id')
                            ->where('orders.status', '=', 2);
                    })
                    ->groupBy('products.id')
                    ->orderBy('count', $order)
                    ->select((['products.*', DB::raw('COUNT(orders.product_id) as count')]))->paginate(50);
Laravel: orderBy like count

Laravel: orderBy like count


By : user326839
Date : March 29 2020, 07:55 AM
I hope this helps . Right so on my website, registered users can create posts and like other peoples posts. When displaying all the posts i have options where the users can sort by title, date and like count. I have done the title and date but im having trouble with sorting by like count. What would be the best way to implement this feature into my system. , Use this:
code :
$posts = Post::withCount('likes')->orderByDesc('likes_count')->get();
public function likes() {
    return $this->hasMany(Like::class);
}
In Laravel, how to combine where() and orderBy() when orderBy() is working on relationship?

In Laravel, how to combine where() and orderBy() when orderBy() is working on relationship?


By : Cratos
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have two Models, a Track model and an Artist model, where each Track model belongsTo() one Artist model. Track models have a number column and Artist models have a name column. I want to retrieve all Tracks where number = 1 and then display those sorted by their Artist's name. So far I have only figured out how to do one or the other, but not both. , Use something like this:
code :
Track::with('artist')->join('artist', 'artist.id', '=', 'track.artist_id')->orderBy('artist.name', 'DESC')->get()
Related Posts Related Posts :
  • Search Value in Laravel Collection
  • PHP preg_split adds a blank array key that can't be cleared by array_filter because there's a 'space' in it
  • change the link color if a post has been read wordpress
  • How do you customize variables in Laravel default emails?
  • Extract content of specific div class php dom
  • Showing results of a database query inside Navigation Tab
  • Return many object in my json response using resource
  • How to upload an image to Azure storage through PHP?
  • PHP - DOMDocument scrap divs dont remove images
  • Adding to functions.php in child theme doesn't work
  • Regular Expression to match markdown and regular href sources from specific domain(s)
  • How to manipulate and collate data in an associate array
  • How do I check an array for an entry that contains HTML characters?
  • Select current ( week or any given week ) records excluding week ends and holidays
  • How do I find the shipping class id in WooCommerce?
  • Make a select field disabled with a preselected value in Woocommerce
  • How to use the querybuilder in doctrine to test whether or not atleast one of two given values is already set in the dat
  • Laravel File Privacy
  • PHP read TXT file from a special character
  • Select first 10 rows and display into a slideshow
  • PHPMailer autoload.php not able to open
  • PHP - Create GUID from ID
  • PHP variable causing my SQL query to fail, but hard-coding the same values into the query works Why is this?.
  • generate a random password and update the database
  • DELETE to /oauth/personal-access-tokens/token not supported
  • Forget not modifying the original collection Laravel 5.7
  • Print a sequence of letters
  • Is there any alternative for asset and URL in Laravel?
  • PHP - how to link other pages in view in ci
  • Cannot Access Laravel Storage Directory
  • Find and replace value in system.cfg php
  • Laravel : How to retrieve data from inside an nested array
  • Codeigniter search with comma-separated values
  • upload file on laravel 5.7
  • Wordpress - How to display 10 users only?
  • Display the results in the HTML
  • PHP - mysqli_fetch_assoc, 2 results then into an array
  • mysqli update database receiving email and password reset token
  • PHP mailer not creating new lines
  • IF / OR statement causes Call to undefined function() error. Why?
  • How to read Protocol Buffers delimited I/O functions
  • Creating a link from Google Chart timeline item
  • Rewrite PHP variables from URL with .htaccess
  • Undesired output for php array creation
  • Symfony 4 - custom authentication FosUserBundle
  • laravel: cant push object property to array
  • When sharing on facebook, showing angular brackets in title and decription
  • Undefined function: 7 ERROR: operator does not exist: public.ltree public.< public.ltree[]
  • Generate a CSV in php from Array or Object
  • LibreOffice convert PDF to Word as textbox instead normal document
  • php laravel swift email sending problems
  • Reload PHP file in command line PHP-CLI
  • how do I fetch a certain part out of an database?
  • CodeIgniter URL Route with ID and title
  • Add a header to each column
  • How can I loop stdClass in PHP?
  • Four arrays in for each loop
  • bootstrap 4 cards with php foreach loop
  • Codeigniter return not found on the server
  • PHP Codeigniter Do Upload Function Does Not Work
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org