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:

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

or even shorter:

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
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:

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

Copyright © 2023 Marcin Nabiałek