Skip to main content
9 votes

Sanitize URL string for Insertion Upon DB table via Eloquent model

Beware of re-assigning a variable. In the setter method setUrlAttribute() the variable $saneUrl is over-written numerous times: ...
Sᴀᴍ Onᴇᴌᴀ's user avatar
7 votes

Queries to get unpaid and unpaid invoices

There seems to be a section in your code that you repeat three times. This can probably be placed in a separate function. Something like this: ...
KIKO Software's user avatar
7 votes
Accepted

Setting default of <options> in Laravel with PHP Match

Some variable assignments are excessive Let's look at what $selected is set to: $selected = 'selected'; Then in both of the <...
Sᴀᴍ Onᴇᴌᴀ's user avatar
7 votes
Accepted

Sanitize URL string for Insertion Upon DB table via Eloquent model

The biggest problem here is that you are going overboard with precautions. Security is important, but it must be logical. What ...
Your Common Sense's user avatar
7 votes
Accepted

User Management API

General review There are numerous Laravel features which could be used to simplify the controller methods - e.g. the base User model (which ships with the framework ...
Sᴀᴍ Onᴇᴌᴀ's user avatar
6 votes

Sanitize URL string for Insertion Upon DB table via Eloquent model

As some general feedback, I would say that your unit tests are lacking. It looks like you are just checking that your filter function modifies the input in some way? One of the main jobs of tests are ...
Pierre Menard's user avatar
6 votes

Laravel as client for Strava 3rd party API

Some minor points from me, just after a cursory look at the code. My PHP is rusty, but I know unset can destroy more than one variable. Useful to know. I would try ...
Kate's user avatar
  • 8,778
5 votes

Laravel 5.8: prevent duplicated code

I think you can DRY this up pretty well by refactoring. This feels like a “ifs are evil” code smell to me. There are three “if” statements within that whereHas. They are packed in fairly tightly, and ...
James Clark's user avatar
5 votes
Accepted

How to reduce a POST request duplicate in Laravel/PHP for payment system?

Let's implements following: separate logic, refactoring of methods Separate logic Validate Requests/PaymentRequest.php add rule for validation ...
Kostiantyn Okhotnyk's user avatar
5 votes
Accepted

Update specific model attributes from a request

How does this look? It looks okay. It appears to mostly adhere to PSR-12 and is easy to read. While there is only one method with a single loop it isn't very complicated. The variable names are ...
Sᴀᴍ Onᴇᴌᴀ's user avatar
4 votes

Let users create custom blade templates

Ensure template contains required tags The user shall be made to include the three <%X%> tags in the template code. It is difficult to know since the code ...
Sᴀᴍ Onᴇᴌᴀ's user avatar
4 votes
Accepted

Laravel UserController Design

First of all do not use Laravel 5.4 even if you've written some* code already, use latest 5.7, and try to always update. *: You really need to rewrite the application if you have code like you ...
Kyslik's user avatar
  • 181
4 votes
Accepted

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

You could use route model binding to remove some lines from your code, but that would require changing the route so it passes the MenuItem's id. ...
IGP's user avatar
  • 158
4 votes
Accepted

A method to get a document's path

I'm sorry, this is not an answer to your question, but it is a partial review of your code. It seems you've got a serious problem with choosing your names. They don't actually tell me what the ...
KIKO Software's user avatar
4 votes
Accepted

Using PHP goto Labels for code folding

The main issue I have with your approach is that it is non-standard and purely for the way that you work. Others looking at your code may be a bit confused as to what you are doing and why. In your ...
Nigel Ren's user avatar
  • 368
4 votes
Accepted

Laravel controller method that searches jobs using variable criteria

I'd like to recommend condensing some of your script so that it is easier on the eyes. This: ...
mickmackusa's user avatar
  • 8,802
4 votes

Refactor PHP | Laravel Code, Optimisation needed to make code clean

You could exploit Laravel collections to achieve what you need. Most of your code is checking for the presence/absence of props on your arrays. Laravel internally exposes and uses the ...
mdexp's user avatar
  • 141
4 votes
Accepted

What would be the best way to update data from associative array?

"Premature optimization is the root of all evil." As long as this code works and causes no problem, nothing should be really done. Trying to "improve" it, most likely you will cause a severe harm to ...
Your Common Sense's user avatar
4 votes
Accepted

Using method trait to return something in laravel model

I have question about scope yesterday, and someone gives an answer to don't return anything in scope. Is return something in scope really bad practice? This is because laravel is handling "it&...
user3402600's user avatar
4 votes

Structuring code logic for events on laravel controller

I'm mostly a back-end guy who fusses his way through front end code. Here's my advice: Keep the controller code as close as possible to handling the response only. Delegate the creation of objects to ...
Nick's user avatar
  • 141
4 votes

Laravel controller method to update user

So the above answer from Sᴀᴍ Onᴇᴌᴀ already covers quite a few points. I've worked with Laravel for the past 4 years, so I'll go a bit further in depth on how I'd approach this, including a few ...
Tropus's user avatar
  • 61
4 votes

Controller method to store course

You could create 2 sub-functions for the different parts in your function. validate($request) so that long part is hidden ...
Double_A's user avatar
  • 101
4 votes

Optimization of a Laravel controller that passes data and views

I don't use Laravel, but yes, you can greatly reduce the preparatory PHP scripting and make it more "D.R.Y.". You could use a lookup array and then null coalesce to fallback values, use a <...
mickmackusa's user avatar
  • 8,802
4 votes
Accepted

Optimization of a Laravel controller that passes data and views

Put your repeated or complicated queries into scopes on the model class. Here I also use the helper now to avoid importing Carbon: ...
Laurel's user avatar
  • 785
4 votes

Query to get summary of regional model with count of contracts

You can use the withCount() method to get the count of child relationships. Such as: user::withCount['getPosts']->find(1); ...
Mubashar Ali's user avatar
4 votes
Accepted

Retrieve config values using custom helper

I'll review your 2nd and most recent refactor. Your function getTheme() builds a space-delimited string of class values. Your current function name is clear about ...
mickmackusa's user avatar
  • 8,802
4 votes

Laravel controller getting results based on parameter including relationship results

Using eloquent when() function the above code would be cleaner, especially if you add more filters in the future. I am not sure what version of laravel you are using, but Request injection probably ...
Nikolas's user avatar
  • 161
4 votes
Accepted

Laravel 9 - Metadata creation using middleware?

Redis If you really need to use specific data and you don't have other ways to do it you can use Redis. Misc improvements (Not sure if this works with Laravel 9) You also should not save direct URL ...
Deadman's user avatar
  • 56
4 votes
Accepted

Laravel PostController

Thanks for migrating this from Stackoverflow! Let's take a look at your code: ...
Tim Lewis's user avatar
  • 186

Only top scored, non community-wiki answers of a minimum length are eligible