310 Best Reply Awards

  • Member Since 2 Years Ago
  • 688 Lessons Completed
  • 2 Favorites

22nd June, 2017

ohffs left a reply on Folder Picker • 4 days ago

I don't think you can do that without using a browser plugin. You could add the 'multiple' option to the input to allow them to do a 'select all' of the files in the folder though, eg

<input type="file" name="import_file" multiple />

16th June, 2017

ohffs left a reply on Testing File Download With Dusk • 1 week ago

You might be better using the 'basic' http tests for downloads. You can do things like :

$response = $this->actingAs($user)->get(route('export.report'));

$response->assertHeader('content-disposition', 'attachment; filename="report.csv"');
$file = $response->getFile();
$filename = $file->getPathname();
// do whatever you need with the filename/file

ohffs started a new conversation JS Laravel-like Collections Library • 1 week ago


Seems quite nice - even if it will make me make even more -> vs . mistakes when switching between PHP and JS... ;-)

5th June, 2017

ohffs left a reply on Wow, PHP Reddit Really Is Full Of Jerks • 3 weeks ago

Wow, Reddit really is full of jerks

ftfy ;-)

I occasionally look at it for links to new projects/stuff (much like I use Hacker News) or to find documentaries I'd never come across myself (see /r/Documentaries) - but yeah, never read the comments ;-)

I guess the only up-side is that it makes twitter seem (marginally) more reasonable ;-)

2nd June, 2017

ohffs left a reply on Call Custom Class Inside Eloquent • 3 weeks ago

I guess you could do :

public function getFormattedPriceAttribute($value)
  return (new Format)->price($value);

If you're going to have maybe other format methods though you might want to do something like :

class Format
  public function __construct($model)
    $this->model = $model;

  public function price()
    return $this->model->price . '$'; // whatever

// Model
public function format()
  return new Format($this));

// Elsewhere

There's a lesson on here that might help :-)

31st May, 2017

ohffs left a reply on Multiple Dependent Jobs In Laravel 5.4 • 3 weeks ago

If it's any use, Laravel 5.5 should come with 'chainable jobs' which I think covers your needs - do job A, if that works, do job B etc? Not much detailed docs yet (obviously) but mentioned in the changelog.

It might save you a bit of work if you can hold off a little and work on another part of the code? Or pull in the 5.5 dev code to try?

27th May, 2017

ohffs left a reply on Code Quality Check • 4 weeks ago

I've run phpmetrics (or an article about using it in the past - it's free/open source so you could give it a try.

Other than that I just run phpcs/phpmd myself or as part of my gitlab CI runs. Sometimes I have to disable phpmd though as I'm way too lazy to try and figure out how to configure all it's rules :-/

I've never even looked into the JS side of 'code quality' - my JS use is pretty minimal and I know it'd just give a report back with a kind of look of pity ;-)

ohffs left a reply on Difference Array_filter With & Without Laravel • 1 month ago

I think the error just comes from how the PHP error levels are set by the framework - you could alter it to make the error (in a PHP kind of way) not be an error... ;-)

But you're probably better making your code do something safer like (in php7) :

return $acsv['MYKEY'] ?? false;

26th May, 2017

ohffs left a reply on Url In Email Includes An Ending Curly Bracket %7D • 1 month ago

Maybe try using

{!! $url !!}

If that doesn't help, try dd'ing the $this->url inside the mailable before sending it just to check what it looks like outside of the view itself. If that still looks fine, maybe add a @php dd($url) @endphp block into the view itself and see.

25th May, 2017

ohffs left a reply on Create A Form To Be Filled By Steps • 1 month ago

If you're ok with going down a more javascript route, you could use something like uikits tabs or a vue tabs component. Just make step 2, step 3 disabled until step 1 was valid and so on?

24th May, 2017

ohffs left a reply on Testing Validation • 1 month ago

It depends how bad it would be for your application if, for instance, you comment one of them out while debugging then forget to put it back.

If you're on 5.4 you should be able to do a one-liner something like (if I remember correctly!) :

$response->assertSessionHasErrors(['field1', 'field2', 'field3', ...]);

which would at least cover that the validation was picking up 'something' was going wrong.

23rd May, 2017

ohffs left a reply on Is This How To Register An Event In A Package? • 1 month ago

Ahh - got you. I'm assuming when you fire the event the listener isn't being triggered? If you set them up then do :

