Image Component
Usage
From the Model
Assuming you have the avatar_id
column in your users table, which stores attachment ID and relation for it.
In order to show the attached image, you just need to pass the relation “path” as a key into the make()
field factory method.
You can pass the column name, which stores the Attachment ID.
If you store URL in database it still is valid option.
External image
A path to an image can be passed directly to a src()
method; more details can be found here.
Options
Set of useful methods to control images.
Sizes
You can specify both image width and height if you wish. By default, the image stretches over the entire available space and keeps its proportions.
It can accept not only integer
values, but any valid CSS property as a string
.
Default values for width are 100%
and auto
for height.
If you wish to make a square image, you may pass both width and height as one property using the size()
method.
Alt
The alt property simply represents the image alt attribute.
In order to change the alt attribute of the image, just pass the value into the alt()
method.
Fit property
It represents the CSS object-fit property and accepts values in a Bootstrap way.
Accepts one of these 5 values: contain
, cover
, fill
, scale
(for scale-down), or none
.
You can also use the ObjectFit
enum instead.
Placeholder
So far, we are dealing with users who have avatars. But what if they don’t?
For this case, placeholder can be used. It accepts a path to an image, which will be shown instead of an avatar if there is none.
In this example, the no-avatar.jpg
image will be shown if the user has no avatar.
Src
You may pass a value directly into the src()
method. This is useful for showing external images or images within the table layout.
It may accept not only a string
but a relation that returns an Attachment or the Attachment model itself.
Aspect Ratio
Caption
Use it when you need picture description