Laravel developers tips

Helpful solutions for Laravel framework developers

Get model column value in Laravel and new whereKey method in Laravel 5.4

January 22, 2017 by Marcin Nabiałek / 4 Comments

In some cases we want to get Eloquent model single column value based on other column value. Let’s assume we have user id and we want to get his e-mail only. We can obviously do:

PHP
1
2
$user = User::find($id);
$email = $user->email;

or even shorter:

PHP
1
$email = User::find($id)->email;

but there is one big problem with this – what if user doesn’t exist? Obviously … [Read more…]

Posted in: Eloquent, Laravel Tagged: value, wherekey

Data transformations in Laravel using collections and helper functions

December 28, 2016 by Marcin Nabiałek / Leave a Comment

When developing not trivial application it often happens that we need to make some data transformations. Let’s assume we we have user and address and we create API and want to data come in the following format:

Data format
PHP
1
2
3
4
5
6
7
8
9
[
  'name' => 'John',
  'surname' => 'Doe',
  'address' => [
     'street' => 'Street',
     'zipcode' => '23123',
     'city' => 'Some city',
  ]
]

but in database we don’t want to create separate table and put address fields in user table … [Read more…]

Posted in: Laravel, PHP, Request Tagged: array_dot, array_set, collections, data transformation, keyby

How to install Laravel 5 application in different versions

August 24, 2016 by Marcin Nabiałek / 7 Comments

Laravel 5.3 framework has been just released. I won’t focus on new features about it right now, I will just make it clear how to install different versions of Laravel without a problem as for many especially new developers it causes problem. As Composer is standard for installing dependencies let’s use this method to install … [Read more…]

Posted in: Composer, Laravel, PHP Tagged: branch, installation, project

Tricky things in PHP – floats

July 27, 2016 by Marcin Nabiałek / 2 Comments

As Laravel developer we don’t deal only with Laravel specific features, but quite often with pure PHP. Let’s verify your knowledge a bit. What would be the result of the following script:

PHP
1
2
3
<?php
 
die(var_dump(1200.85 * 100 === 120085));

Possible answers: false true It depends … Okay, you’ve chosen your answer. The second question – would the answer change if we … [Read more…]

Posted in: PHP Tagged: comparison, float, precision

Partial mocking and issues you don’t expect

June 30, 2016 by Marcin Nabiałek / 2 Comments

As we all know, writing tests is very useful when developing applications. But sometimes you might find issues when writing tests that are really hard to detect. If you ever got “Serialization of ‘Closure’ is not allowed” / “unserialize(): Error at offset 0 of 186 bytes in” errors when running PhpUnit together with Mockery – … [Read more…]

Posted in: Laravel, Mockery, PHP, PhpUnit, Testing Tagged: exceptions, issues, mocks

How to trim input in Laravel

June 26, 2016 by Marcin Nabiałek / 4 Comments

When you want to validate Laravel input, you can do it out of the box using for example Form Request class. Let’s assume you want to allow creating tags. Each created tag should have at least 3 characters. Simple? Well, it looks really easy, but when we look at details, we can find a few … [Read more…]

Posted in: Laravel, Request, Validation Tagged: input, trim
« Previous 1 2

Marcin Nabiałek

Marcin Nabiałek - Certified Laravel Developer
Marcin Nabiałek - Zend Certified PHP Engineer

Categories

  • Composer
  • Eloquent
  • Laravel
  • Laravel Blade
  • Laravel Dusk
  • Laravel packages
  • Mockery
  • PHP
  • PhpUnit
  • Request
  • Testing
  • Validation

Recent Posts

  • More flexible Laravel 7 Blade X components
  • Unit tests – complex argument matching with Mockery::on – the right way
  • Laravel validation – use multiple Form requests for complex scenarios
  • How to backup Laravel application to FTP/local computer
  • Best ways to validate data in Laravel

Recent Comments

  • Marcin Nabiałek on Laravel validation – use multiple Form requests for complex scenarios
  • Arek on Laravel validation – use multiple Form requests for complex scenarios
  • Marcin Nabiałek on I’m Laravel Certified Developer
  • Ridvan on I’m Laravel Certified Developer
  • cristi mocean on How to make your Laravel (Dusk) tests 3 times Faster

Archives

  • April 2020
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • October 2017
  • September 2017
  • April 2017
  • January 2017
  • December 2016
  • August 2016
  • July 2016
  • June 2016

Categories

  • Composer
  • Eloquent
  • Laravel
  • Laravel Blade
  • Laravel Dusk
  • Laravel packages
  • Mockery
  • PHP
  • PhpUnit
  • Request
  • Testing
  • Validation

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2026 Marcin Nabiałek