Worked for me
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
public function index(){
$data = HeaderSlider::all();
return $data;
}
<ul *ngFor="let item of data">
<img [src]="item.image" />
</ul>
Worked for me
Thansk for replying but i want to sent a complete url in end point include image path bcs front end dont know where it hosted
I know
and I dont know how you have stored the link to the image in your database
private function uploadProfilePicture(Request $request)
{
try {
$file = $request->file('profile_picture');
if(!empty($file) && $file != null)
{
$user = getApiUser();
$user_profile = User::whereId($user->id)->first();
$fileExentsion = $file->extension();
$fileName = $file->getClientOriginalName();
$fileFullName = time()."_".$fileName;
$path = Str::slug($fileFullName).".".$fileExentsion;
$file->move(public_path('profile-pictures/'), $path);
$fullpath = 'profile-pictures/'.$path;
$user_profile->profile_picture = $fullpath;
$user_profile->save();
return $fullpath;
}
} catch (Exception $ex) {
error_logs("An error occurred while uploading profile picture!");
}
}
So, in $user_profile->profile_picture you save the path to the user's picture as
profile-pictures/timestamp.originalname.originalextension
and the image is in the public_path (/public/profile-pictures etc etc)
so in your controller you need to return the URL to this
$url = asset($user->profile_picture);
@snapey bro this not working for me
$user = User::find($user->id);
$url = asset($user->profile_picture);
\Log::info($url) ===> get ``http://127.0.0.1:8000/ ``
$msg = "Signing In.";
$this->success($msg);
$this->with('user', $user);
$this->with('token', $token);
Do you have the URL stored in the database for this user?
have you set APP_URL in config or .env ?
Yes @snapey i have the image path in database but problem is that frontend not now the where it is hosted
and in my .env APP_URL is http://localhost
open tinker
>>> $u = App\User::find(1) //insert valid user number
>>> $u->profile_picture;
>>> asset($u->profile_picture);
what do you get
@snapey brother how can return this to api with response
this is my code give me some more help of you so i solve problem
$user = User::find($user->id);
\Log::info( asset($user->profile_picture));
$msg = "Signing In.";
$this->success($msg);
$this->with('user', $user);
why are you reloading user?
$user = User::find($user->id);
So you have shown in tinker that the code I suggested works. Just return url with your data
so How can return user with this url where can i put asset functionality
$user = User::find($user->id);
asset($user->profile_picture);
$msg = "Signing In.";
$this->success($msg);
$this->with('user', $user);
should i store with complete url in database because when user upate profile then what will functionality
You want it as part of the user data, and not returned to the client as a separate property?
first, stop doing this... its pointless.
$user = User::find($user->id);
In your user model, add the following;
Public function getPictureAttribute()
{
return asset($this->profile_picture);
}
add an property called $appends
protected $appends = ['picture'];
https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor
https://laravel.com/docs/7.x/eloquent-serialization#appending-values-to-json
this working fine store database and fetch this is true?
$user_profile->profile_picture = asset($fullpath);
$user_profile->save();
Not a good idea since you will then encode the website URL into the profile image.
Just do it as suggested
Or do what you like, I'm done here.
@snapey Thanks dear brother for helping me
why i need to encode i have functionality when user will update picture then old picture delete from database and new picture will upload
private function deleteOldFile($request)
{
//check whether there is a new image in the coming requets
$file = $request->file('profile_picture');
if(!empty($file) && $file != null)
{
//remove image from user table
$user = User::find(getApiUser()->id);
if($user){
$path = $user->profile_picture;
$user->update([
'profile_picture' => ''
]);
$fullPath = public_path()."/".$path;
if(is_file($fullPath))
{
unlink($fullPath);
}
$fullPath = public_path()."/".$path;
// $fullPath = str_replace('profile-pictures/', 'profile-pictures/', $fullPath);
if(is_file($fullPath))
{
unlink($fullPath);
}
}
return true;
} else {
return false;
}
}
No idea what you are talking about, sorry. Was that a question?
@snapey sorry for disturb you... i have not concept so discuss with you..
Thanks alot for lot of helping..
much appreciated !
Please or to participate in this conversation.