Questions tagged [eloquent]

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

The Eloquent included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Resources

26966 questions
183
votes
18 answers

Laravel Eloquent groupBy() AND also return count of each group

I have a table that contains, amongst other columns, a column of browser versions. And I simply want to know from the record-set, how many of each type of browser there are. So, I need to end up with something like this: Total Records: 10; Internet…
kJamesy
  • 5,973
  • 5
  • 20
  • 22
181
votes
7 answers

Eloquent - where not equal to

I'm currently using the latest Laravel version. I've tried the following queries: Code::where('to_be_used_by_user_id', '<>' , 2)->get() Code::whereNotIn('to_be_used_by_user_id', [2])->get() Code::where('to_be_used_by_user_id', 'NOT IN',…
aBhijit
  • 5,261
  • 10
  • 36
  • 56
181
votes
12 answers

How to do this in Laravel, subquery where in

How can I make this query in Laravel: SELECT `p`.`id`, `p`.`name`, `p`.`img`, `p`.`safe_name`, `p`.`sku`, `p`.`productstatusid` FROM `products` p WHERE `p`.`id` IN ( SELECT `product_id` FROM…
Marc Buurke
  • 1,917
  • 2
  • 13
  • 14
168
votes
8 answers

Laravel Eloquent Sum of relation's column

I've been working on a shopping cart application and now I've come to the following issue.. There is a User, a Product and a Cart object. The Cart table only contains the following columns: id, user_id, product_id and timestamps. The UserModel…
Admiral
  • 1,878
  • 2
  • 12
  • 17
158
votes
8 answers

Laravel where on relationship object

I'm developing a web API with Laravel 5.0 but I'm not sure about a specific query I'm trying to build. My classes are as follows: class Event extends Model { protected $table = 'events'; public $timestamps = false; public function…
Lic
  • 1,757
  • 3
  • 14
  • 16
158
votes
14 answers

Laravel Pagination links not including other GET parameters

I am using Eloquent together with Laravel 4's Pagination class. Problem: When there are some GET parameters in the URL, eg: http://site.example/users?gender=female&body=hot, the pagination links produced only contain the page parameter and nothing…
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
155
votes
6 answers

How to Make Laravel Eloquent "IN" Query?

I want to make query in Laravel Eloquent like here its raw MySQL query SELECT * from exampleTbl where id in(1,2,3,4) I have tried this in Laravel Eloquent but it's not working DB::where("id IN(23,25)")->get()
Ravi Jethva
  • 1,931
  • 2
  • 11
  • 12
152
votes
2 answers

Retrieve Laravel Model results based on multiple ID's

I have implemented ZendSearch into my Laravel application. I am using it as my search engine where users will type a search word, and then ZendSearch will return me an array of results ordered by relevance. However, the array that ZendSearch…
justinl
  • 10,448
  • 21
  • 70
  • 88
149
votes
15 answers

How to get distinct values for non-key column fields in Laravel?

This might be quite easy but have no idea how to. I have a table that can have repeated values for a particular non-key column field. How do I write a SQL query using Query Builder or Eloquent that will fetch rows with distinct values for that…
gthuo
  • 2,376
  • 5
  • 23
  • 30
147
votes
8 answers

Property [title] does not exist on this collection instance

I am following Laracasts' videos: Basic Model/Controller/View Workflow. I have a table holds contact information. CREATE TABLE `about` ( `id` int(10) UNSIGNED NOT NULL, `title` varchar(500) COLLATE utf8_unicode_ci NOT NULL, `content` text COLLATE…
zkanoca
  • 9,664
  • 9
  • 50
  • 94
146
votes
12 answers

How to select from subquery using Laravel Query Builder?

I'd like to get value by the following SQL using Eloquent ORM. - SQL SELECT COUNT(*) FROM (SELECT * FROM abc GROUP BY col1) AS a; Then I considered the following. - Code $sql = Abc::from('abc AS a')->groupBy('col1')->toSql(); $num =…
quenty658
  • 1,587
  • 2
  • 10
  • 7
145
votes
4 answers

Is there a way to "limit" the result with ELOQUENT ORM of Laravel?

Is there a way to "limit" the result with ELOQUENT ORM of Laravel? SELECT * FROM `games` LIMIT 30 , 30 And with Eloquent ?
Natan Shalva
  • 1,572
  • 2
  • 11
  • 11
140
votes
5 answers

Eloquent get only one column as an array

How to get only one column as one dimentional array in laravel 5.2 using eloquent? I have tried: $array = Word_relation::select('word_two')->where('word_one', $word_id)->get()->toArray(); but this one gives it as 2 dimentional array like: array(2)…
Riiwo
  • 1,839
  • 3
  • 15
  • 18
136
votes
1 answer

Laravel. Use scope() in models with relation

I have two related models: Category and Post. The Post model has a published scope (method scopePublished()). When I try to get all categories with that scope: $categories = Category::with('posts')->published()->get(); I get an error: Call to…
Ilya Vo
  • 2,239
  • 3
  • 18
  • 31
134
votes
9 answers

How to Merge Two Eloquent Collections?

I have a questions table and a tags table. I want to fetch all questions from tags of a given question. So, for example, I may have the tags "Travel," "Trains" and "Culture" attached to a given question. I want to be able to fetch all questions for…
Martyn
  • 6,031
  • 12
  • 55
  • 121