Use Stack Overflow for Teams at work to find answers in a private and secure environment. Get your first 10 users free. .

Questions tagged [eloquent]

Eloquent is Laravel's own object-relational mapper. It is named "Eloquent" because it allows you to work with your database objects and relationships using an eloquent and expressive syntax.

Filter by
Sorted by
Tagged with
1
vote
1answer
15 views

Laravel paginate with only first result

i'm kinda new to Laravel so I hope the question is clear enough. I have a table with Users and another table with Tasks. In my User model I have the following : public function tasks() { return ...
0
votes
1answer
22 views

Laravel belongsToMany.belongsTo nested relationship query where child column = parent id

I have two models (and database tables with Laravel naming conventions) and I am trying to Eager Load the Collections with Equipment and Equipment.collection_meters where collection_meter equals ...
0
votes
3answers
34 views

Laravel @include controller data

I'm trying to receive data on a sidebar that is included in the blade template but i'm not getting any data delivered. I've tried adding @include('admin.sidebar',['message_counter' => $...
2
votes
1answer
54 views

Laravel Eloquent ORM filter with relationship

I have this model using laravel 5.8: Model I need to get all Relatorios from one User. I've already set up relationships with models, this way: Cliente public function relatorios(){ ...
1
vote
1answer
40 views

How to hide relationships column in Eloquent?

