Show the result of a route in blade view (Laravel) - laravel

I have a route that return an image, and i want the show the result of this route in a blade view, is there a way to directly call controller action or result of this route directly in the blade view ?
Thanks in advance.

You rather don't want to return image in blade. You should create separate controller action, that will return this image and link image src to this route, for example in controller:
public function image()
{
return $img = Image::make('image.jpg')->resize(800, 500)
->insert('sub_image.jpg', 'bottom-right', 10, 10) ->response();
}
then you create route to this controller action:
Route::get('my-image', 'MyController#image')->name('get-my-image');
and then in your Blade view you should link image src to this action:
<img src="{{ route('get-my-image') }}">

You can do this by giving the URL in src attribute of <img> tag as:
<img src="{{ route('image.url') }}">

Well if it's an image, why don't you use an img tag?
<img src="{{ route('my.image') }}" alt="My Image">

Related

Image from storage folder is not showing in laravel

I have stored a image in my storage directory using spatie media package, the url to the image as returned by the getFullUrl() is http://127.0.0.1:8000/storage/34/conversions/download-thumb.jpg.
my APP_URL is APP_URL=http://127.0.0.1:8000
Then in in my blade file i'm using <img src="{{ $channel->image() }}" alt="">
And my function look like this
public function image() {
if($this->media->first())
{
return $this->media->first()->getFullUrl('thumb');
}
return null;
}
I'm not sure why my image is not showing on this url

Laravel: Display image from public storage

I am saving an uploaded image like this:
$profile_picture = $request->file('profile_picture');
$user->profile_picture = $profile_picture->getClientOriginalName();
Storage::disk('public')->put( $user->profile_picture, $request->file('profile_picture') );
and then I am trying to display that image in HTML with:
<img class="img-fluid" src="{{ asset('storage/'. Auth::user()->profile_picture) }}">
the image is being saved in storage/app/public, but the image is not showing in html, how do i go about this?
thank you
You may use response method on a Laravel filesystem
return Storage::disk('public')->response($user->profile_picture);
N.B: In order to do that, you need a Laravel route. The above code should be located in a controller method

NotFoundHttpException error in laravel

This is my view:
<img src="../public/{{ $axid->img }}">
and this is my route :
Route::get('/ax/{axid}', function($id) {
$axid = show::find($id);
return view('ax', compact('axid'));
Images are uploaded in public/uploads and show is model name. However, I have this error:
NotFoundHttpException
...what is the problem?
If we assume that your image resolving code is functioning properly and that you are returning the full url of the image you want to show, then change your view code to:
<img src="{{ asset($axid->img) }}">
Please see how you should be setting this out:
View before moving to this a href..: Get ID
Route: Route::get('/ax/{id}', 'AxidController#get');
Controller with me assuming Show is your model name:
public function get($id, Show $show)
{
$axid = $show->find($id);
return view('page', compact('axid'));
}
On the page of ax/{id}:
<img src="{{ $axid->image }}">

Display image in laravel

I have already uploaded the image in a folder and stored its path in the database field. I am trying to display image like this:
Controller:
public function showProfileImage($id)
{
$profileimage = UserProfileModel::where(['usrID' => $id])->first();
return view('userlogin::welcome')->with('profileimage', $profileimage->userPicpath);
}
my blade template has:
<img src="{{ route('showprofileimage', ['id' => Session::get('loggedinuserid')]) }}" height="150" />
But the path returned in the src is the route path, it doesn't contain the 'profileimage' path returned form the controller. How do i do this? Thanks
The route('<named route>') helper function returns the URL to a page/endpoint/resource that was defined as:
Route::get('some/uri', ['as' => 'showprofileimage', function() {
//
});
It may not be used for displaying images (although it could be used with a few tweaks), but I would go with either one of the following approaches:
If you have the fully qualified image URL then you can use the url() helper function. Do:
<img src="{{ url('showprofileimage') }}" ... />
If your image is in the /public directory, you can use the asset() helper function:
<img src="{{ asset('path/to/profile/image') }}" ... />
If your image is in the /storage directory, you can use the storage() helper function (but I believe this is not recommended):
<img src="{{ storage('path/to/profile/image') }}" ... />
Hope this helps. Cheers!

laravel passing parameters from view to route

Using a view with user input. I then want to pass to a route. This what I found so far:
href="{{URL::to('customers/single'$params')}}"
I want to pass the user input as the above $params to my route. This is sample of my route:
Route::get('customer/{id}', function($id) {
$customer = Customer::find($id);
return View::make('customers/single')
->with('customer', $customer);
As soon as I can pass the parameter I can do what I want with the route, which I know how.
Basically you can pass parameter to routes by doing:
Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
In your anchor tag, instead of doing href={{URL...}}, do something like:
{{ URL::to('user/$param') }}
For more information on routing, visit link
This is what I have and works:
<a <button type="button" class="buttonSmall" id="customerView" href="{{URL::to('customer',array('id'=>'abf'))}}" >View</button></a>
But I need the array value 'abf' to be the value of a textbox.
This worked for me in my view anchor tag
href="{{ URL::to('user/'.$param) }}"
instead of what was specified above
href="{{ URL::to('user/$param') }}"
You can user it in View as I used:
<a class="stocks_list" href="/profile/{{ Auth::user()->username }}">Profile</a>
Hope it helps you.

Resources