if (Event::hasListeners('Aacotroneo\Saml2\Events\Saml2LoginEvent')) {

that should (ha) at least tell you if the system has registered the event. Other than that - I'm not much help I'm afraid :-/

ohffs left a reply on Is This How To Register An Event In A Package? • 1 month ago

I think (and might well be wrong!) that using the facade you can only register callback listeners, and not class-based ones. So you can do :

\Illuminate\Support\Facades\Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function($whatever) {
  // do something

To use class-based ones you have to shove them into the protected $listen array on the EventServiceProvider like :

protected $listen = [
  'Aacotroneo\Saml2\Events\Saml2LoginEvent' => [

I might well be wrong about the facade though - I don't really use the event stuff much and when I do it's via the $listen style.

19th May, 2017

ohffs left a reply on Collection Methods Series, Please :) • 1 month ago

Thirded, on the Refactoring to Collections series - it's really good stuff. There's also a video of Adam's conference talk on using collections on youtube too if you want an intro to the kind of things he goes through in the ..Collections series too :


18th May, 2017

ohffs left a reply on How To Know The Laravel Php Base? • 1 month ago

There's also version_compare() if you need to do something, e.g. :

if (version_compare(phpversion(), '7.0.0', '>=')) {
    require 'iknow/iknow/mysql_shim.php';

ohffs left a reply on Get Laravel 5 DD In Laravel 4 • 1 month ago

You could also try kint instead.

17th May, 2017

ohffs left a reply on Announcement: Laravel Sketchpad 1.2 Is Out • 1 month ago

Goodness - that's really pretty slick :-) Have you done any kind of write-up about it? Be a nice read :-)

ohffs left a reply on Best Performance Stucture • 1 month ago

If that's the main feature of your application and performance is key, then I'd probably stick with one table. If there's going to be a lot of data then maybe think about something like hbase instead of a regular sql db.

16th May, 2017

ohffs left a reply on Can We Get Exact Updated Field From The Update() • 1 month ago

I don't think you can do it directly, but a slightly more verbose way is :

$model = Model::find($id);
$model->fill(['email' => [email protected]']);
$changes = $model->getDirty();
// $changes is an array of changed fields ('email' in this case)

You could always write a helper method on the model if you need to do it a lot, something along the lines of :

public function change($data)
  $changes = $model->getDirty();
  if (count($changes) == 0) {
    return false;
  return $changes;

ohffs left a reply on Testing After A Command Completes • 1 month ago

Is it because your command is doing something similar to the factory like Carbon::now()->subDays(1) and it's running so fast it's not seeing the record as being different than the overdue_at field? Try something like this and see if it helps :

factory(Record::class)->create(['overdue_at' => Carbon::now()->subDays(1)->subHours(1)]);

(or addHours() if that's the condition you're checking)

15th May, 2017

ohffs left a reply on White Background In Editor • 1 month ago

I've taken to wearing sunglasses... ;-)

12th May, 2017

ohffs left a reply on Laravel Frustration • 1 month ago

@ZetecVan we were all kidding - you're totally old! ;-p

ohffs left a reply on Laravel Frustration • 1 month ago

Good luck @muuucho :-). I wrote my first program in the late 1970's - so I guess this thread is full of the forum oldies ;-)

11th May, 2017

ohffs left a reply on Laravel Frustration • 1 month ago

If it's any help, a lot of the code for the lessons is available to download on github.

If you're willing to pay a little money, then Matt Stauffers book might be helpful - maybe easier to read the lessons at your own pace than listen if English isn't your first language?

ohffs left a reply on Is Hopping To Latest Version Of Laravel A Pain? • 1 month ago

Mostly minor versions (i.e., 5.2 -> 5.3) are fairly straightforward. Sometimes there's a bit of hassle, but it's mostly ok.

The last big change in directory structure was from 4.x -> 5.x - hence the major version bump.

You can also save a lot of the basic 'grunt work' of upgrading by using something like shift.

9th May, 2017

ohffs left a reply on What Is The Best Way To Achieve Unique Urls For Each Item • 1 month ago

Or maybe use something like hashids.

ohffs started a new conversation L5.4.22 Security Update • 1 month ago

Sounds like it's worth investigating if your code uses the built-in password reset code :


ohffs left a reply on Looking For Best Admin-panel Theme For School Management System • 1 month ago

Is there anything else you'd like it to do?

8th May, 2017

ohffs left a reply on Slow Excel Report With MySQL Queries Inside Nested Foreach Loops • 1 month ago

Ontop of eager loading as @jbloomstrom suggests, you could also look into spout - I use it a lot instead of the phpexcel-derived libraries. It's a lot faster and lower memory overhead. Doesn't do everything that phpexcel can - but for most cases it's fine.

You could also try just building an array of data rather than building the spreadsheet. Then dump it out as one big "set".

6th May, 2017

ohffs left a reply on Reasons Why We Need Repositories Even If We Have The Model Eloquent • 1 month ago

@JeffreyWay I wonder if you'd consider re-visiting the repository video(s) sometime? They come up a lot on the forum and generate 'a bit' of debate.

Other than annoying Jeffrey though... I think repositories have their place - but there seems to be some kind of 'mantra' floating about that if you're not using them then in some way your project is doomed - or "you're not doing it right". It quite often seems to be the 'go-to pattern' that people implement to solve a problem they don't have.

Say I have a controller like :

class MyController {
  public function __construct(MyEloquentModel $model)
    $this->repository = $model;

  public function index()
    $items = $this->repository->all();
    return view('mymodel.index', ['items' => $items]);

  public function show($id)
    $item = $this->repository->find($id);
    return view(...);

Am I now 'using the repository pattern'? If not and I create a 'repository' like :

class MyModelRepository
  public function all()
    return MyModel::all();

  public function find($id)
    return MyModel::find($id);

I'm surely now using the repository pattern, right? Bonus reddit points all round! :-)

I think the repository pattern gets thrown around as a panacea for 'what if I (change framework|change ORM|word ends)' or as a 'fancy' way of describing a wrapper around something you don't have 100% control over.

I think the 'Active Record Pattern' (or 'anti-pattern' for more reddit bonus points) is so close to being 'a repository' in practical terms that it's not worth giving a lot of thought to unless you're in a strong position to know that you're likely to change framework/ORM/whatever a lot. But what if you change from PHP to Ruby or Node? Your repository is toast anyway.

Making a 'repository' for something like a 3rd-party HTTP API still makes good sense as they are kind of in the place the DB layer was when people started to fret about 'the repository pattern' to begin with. I also tend to 'collapse' long eloquent/ORM calls down to a method as they make me a bit twitchy ;-)

I do think the repository pattern has places it's the right thing to use. I just think for 99% of cases in a PHP web app, it's not needed and just adds a layer of abstraction over the layer of abstraction we're already using.

Anyway - sorry for the long rant!

ohffs left a reply on Testing File Uploads Fails At Validation • 1 month ago

Not sure - I've seen file upload/validation stuff fail in weird ways if the file is bigger than php is configured for. Try uploading a tiny file maybe just in case?

Other than that - maybe before the validation try storing the file somewhere on disk and seeing what it contains - just to make sure it's actually being passed through?

ohffs left a reply on How Far Do You Take Testing? • 1 month ago

Crud stuff can be quite tedious to test. In this kind of case (assuming a contact is something a user is adding) if it really mattered (ie, do you care if they type in garbage contact data) I'd probably end up with tests along the lines of :


// which pretty much checks for a redirect/session error




// and assuming your twitter validation was extending the laravel validation



For things like a phone or email I generally just trust laravel's built-in validation - I just care it's 'phone-like' or 'email-like' - same for a twitter handle. If it's vital that those are 'real' then I'd bother.

Maybe I'm lazy, but if a user types in the wrong twitter handle or phone number then I'm not that fussed - they'll figure it out themselves ;-)

I've also been bitten down the years by adding too much validation. A project I worked on a while back gave me a load of sample data - the title of the items were all (like 1000's of them) of the format 'XXX9999', so I had a little bit of validation to check that. First day it was rolled out I got complaints because they wanted to add 'XXX_9999 (XX)'. So quickly changed to just a 'required' check rather than any kind of 'validation'.

ohffs left a reply on Testing File Uploads Fails At Validation • 1 month ago

Have you tried dd'ing the $request in the controller before the validation call? Just to see what's in it?

ohffs left a reply on Testing File Uploads Fails At Validation • 1 month ago

...What is actually going wrong?

5th May, 2017

ohffs left a reply on Useful Option "Import Class" In SublimeText • 1 month ago

Give this a try : https://github.com/erichard/SublimePHPCompanion . You need to add your own keymappings for it - but other than that it works fine :-)

ohffs left a reply on Prevent Changes To Form Action By Use Of Developer Tools • 1 month ago

I don't think I'd worry about it :-)

ohffs left a reply on Prevent Changes To Form Action By Use Of Developer Tools • 1 month ago

If the user doesn't have permission to add a classification to company 2, then hopefully you'll check that before proceeding and bail out.

If they do have permission then I'd just shrug. If someone wants to 'hack' your system to do something they could do already - then, well, whatever ;-)

Most people are just doing their jobs and really don't care :-)

ohffs left a reply on Testing With File Uploads • 1 month ago

Yep - it's annoyingly awkward to test a real file upload :-/

ohffs left a reply on Testing With File Uploads • 1 month ago

Could you get away with using the storage fake? If not and you actually want to test the file is written to disk, in the past I've done something like :

$file = './tests/data/myfile.dat';

// call post with the request data

$dbFile = FileModel::first();
$original = file_get_contents($file);
$new = file_get_contents($dbFile->path);

$this->assertEquals($original, $new);

ohffs left a reply on Laravel-Excel Return Redirect() Has No Effect • 1 month ago

Do you need to bail out on the first error? If not I'd just keep adding errors then return them all. Something along the lines of :

$this->errors = new \Illuminate\Support\MessageBag;
   if ($validator->fails()) {
     $this->errors->add('Line', $count);
return redirect('dashboard')->withErrors($this->errors);

If you do need to stop on the first error, if you save the result of the call to Excel::load and check it to see what to do.

3rd May, 2017

ohffs left a reply on Laravel 5.4 On CentOS 7 And PHP 7 (Production) • 1 month ago

Yeah - but it's not very friendly, sadly :-/ It's something like - but my selinux-foo is fading fast as I usually give in and disable it (and feel guilty, but there we go) :

semanage fcontext -a -t 'httpd_cache_t' '/path/to/your/storage(/.*)?'

restorecon -Rvvv /path/to/your/storage

ohffs left a reply on Laravel 5.4 On CentOS 7 And PHP 7 (Production) • 1 month ago

Are you running chown/chmod with the '-R' flag (recursive)? If you have already done that - try running sestatus to check if selinux is maybe causing the problem. If it says it is enabled try running setenforce 0 and see if you can write to the logs ok.

ohffs left a reply on Progress Updates For Long Operations • 1 month ago

You could dump the long tasks progress to something like redis, then have a JS timeout which queries a url which returns it. Something like (but not actually real code) :

public function longTask()
  $key = 'task-status-' . auth()->id;
  foreach ($things as $thing) {
    // do something
    Redis::rpush($key, "processed $thing");

public function taskStatus()
  $key = 'task-status-' . auth()->id;
  $log = Redis::lrange($key, 0, -1);
  return json_encode($log);

// js
setTimeout(function() {
  // http get /taskstatus
  // update contents of div with result
}, 1000);

2nd May, 2017

ohffs left a reply on Installing Laravel Dependencies Globally For Multiple Websites • 1 month ago

I guess you could just have a 'master' project with the dependencies installed then symlink the vendor/ folder into the other projects. But it might cause you other problems - projects needing slightly different dependencies or versions etc.

ohffs started a new conversation 'Kint' Dd() Style Debug Tool • 1 month ago

Been playing with this a bit - it seems very nice. Works like dd() but gives a lot more info and will format a lot of data in a really clean way :


Just thought it was worth sharing :-)

ohffs left a reply on PHP Popularity • 1 month ago

At a very rough guess (and having looked at a lot of job adverts towards the end of last year), PHP has come one quite a bit in the past few years so there are more jobs asking for (say) a 'symfony/laravel/drupal/wordpress' developer rather than a PHP one.

Compare google trends for PHP and Laravel for instance :



That said - it's always worth having another language or two in your toolbelt. In job terms, Javascript, python and java aren't going to disappear anytime soon - and I think PHP will be around with them.

1st May, 2017

ohffs left a reply on Display History Of User By Id User • 1 month ago

This package might do what you're after : https://github.com/spatie/laravel-activitylog

Edit Your Profile

Want to change your profile photo? We pull from gravatar.com.