I have the Eloquent query in which three tables are connected. $result = Kompaniya::with(['Otdel'=>function($query){ $query->select('kompaniya_id','title as otdel','id'); ...
0
votes
1answer
45 views

Laravel Eloquent - get count of another table

I have an eloquent model "Athlete" and there is another table performances. Each athlete has 0 to many performances. And I would like get best performance of each athlete(personal best) or null if the ...
0
votes
1answer
22 views

Eloquent belongsToMany relation with the not standard column name

I have a problem. I have a database image and I wanna make belongsToMany relation with the not standard column name. I have table order as relation table and order detail as details, but when I make ...
1
vote
2answers
34 views

Laravel: update rows with one query

I have title and slug field in my table, where slug == null. And now how I can write to slug field title slug using facade Str::slug($title) with one query if it possible of course. I can do it ...
0
votes
1answer
27 views

Laravel - Pivot table with 3 models realtionships Many to Many and One to Many

I want to make authentification in my Laravel project for Users who have Roles and Permissions. Actually That's not a problem to do but Users can create Teams or be invited member of any team and each ...
2
votes
1answer
34 views

Lazy eager loaded relationship missing in toArray()

Laravel 5.8 I am lazy loading an user with the related customer which has a one-to-one-relation with a crmaccount-object The models are working so that when i retrieve the eager-loaded entity it ...
0
votes
1answer
36 views

One To Many inverse

I would like to obtain for ONE user his country. So I have two tables: User id country_id Countries id I can't decide which relationship to use. I think it is the OneToMany relationship (Inverse) ...
1
vote
2answers
34 views

laravel query relationship to check if data exists

I have a set up like this, QUIZ MODEL public function scores() { return $this->hasMany('App\Score'); } SCORE MODEL public function quiz() { return $this->belongsTo('App\Quiz'); } ...
0
votes
0answers
42 views

Eloquent get ALL users, but order from related table first

I'm building my very first Laravel project and I'm trying to get all users and sort them by the amount of products they have. This is my code: I have User.php public function products() { return ...
0
votes
1answer
25 views

Laravel loading pilymorphic related model with main model not working

Suppose I have a posts, and attachments tables, What I want is to get a specific post, and load its attachments. Currently, I'm doing this: $post->load([ 'attachments:id,name,original_name,size,...
0
votes
1answer
42 views

Getting a value from another two models at the same time in Laravel

Basically I have a function within a controller which is public function get_all_clubs($city) { $city = ucwords($city); $city_des = Town::where('town', $city)->value('...
0
votes
0answers
26 views

Laravel eloquent with 2 table relationships

I want to get the first and the last data from data that returned by Laravel eloquent, which is using date range and there are 2 date ranges which is weekly = today to last week and monthly = at the ...
1
vote
1answer
37 views

Relationships not working after setTable() in eloquent

Consider without using relation first, DB::connection()->enableQueryLog(); $oPost = new Post(); $oPost->setTable('posts_two'); $aPost = $oPost->get(); $queries = DB::...
-1
votes
1answer
33 views

Laravel Time Range

I am using Laravel 5.8 and I have two columns with data type timestamp, start_time and end_time. Before saving a new record I want to check if it already exists. For example if I have already in my ...
0
votes
2answers
30 views

How to customize working hours for a project in Laravel [on hold]

I am working on a project with Laravel in which I have to add the working hours for an office for example one branch office works from monday to friday and other branch office from the same company ...
0
votes
0answers
37 views

Update Model Laravel

While updating the model depending on the where clause, eloquent is unable to update the table. Using DB::enableQueryLog(), I found the raw query and executed it on Mysql workbench, it works as ...
1
vote
1answer
33 views

Laravel - How to load a hasone relation on a hasmany relation

I'm trying to load a hasone relation on a loaded hasmany relation. Basically, I have loaded my completion logs, but would like to also load the pool_section model relation for each of the loaded ...
0
votes
2answers
41 views

Laravel Eloquent ORM returning stdClass instead of Model instance

When I query data with eloquent, it returns stdClass objects instead of an instance of my Model when using the get() function. $userFind = User::find(1) $userGet = DB::table('users') -&...
0
votes
1answer
34 views

How to change the default Laravel Eloquent table-id-name

When i create a Laravel Factory like this: factory(App\User::class, 10)->create()->each(function ($user) { $user->useroptions()->save(factory(App\UserOption::class)->make(['...
0
votes
3answers
41 views

Laravel db:seed success but no data in my database

I tried to make simple seed in laravel, but when I did "php artisan db:seed", it didn't fill my field. I made my Categories seeder file this is my Categories seeder file : class CategoryTableSeeder ...
0
votes
1answer
44 views

Should we use count method of eloquent?

What is the best practice to get the count of the records from database in mySql. As if records count get increased(>1000000) then its very hectic to find the number of records because query get ...
0
votes
2answers
44 views

How to get property or method in relationship ( laravel - eloquent )

in Post Model function user() { return $this->belongsTo( \App\User::class); } in User Model function posts() { return $this->hasMany( \App\Post::class); } function somedata() { ...
0
votes
1answer
44 views

How to build a quaternary relationship in laravel 6?

I have to create a relationship between this tables: As you can see, I need to relate 4 tables creating a quaternary relationship, asuming that the database model is ok, how i can do the relationship ...
0
votes
2answers
28 views

Relationship to users table without using the standard 'user_id' column name

I have 2 tables, one has different columns to record different users names based on authorisation level. but i would like to link to two together. at the moment i have tried the following: User.php ...
0
votes
3answers
34 views

How to remove and update data on relations table using laravel eloquent?

I have three tables. they are, users id name 1 personA 2 personB 3 personC skills id name 1 html 2 css 3 javascript userskills userid skills_id 2 3 2 ...
0
votes
0answers
15 views

Laravel doesnthave() generate a query, that not escape \

I have a users table, and it has a relation with businesses table, what I want is to search for users that don't have a business yet, but their role is business (Users which not created their business ...
0
votes
1answer
16 views

Laravel nested select and conditional statement in view blade

So I've succeeded to make a calendar display for the current month. This time, I want to show event details for a specific date. I'm not sure how to get the $event data where $event == $date. This is ...
0
votes
0answers
29 views

Laravel Eloquent Using sub query search or filter with left table on name or title condition

++++ PieModel code ++++++++++ <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class PieVideo extends Model { protected $table="sc_pie_video"; public $timestamps =...
1
vote
2answers
41 views

How to validate DateTime Laravel

I am saving in the database a DateTime format for creating an appointment. I have two inputs one for start_time and the other for end_time and it saves like this 2019-09-16 08:00:00. In the view for ...
0
votes
3answers
45 views

If relationship doesn't exist use parent's relationship

In my app I have Games and Tournaments, with a Game optionally belonging to a Tournament. Both the Game and the Tournament can have a belongsTo relationship with a PhotoAlbum. What I want now is if ...
0
votes
2answers
28 views

How to whereDate, whereMonth a variable?

I want to display the total payments overall, and total payments for this month using only one variable. Controller $payments = Payment::where('user_id', auth()->user()->id)->get(); return ...
0
votes
1answer
26 views

Get raw sql query from belongsToMany relationship

How do I get raw sql like "SELECT * FROM table WHERE id = 1" from belongsToMany. public function users(){ return $this->belongsToMany('App\User','user_projects','project_id','user_id'); } i ...
0
votes
0answers
6 views

How to insert data with multiple foreign keys in Laravel 5.7 with Eloquente?

I'm working in a web application with Laravel 5.7, where I create differents employment contracts each one is a blade view with a form with input text between the content of the contract. So when a ...
1
vote
0answers
16 views

How to find nearest users from Latitude and Longitude

I want to query nearby users, but I am getting this error: SQLSTATE[42000]: Syntax error or access violation: 1582 Incorrect parameter count in the call to native function 'radians' (SQL: select *, ...
0
votes
2answers
27 views

Why I'm getting a null value when querying a model in eloquent?

I have a problem where I'm getting a null value when querying a model in eloquent, I have a two models User.php and Project.php, When I use tinker and use User.php to check if the data has a relation ...
0
votes
1answer
32 views

Filtering withCount relationships with whereHas/whereIn doesn't work

I am building a not-that-complex API with multiple related models but I can't seem to make the whereHas filter work as wanted. So I have a structure like this : Mapregions -> (hasMany) Subregions -...
0
votes
0answers
36 views

using laravel delete() three times in one controller function

I am new programming as well Laravel I have used my approach to delete the two Relations or Hierarchy for this situation like City could have many Towns & Towns also could have many Sub-towns ...
0
votes
1answer
68 views

Model return empty array when use select in model (Laravel)

I have a hasOne(Many) relation function like this: return $this->hasOne('App\Models\ProductTranslation','product_id','id')->where('language_id', $language_id['id']); Also, I tried to use ...
0
votes
1answer
32 views

How to mock User model within composer package development tests?

I started creating a laravel 5.8 based modular API framework for our company which should be extended using composer packages. Now I stumbled over the problem to test each package by itself (each ...
0
votes
1answer
63 views

How can I display a table value based on the selection from a dropdown?

I want to display (on an input text field) the description of a product (which is a MEDIUM TEXT field type) based on the selection of a dropdown containing the product ID. The tricky part is that I ...
0
votes
2answers
33 views

Slow query in laravel

I have a laravel application but a query needs a lot of time to be executed. I tryied to build a raw query but with no success The query is this one: $utente = Utente::with('coll')->with('...
0
votes
0answers
39 views

laravel eloquent postgres two rows to one in result

I read a little bit and dont want to use json_agg, i would like to have another fild in my result like name2. My call with the Join works etc. but i would like to hae a little bit other result and I ...
1
vote
1answer
19 views

Laravel 5.2 - use attach method with array values

I am creating a blog with posts in Laravel 5.2. Every post is going to have multiple tags. I created a relationship between posts and tags. When the post is created, I am trying to attach multiple ...
0
votes
1answer
34 views

Why stored duplicate rows to table

I have custom query for adding parsed remote posts to table: foreach ($parsedPosts as $post) { $hasRemotePostAlready = Post::where('remote_post_id', $post->id)->first(); if(null === $...
0
votes
1answer
35 views

How to select all values with latest dates Laravel 5.8

I am trying to get all of the rows with the latest dates I want to get the rows 3 and 4 only When I use this eloquent query: $items = Purchase::whereYear('created_at', '=', $year) -&...
0
votes
3answers
41 views

Laravel: Selecting all films where the condiction is in another table

I have two models ("Films" and "Tags") related in a Many to Many relation. I have therefore Three Tables: "films", "tags" and "film_tag" EDITED: I have belongsToMany relations in place like this: ...