in my Laravel app I have 3 tables : users, documents and type_documents, the user have multiple documents and document have one type_document I want select the types that are not used in documents table for the current user with eloquent I try with this, but it give me the used type_documents : I use Laravel version 8 Answer Have
Tag: eloquent
How to use CASE WHEN IN in Laravel database query builder
I have users table and a followers table. I’m doing a search to get all the users with a specific keyword, but I want the ones which are followers of the current user to appear first. I can get all the followers id’s in a subquery, so my idea was to do something like this: I’ve been trying to get
Laravel onDelete(‘cascade’) does not work
My Tables: I am getting following errors when I try to delete support_categories Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails Answer You just write foreign key in wrong order. Instead of: Write: or you can use casadeOnDelete():
Many-to-many relation Laravel Eloquent. Fetch related table with specific related id
I am writing a query in Laravel 8 using Eloquent. There is a many-to-many relation between subjects and streams and their pivot table is courses. subjects: id name streams id name courses id subject_id stream_id I have to fetch subjects with specific stream. I have written the following query. $this->subjects = Subject::has(‘streams’,$this->stream)->get(); I am having problem in this. courses table
Select from select using Laravel eloquent
I want to get all user’s favourite products. The query should be like this: How can I query with eloquent? Answer You’re trying to achieve something like this: In the oficial documentation you can find the explanation of Advanced where clauses The final result will be (you can see this dumping the query using toSql() method instead of get()) :
SQL query for last 365 days report
I have a reports table with the following structure : I want a SQL Query to get the report for the last 365 days by following conditions : Group dates if the same date is repeated. The days which the report is not available for the last 365 days, I need those days added to the result rows with 0
Laravel many to many relastionship count
I have two tables. User and days, with many to many relationship. user_days : user_id, day_id I want to get the count number of users in day_id = 1 {for a particular day} or something like this. Any way to get count from pivot table? This function didnot work. the table structure ? How do I find the user count
How to order by multiple columns not creating different groups using Laravel eloquent
I have a table which looks like this: Name AltName Sam null Ann null null Mark John null null Bart Sasha null I want to order by both Name and AltName. If I do this: This will return the data in this order: Ann, John, Sam, Sasha, Bart, Mark I don’t want it to group the data but instead merge
Laravel 8 Livewire search filter not working with first where clause
I want to have a table which shows all the data per company that has a filter search input, the problem is when I put the first argument where(‘company_id’, Auth::user()->company_id) it bugs out and doesn’t make the filter search work. This is my query for the table. Answer I suppose you want company_id filter to apply to all query and
Delete unused groups in Laravel 8
I have a group model and I want to delete groups that don’t have any member. How can I get empty groups with eloquent or SQL query ? And this is the User model code: Answer I think whereDoesntHave work in you situation.