Tips to Know You in Laravel

Laravel_learn
2 min readOct 8, 2022

--

How fluent Class can reduce your time usage.

A fluent class is a good way to get your variable-store data as an array.

A fluent class is a General structure

This means that we can now handle data fluently and easily.

Ok, How to use it.

use Illuminate\Support\Fluent;

$data = [‘name’ => ‘John’];

$data = new Fluent($data);

return the name or default name

return $data->get(‘name’, ‘Robbert’);

Return 403

If you want to return a 403 error and you use like this

$example = $example::find($id);

if($example == null){

abort(403);

}

return $example;

But how to return 403 without an if condition?

$example = Category::where(‘id’, $id)->firstOr(fn () =>abort(403));

You want to return the last day’s data from the database.

example::where(‘created_at’ , ‘’, now()->subDay())->get();

But how can we shorten this line of code?

We can shorten this line of code by using resent()

User::recent()->get();

Are you using faker in laravel? but haven’t created an image from faker?

Good news you can now create fake photos by using an user image.

$factory->define(User::class, function (Faker $faker)){

return [

‘name’=> $faker->name,

‘UserImage’ => $faker->image(storge_path(‘image’), 50, 50);

]

}

Result:

Know that you’ve learned four tricks that can help you make your code faster and easier.

Everything we do is practice for something bigger than where we are right now. Practice only leads to improvement.

--

--

No responses yet