Issue
I have a very simple solution but I dislike that. So, How can I do?
my data base is
--------------------------------------
id name view created_at
--------------------------------------
1 one 9 2021-01-13 12:34:22
2 two 8 2021-01-15 10:23:02
3 three 23 2021-01-15 20:55:17
4 forth 15 2021-01-16 12:34:22
5 fifth 0 2021-01-19 10:37:02
I want to sort and get my data like this--
--------------------------------------
id name view created_at
--------------------------------------
5 fifth 0 2021-01-19 10:37:02
3 three 23 2021-01-15 20:55:17
4 forth 15 2021-01-16 12:34:22
1 one 9 2021-01-13 12:34:22
2 two 8 2021-01-15 10:23:02
My solution is
$today = '2021-01-19'; //this date will calculate in daily. Not absolute date!
$firstarray=Product::where('created_at','LIKE',$today.'%')->get();
$secondarray=Product::orderBy('viewer', 'DESC')->get();
$data = array_merge($firstarray,$secondarray);
return $data;
In real, I want to make my code like this
$today = '2021-01-19'; //this date will calculate in daily. Not absolute date!
$data = Product::orderBy(DB::raw('FIELD(created_at, LIKE $today."%")'),'DESC')
->orderBy('view','desc')->get();
return $data;
How can I get powerful code for my problem?
Sorry for my english skill
thank all
Solution
Your question is not clear! If it's the way I understand, that's the solution.
$data = Product::whereDate('created_at', now())->orderBy('view','desc')->get();
Answered By - Yunus Kocabay Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.