problems with sequence code in controller - laravel

The $trendtag code alone works. However it doesnt get posts if I want to load the posts.
If I run dd($tagIdArray), I get the id's of the tags. But the following code has no function. If I do not use $trendtags, or only like this, it works:
$trendtags = Taggable::take(3)
->with('tag')
->get();
But how can I find the most used tags in the last 48 Hours?
Is there another way?
$trendtags = Taggable::whereDate('created_at', '>=', now()->subHours(48))
->groupBy('tag_id')
->orderByRaw('count(tag_id) DESC')
->take(3)
->with('tag')
->get();
$tagIdArray = $trendtags->pluck('id')->all();
$article = Article::with('comments', 'tags')
->whereIn('privacy', [1, 2])
->where('status', 1)
->whereHas('tags', function($query) use ($tagIdArray) {
$query->whereIn('id', $tagIdArray);
}, '>=', count($tagIdArray))
->latest()
->paginate(15);

Related

Getting trouble in writing query laravel

Can anyone help me to get the data .
$ticketList = Ticket::with('appliance', 'brand')->get();
$userAppliances = DB::table('user_appliances')
->where('user_id', 5)
->pluck('appliance_id')
->toArray();
$userBrands = DB::table('user_brands')
->where('user_id', 5)
->pluck('brand_id')
->toArray();
$ticketList = $ticketList->map(function ($ticket) use ($userAppliances) {
return $ticket->where($ticket->appliance_id,'=', $userAppliances);
});
dd($ticketList);
It returnd the collection. I want all the tickets, where ticket->brand_id and tickt->appliance_id includes {all the id i am getting in array from userBrands and userAppliances}. I am getting trouble in writing query
Try it out this
$userBrands = DB::table ('user_brands')
->where('user_id', 5)
->pluck('appliance_id')
->toArray();
$ticketList = Ticket::with('appliance', 'brand')
->where(function($query) use ($userBrands){
$query->whereIn('appliance_id', $userBrands);
})->get();

How create a subquery with eloquent and laravel with softdeletes

I am using Laravel 7 and Vue.js 2.
I want to retrieve the tasks that are not assigned to a specific user.
I made the following code that works correctly.
$tasks_user = TaskUser::select('task_id')
->where('user_id', $id)
->get();
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', $tasks_user)
->distinct()
->get();
By the way to be more elegant I decided to transform the above code into a single query as follows:
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', function($q) use ($id)
{
$q->select('task_id')
->from('task_user')
->where('user_id', $id)
->get();
})
->distinct()
->get();
Unfortunately I discovered that the above query didn't work because it doesn't considers softdeletes.
For example, if the user with id 3 was related with the task 7 but now that row has been deleted with softdeletes in the table task_user, the first code returns also the task with id 7 (correctly) and the second one not (uncorrectly).
So finally, I must do a single query that works as the first code.
Can help?
You can actually combine both approaches. whereNotIn accepts also an Eloquent Query, it doesnt need to be a callback. Try this:
$userRelatedTasksQuery = TaskUser::select('task_id')
->where('user_id', $id);
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', $userRelatedTasksQuery)
->distinct()
->get();
Be sure to not use get() at the end of the $userReleatedTasksQuery, as you want the eloquent query instance, not the result.

Add Parameter to Laravel Eloquent relationship in "with" function

