I have a piece of code like this:
$products = Product::all()
if ($search_value) {
$products = $products->where(\'name\', \'LIKE\', \"%$search_value%\
If you want to get the list of all data and grab it in descending order try this:
$post = Post::orderBy('id', 'DESC')->get();
Your query is wrong.
remove all from $products = Product::all()
and then put get()
at the end of your query.
use sortByDesc('id') or simple sortBy() inside use the variable through which you wanna sort like i add id
You're trying to use orderBy()
method on Eloquent collection. Try to use sortByDesc() instead.
Alternatively, you could change $products = Product::all();
to $products = new Product();
. Then all your code will work as you expect.
$table_Data = DB::table('tbl_product')->orderBy('id','DESC');
You can use this...
You are first getting all()
data and then trying to sort which is wrong. You have to fix this by removing
$products = Product::all()
and changing your code into something like this
if ($search_value) {
$products = Product::where('name', 'LIKE', "%$search_value%");
}
else {
$products = Product::orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
}
Hope you get idea to tweak your code.