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
5
votes
1 answer

Laravel updateOrCreate general error field post_id doesn't exist

I have a views table in the database and i'm trying to update a record in it ( or create it if it doesn't exist yet) with this code: $ar = ['number_of_views' => $number_of_views['number_of_views']+1, 'post_id' =>…
DomeWTF
  • 2,342
  • 4
  • 33
  • 46
5
votes
4 answers

How to add two columns value and perform where condition in laravel eloquent

This is my table: id remaining_amount additional_amount 1 200 0 2 100 -100 3 300 100 4 200 -50 I'm trying to get the rows…
Thatoe Aung
  • 167
  • 4
  • 12
5
votes
2 answers

Laravel Pagination Latest Rows (Reversed)

Can't find an example anywhere on how to return latest rows (per the created_at column) within pagination? My current code is: $messages = Message::where('to_id', $user->id) ->where('is_read', false) ->paginate(10); I want to sort the…
TJH
  • 766
  • 2
  • 9
  • 21
5
votes
1 answer

Using with and withCount together in laravel eloquent

withCount is a new method of laravel to return count of the relations. I'm trying to use with and withCount together. For example: Article::with('Comments')->withCount('Comments')->paginate(); Problem I'm facing is, in the results is see for…
5
votes
3 answers

count relation of relation in laravel

Suppose I have a Conversation model like this : class Conversation extends Model { public function questions (){ return $this->hasMany('App\Question','conversation_id','conversation_id'); } public function category () { …
Ahmad Badpey
  • 6,348
  • 16
  • 93
  • 159
5
votes
1 answer

How do I query by geolocation in Laravel 5?

I am looking to add a geolocation attribute to my "Candidate" Eloquent model. I want to find Candidates based on their location. For instance, find all Candidates within 20 miles of San Francisco. What should my Eloquent model look like to store a…
Connor Leech
  • 18,052
  • 30
  • 105
  • 150
5
votes
1 answer

Laravel orm database query inside model function

I am new in Laravel. I want to make some custom functions in models which is related to database query. Class A Extends Controller{ public function view(){ B::get_user(); } } Class B Extends Model{ protected $table = "user"; public…
Vam
  • 429
  • 1
  • 4
  • 13
5
votes
3 answers

Applying hasManyThrough to deeper relationships

The Laravel docs seem to indicate that the hasManyThrough declaration can only be used for relationships that are two levels "deep". What about more complex relationships? For example, a User has many Subjects, each of which has many Decks, each of…
clb
  • 715
  • 9
  • 23
5
votes
3 answers

Laravel 5: How to do a join query on a pivot table using Eloquent?

I have 2 tables in my Laravel application namely customers and stores. Customers can belong to many stores and stores can have many customers. There's a pivot table between them to store that relation. Question is, how do I extract the list of…
captainblack
  • 4,107
  • 5
  • 50
  • 60
5
votes
2 answers

Running raw sql inside Model : Laravel

I am using Laravel and need to run raw sql query from a Model/Repository class INSERT INTO calendar (room_id, date, default_count, default_price) VALUES ('1', '2017-01-02', '2', '400004') ON DUPLICATE KEY UPDATE default_count =…
5
votes
2 answers

Laravel Create Model Class with tables that has a schema

i have a very simple question that somehow it might sound stupid but excuse me . I am working on a project where i use sql server with an existing database where table names are having a schema , forexample dob.Tablename . My question is , if i want…
robertrutenge
  • 678
  • 2
  • 9
  • 28
5
votes
4 answers

How to get the data of the inserted multiple rows in Laravel Eloquent ORM

So I manage to save multiple datas at once using laravel model::insert(array(...)); example data array ( 0 => array ( 'organization' => '1', 'status' => false, 'project' => '1', 'act_date' => '2016-11-23 08:19:06', ), 1 => …
Hard Spocker
  • 765
  • 11
  • 32
5
votes
2 answers

Laravel : SQLSTATE[42S22]: Column not found: 1054 Unknown column

I have three tables (services, services_products, services_product_translation) when try to insert new product I get this error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'services_products.services_product_id' in 'where clause' (SQL:…
Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71
5
votes
4 answers

Laravel eloquent relationships 3rd level

tables : divisions id name districts id division_id name subdistricts id district_id name class Division extends Model { // public function districts() { return…
Kabir Uddin
  • 61
  • 2
  • 7
5
votes
3 answers

Should a custom find method on a Laravel Model be static?

In the following Laravel 5 Model should the findByIdAndCourseOrFail method be static? class Section extends Model { //should this method be static? public function findByIdAndCourseOrFail($id, $courseId) { $result =…
Gazzer
  • 4,524
  • 10
  • 39
  • 49