I am trying to do a where clause on withCount method of laravel’s eloquent query builder using this piece of code. and this code is giving me this error. SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘upvotes_count’ in ‘where clause’ (SQL: select , (select count() from upvotes where upvotes.upvoteable_id = posts.id and upvotes.upvoteable_type = AppPost) as upvotes_count from posts where
Tag: laravel
How to convert Laravel migrations to raw SQL scripts?
Developers of my team are really used to the power of Laravel migrations, they are working great on local machines and our dev servers. But customer’s database admin will not accept Laravel migrations. He asks for raw SQL scripts for each new version of our application. Is there any tool or programming technique to capture the output from Laravel migrations
Laravel 5 Eloquent where and or in Clauses
i try to get results from table with multiple where and/or clauses. My SQL statement is: SELECT * FROM tbl WHERE m__Id = 46 AND t_Id = 2 AND (Cab = 2 OR Cab = 4) How i can get this with Laravel …
How to insert multiple rows from a single query using eloquent/fluent
I have the following query: and as expected I get the following result: Is there a way of copying the above result into another table so that my table looks like this? The problem I have is that the $query can expect any number of rows and so im unsure how to iterate through an unknown number of rows. Answer
Laravel – Add custom column in select with Eloquent query buider
this is a simplified use case, only to illustrate what I want to achieve: Considering this query in pure SQL: How can I add the constant active column using query builder ? Here is my Query Builder without the extra column: Answer Simplest would be to use DB::raw
Laravel: getting a single value from a MySQL query
I’m trying get a single value from MySQL database using laravel but the problem I’m getting an array . this is my query result in MySQL command line: my laravel function: expected $data value is a string with value= Admin but what i get is : [{“groupName”:”Admin”}] Answer Edit: Sorry i forgot about pluck() as many have commented : Easiest
Laravel – seeding large SQL file
A memory exhaustion happens when I run my DB seed script in production. Below is my seed script. So what I did was add a no-limit on my seed script. The problem now is that when I run the script it logs the output into the terminal the content of the SQL script (which is very, very big). Is there
two foreign keys, how to map with laravel eloquent
I have two tables in MySQL, where the first one is called users and the second one is called games. The table structure is as follows. users id (primary) email password real_name games id (Primary)…
Counting related rows in a child table
I have been trying to do some queries and getting a count on related tables using eloquent. Tables: requests contact (belongs to requests) history (belongs to contact) As such X number of requests each have Y number of contacts which in term each have Z number of histories Using sql I can do something like this to get all the
Laravel foreign key onDelete(‘cascade’) not working
I have a many-to-many relationship between User & Role, with a role_user table. My migrations are setup as so (simplified): users table: public function up() { Schema::create(‘users’, …