I would like to add a parameter to my Eloquent relationship in the with:: static method.
I have a Dealer Model that has those relationships
public function events()
{
return $this->hasMany('App\Event', 'dealer_id', 'dealer_id');
}
public function recentEvents($interval = 14)
{
return $this->events()
->whereDate('datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('dateend', '>=', Carbon::now('America/Toronto'));
}
Everything works fine if I call it like this $dealer->recentEvents(20) but in my DealerController
I would like to do something in the line of this
Dealer::with("recentEvents")
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();
Where i would pass a paremeter to "recentEvents".
I've tried putting a closure function to with like this
Dealer::with(["recentEvents" => function ($query) use ($interval){
//The problem here is that I would need to put the recent-events code like this
$query->whereDate('cal_datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('cal_dateend', '>=', Carbon::now('America/Toronto'));
}])
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();
But it would defeat the purpose of having a recentEvents relationship.
Is it possible to just pass a parameter ?
I've tried using Dynamic Scope from Laravel doc but I figured it was not what I was looking for.
i think u should use like this
Dealer::with(["events",function($q) use($interval){
$q->whereDate('datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('dateend', '>=', Carbon::now('America/Toronto'));
}])
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();

How can I do the job of three foreach loop by one foreach loop?

actually I am new to Laravel. I am doing a project for making a meal management system.
my code in my controller is:
$days=DB::table('days')
->get();
foreach ($days as $day) {
$breakfast[]= DB::table('breakfast_menus')
->leftJoin('breakfast_items', 'breakfast_menus.item_id', '=', 'breakfast_items.id')
->where('breakfast_menus.day_id',$day->id)
->select('breakfast_items.item')
->get();
$lunch[]= DB::table('lunch_menus')
->leftJoin('lunch_items', 'lunch_menus.item_id', '=', 'lunch_items.id')
->where('lunch_menus.day_id',$day->id)
->select('lunch_items.item')
->get();
$dinner[]= DB::table('dinner_menus')
->leftJoin('dinner_items', 'dinner_menus.item_id', '=', 'dinner_items.id')
->where('dinner_menus.day_id',$day->id)
->select('dinner_items.item')
->get();
}
return view('admin.show_menu',compact('days','breakfast','lunch','dinner'));
I want to show the data in a table. Where there will be
<td>day</td>
<td>Breakfast </td>
<td>Lunch</td>
<td>Dinner</td>
You should really use Laravel relationships and do justice with the framework.
Create models for each table.
Define relationship like days has breakfast_manu. See this
Then you will be able to do it like this:
$days = Days::with(['breakfast_menus','lunch_items','dinner_items'])->get();
I can say the way you wrote are very very bad for performance, why don't you use another way round in sql?
$days = DB::table('days')
->leftJoin('breakfast_menus', function($q) {
$q->leftJoin('breakfast_items', 'breakfast_menus.item_id', '=', 'breakfast_items.id')
->where('breakfast_menus.day_id', $day->id);
})
->leftJoin('lunch_menus', function($q) {
$q->leftJoin('lunch_items', 'lunch_menus.item_id', '=', 'lunch_items.id')
->where('lunch_menus.day_id', $day->id);
})
->leftJoin('dinner_menus', function($q) {
$q->leftJoin('dinner_items', 'dinner_menus.item_id', '=', 'dinner_items.id')
->where('dinner_menus.day_id', $day->id);
})
->select('days.id', 'breakfast_items.item as breakfast', 'lunch_items.item as lunch', 'dinner_items.item as dinner')
->get();
I didn't try out the SQL, but you can get the idea.
Then you can foreach $days to show your data

Laravel break up paginated eloquent query

I currently have:
$comments = $this->post->comments()
->where('comment', 'like', "%{$search}%")
->paginate(50);
But I'd like to do something like the below, as I have multiple filters that I'm going to apply:
$comments = $this->post->comments();
if(condition)
{
$comments->where('comment', 'like', "%{$search}%");
}
$comments->paginate();
But the code above doesn't work as I then get errors on things like $comments->links() for pagination links in the view, indicating that this doesn't work.
What you can try is something like this, I don't know what Laravel version you are using, also I don't know the structure you have of your models, but here's the documentation about this example:
$conditions = 1;
$comments = Post::with('comments')
->whereHas('comments', function ($query) use ($conditions){
// pass your conditions
if(!$conditions){
$query->where('comment', 'like', "%%")
}
// more conditions, etc
$query->where('comment_id', 'like', '%%');
})->paginate(50);
It was missing $comment = in front of the query clauses:
$comments = $this->post->comments();
if(condition)
{
$comments = $comments->where('comment', 'like', "%{$search}%");
}
$comments = $comments->paginate();

Resources