Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Magento Development 08: Filter and Sorting

    We have displayed the blog list but it will show all the blogs. Even if we create a new customer s/he will be able to see the blogs from the previous customer. So let’s fix this.


    If you think in terms of sql query then you can use WHERE user_id=$customerId but in magento we do not write raw sql queries. To apply conditions on the collection we have to use addFieldToFilter method. Let’s check out the code for better understanding, we have to edit the Block/BlogList.php because we are sending the data from here only,

    Here we have used the customer session to get the customer id. Now please take note of ->addFieldToFilter(‘user_id’, [‘eq’=>$customerId]); in the first param we have passed the column name. And in the second param we have passed an associative array, whose key represents the operators. eq means equal operator. Some other important operators are,
    [“eq” => $equalValue]
    [“neq” => $notEqualValue]
    [“like” => $likeValue]
    [“in” => [$inValues]]
    [“nin” => [$notInValues]]
    [“notnull” => $valueIsNotNull]
    [“null” => $valueIsNull]
    [“gt” => $greaterValue]
    [“lt” => $lessValue]
    [“gteq” => $greaterOrEqualValue]
    [“lteq” => $lessOrEqualValue]
    [“from” => $fromValue, “to” => $toValue]

    If we do not provide any operator then it just use the equal operator, that means here we can use ->addFieldToFilter(‘user_id’, $customerId) also.

    To combine multiple conditions with AND operator we have to use multiple addFieldToFilter methods. So for example, WHERE (user_id = ‘1’) AND (status != 0) can be achieved with following code,


    It will be better from usability standpoint if we show the latest blogs at the top. In sql we use ORDER BY to sort. Let’s see how can we sort in Magento, we have to edit the Block/BlogList.php

    To sort we use setOrder method where in first param we have pass the column name and in the second we have to mention whether we want sort in ascending or descending order. Here we are updating based on updated_at column so latest updated blogs will come at the top.

    Here you can see that the latest blog is coming at top. Also we are seeing only those blogs which have user_id same as the current customer id.

    PS. We can view the actual SELECT sql query for the collection by printing the $collection->getSelect();

    We have not created any files so the folder structure will remain same.

    Next Blog -> Magento Development 09: Editing and Updating

    Previous Blog -> Magento Development 07: Collection and Block

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again