Laravel Code Tips #1

Bayram EKER
2 min readSep 16, 2022

--

1-) forceFill Method

#Laravel tip: You can force fill guarded fills in a model with the `forceFill` method

// In laravel you can use the forceFill method
// to fill guarded fills in a model
$user->forceFill([‘name’=>$request->name])->save();

2-) isDirty Method

#Laravel tip, the `isDirty` method:

$product = Product::first();
$product->isDirty(); // false
$product->name = “Bayram Eker”;
$product->isDirty(); //true
// You can also check a spesific field
$product->isDirty(‘name’); //true
$product->isDirty(‘price’); //false

3-) The Elvis Operator

#laravel tip. The Elvis operator.

if(!$foo) {
$foo = “bar”;
}
$foo = $foo ?: ‘bar’;

Bonus : render()

#Laravel Blade is useful for other things besides HTML.

You can generate any type of flat file, for example, a dynamic shell script or sitemap file. You only need to call the `render()` method on a view to get the resulting string.

$script = view(‘deploy-script’)->render();$ssh = self::createSshConnection();Log:info(“Running commands…”);
$process = $ssh->execute(explode(“\n”,$script));

Thank you for reading this article.

If you find this article useful please kindly share it with your network and feel free to use the comment section for questions, answers, and contributions.

You might also like :

--

--

Responses (2)