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
0 votes
1 answer
37 views

Laravel controller getting results based on parameter including relationship results

I am trying to learn to improve my coding on a daily basis and this is one of the first times I am posting on here, so bear with me. I have two models, a Payment and an Expense model. A ...
user avatar
  • 377
1 vote
1 answer
97 views

Optimization of laravel pivot table relationship

I have a pivot table called invite_riskarea which is designed as follows: This table handle the permissions that have a specific user (through an invite id) to ...
user avatar
  • 127
0 votes
1 answer
73 views

A create gateway controller in Laravel

In my app users can create and soft-delete payment gateways something like Paypal. Users of a website can connect to the gateway and make payments. I've written the logic in a transaction also locking ...
user avatar
0 votes
1 answer
75 views

Eloquent eager loading deep nested relation with "where" condition

is there a more elegant way to do what the code below does without use "foreach"? I am trying to load answers from questions but only the answers that contains a direct relation with an ...
user avatar
1 vote
1 answer
80 views

Laravel CRUD Controller - code request

Here is my starting point for a laravel CRUD controller. I would appreciate it if someone could do a review and show me how an experienced developer would approach this. I do not want to progress with ...
user avatar
1 vote
2 answers
182 views

'Lean controller'/'Business logic on service' and filtering, sorting, pagination methods on controllers

I'm currently reading about "Lean controllers"/"Business logic on services" and trying to refactor some legacy code. However I am struggling to apply what is taught in the ...
user avatar
2 votes
1 answer
83 views

PHP Laravel Order generation Custom Class

Here is a custom class I use to handle different types of orders in Laravel. This question includes the code regarding Order Type A. I want to know whether I can simplify the code. I have different ...
user avatar
  • 47
5 votes
1 answer
918 views

Laravel Scope get request data/param

I want to pass the param from the request on the scope method in the model and serve it as json to be rendered by select2. Which is better? 1. Pass request from the controller like this controller: <...
user avatar
3 votes
1 answer
274 views

Get list and default value of columns of table with Eloquent

I use the below code to get a list of columns in a table, and assign the default value to the protected var $attributes in a Laravel Model. In this code, I use the DB function because I don't know ...
user avatar
  • 131
1 vote
0 answers
32 views

what is the correct way to pass 3 where () conditions in the same function, multiple filters | eloquente - laravel

see: https://stackoverflow.com/questions/58882690/what-is-the-correct-way-to-pass-3-where-conditions-in-the-same-function-mult/58883277?noredirect=1#comment104033826_58883277 to better understand ...
user avatar
1 vote
1 answer
457 views

PHP Laravel - Improving and refactoring code to Reduce Queries

Improve Request to Reduce Queries I have a web application, where users can upload Documents or Emails, to what I call a Strema. The users can then define document fields email fields to the stream, ...
user avatar
  • 237
1 vote
1 answer
76 views

A PHP Controller, which feels to 'array-like', in Laravel

This is a controller, which receives the validated input from a http request, then finds the assorted model and for each translation key in the received data, it will add it to the model. ...
user avatar
2 votes
1 answer
538 views

Retrive relation on a polymorphic model

I have a Game model that is "static". Containing game information and such, and users can comment on all games. Games model <...
user avatar
  • 215
4 votes
1 answer
92 views

Users adding each other in Laravel

I'm looking for a code review for the following system that I created. It allows a landlord, to add a tenant to start a tenancy. It has added, accept, reject, and prohibits a tenant from adding ...
user avatar
  • 41
1 vote
0 answers
203 views

Replacing a substring in a table

I am using DB::table to search through a table that has a field called 'data' that has a substring ...
user avatar
  • 935
4 votes
1 answer
706 views

Laravel Account models

This library is part of an accounting system. Two major models are involved. For every transaction there is an entry in the AccountTransaction model and ...
user avatar
0 votes
1 answer
930 views

Compare two tables and arrange the values in a new array

I have t things working with this current setup: ...
user avatar
  • 215
1 vote
1 answer
161 views

Laravel - super long relation, it doesn't feel right

Okay, This is my method: ...
user avatar
1 vote
1 answer
91 views

Sending email notifications, each to a different set of subscribers

Here is a code which sends email notification on new comments. ...
user avatar
5 votes
1 answer
838 views

Laravel Pagination with haversine formula in it

I'm using the haversine formula to count distance from selected post-code to desired destination. I have the following scope in ...
user avatar
  • 96
1 vote
0 answers
347 views

Extend the Laravel Eloquent model in order to easier implement CRUD REST for my routes

By extending the Model and using this inside my routefiles, I can easily call these functions inside the corresponding HTTP ...
user avatar
  • 63
1 vote
1 answer
67 views

Phishing Project - Refactor Using Models

After receiving some good feedback on the previous question, I've refactored my database and have begun to refactor the associated code. As a result of implementing models for all tables in the ...
user avatar
  • 818
2 votes
1 answer
105 views

Database query with Eloquent

I have got two tables articles and tags in the database. My goal was to retrieve all fields from articles table and one category field from tags table with Eloquent. I couldn't do this so instead I ...
user avatar
5 votes
1 answer
1k views

Eloquent methods to calculate the average rent of apartments

I am working on a Laravel 5.1 website where I am counting the average rent of apartments in a specific area. Besides of that I want to get the average rent by amount of rooms in the apartment. The ...
user avatar
1 vote
1 answer
139 views

Method on Laravel model to find courses related to a course

I have a Laravel project that has courses. I want to take the related courses from one. Should I create the method in the model (the php class) or in the controller? Currently I have this code in ...
user avatar
0 votes
1 answer
56 views

Compare 2 Different database Table

I have php function that compares two different database table. Any suggestion on improving it. ...
user avatar
  • 563
4 votes
2 answers
367 views

4 database queries, multiple loops in loops, slow processing time

The octoberCMS(based on laravel) controller below is what I use to select the lowest price in 4 pricelists in the given daterange. Prices can vary and it's not guaranteed that the more expensive ...
user avatar
2 votes
0 answers
723 views

Extended Laravel Eloquent models to add filters to model setters

One of the tasks I find myself doing repeatedly is to filter the incoming inputs that will be stored in models. Examples of this include trimming, removing repeated or unnecessary pre/suffixes, commas ...
user avatar
  • 530
1 vote
0 answers
54 views

Eloquent Scope and Normal Way

I read the documentation part of Eloquent, especially scopes: Scopes allow you to easily re-use query logic in your models. To define a scope, simply prefix a model method with scope: And so, with ...
user avatar
4 votes
2 answers
6k views

Effective usage of multiple joins in Eloquent dependent on variable

I have an Eloquent model that has multiple types of joins that is used to filter out different request to grab the data of movies e.g.: Actors, ...
user avatar
  • 1,200
1 vote
1 answer
20k views

Optimizing Laravel Eloquent multiple joins

The following is working and readable SQL for Eloquent that I have created. The downside is that it looks more like a 1:1 with SQL and I don't think I'm using the full potential of Eloquent. I thought ...
user avatar
  • 1,200