0

I want to display user's image in menu bar that is part of master page. master.blade.php

<div class="login_area">
    @if (Auth::check())
    <img src="{{asset('public/user_images/1531068734.jpg')}}" height="40px"
    width="40px" alt="" class="pull-left img-circle login"> 
    <a href="{{ route('logout') }}" onclick="event.preventDefault();                                                
      document.getElementById('logout-form').submit();"><i class="fa fa-sign-in" aria-hidden="true"></i> Logout</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
     <input type="hidden" name="_token" value="{{ csrf_token() }}">
  </form>

     @else 
     <a href="http://www.abc.in\login">Login</a><a href="http://abc.in/register">Registration</a> 
@endif
   </div>

Image model : Each image is associated with each profile. and each profile is associated with user.

<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class image extends Model
{

protected $fillable = 
['profile_id','user_id','image_name','image_status','image_permission'];
protected $primaryKey = 'image_id';
public function profile()
{

return $this->belongsTo(profile::class,'id','id');
}
}
Kapil Dev
  • 11
  • 1
  • 8

5 Answers5

0

You can simply use the general method to display an image as below. You do not need to use public/ as it is the default path for assets.

<img src="/user_images/1531068734.jpg" height="40px" width="40px" alt="" class="pull-left img-circle login"> 
Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34
  • my question is how to pass image url from controller to master page view. because there is no route for master page. – Kapil Dev Sep 12 '18 at 14:36
  • You can simply get the profile image from the `Auth::user()`, i suppose you have the relationship between user and profile image – Sujan Gainju Sep 12 '18 at 14:38
0

Symply using

<img src="{{ auth()->user()->image }}" height="40px" 
     width="40px" alt="" class="pull-left img-circle login" />
Goms
  • 2,424
  • 4
  • 19
  • 36
  • how can i do it using query building ? because a user has many images, i need to display only profile picture – Kapil Dev Sep 12 '18 at 14:57
0

You can simply use the View class's share method to make a variable (or any value) available to all views in laravel.

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them

Inside the AppServiceProvider file, you will add the code like this: View::share('key', 'value') For example:

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Replace 'key' with what you like, and 'value' with the user image path.

Solution 2: You can also use ViewComposer for this: https://laravel.com/docs/5.5/views#view-composers

sdkcodes
  • 142
  • 2
  • 6
0

You do not need pass image url to view from controller. You can print image path in view.

I saw an error your src value of tag. You are using as

asset('public/user_images/1531068734.jpg')

but asset method are using already "public" as root folder. You should remove "public" so, it should be following to;

// If image path stored as only file name;

asset('user_images/' . auth()->user()->needed_profile_image_column)

// If image path stored as full path with file name;

asset(auth()->user()->needed_profile_image_column)

Could you try what I mentioned above?

Community
  • 1
  • 1
FGDeveloper
  • 1,030
  • 9
  • 23
  • each user has multiple images, i need to display only profile image( image_status == 'profile') . how to use where condition with this relationship.auth()->user()->needed_profile_image_column – Kapil Dev Sep 13 '18 at 09:43
  • You can custom attribute in your model. You can set any image to this custom attribute. Check the laravel model attribute on laravel doc. – FGDeveloper Sep 13 '18 at 09:48
  • i want to display only profile image uploaded by that user. like we see in facebook navigation bar – Kapil Dev Sep 13 '18 at 09:52
0

I want describe more detail my above comment. Laravel provide adding a custom attribute that for using as db table column.

Please add following code to your User model;

.....

protected $appends = [
    'profile_photo',
];

/**
 * Get user profile photo
 *
 * @return string
 */
public function getProfilePhotoAttribute()
{
    $profile_photo = null;
    if($this->image_status == 'profile'){
        $profile_photo = public_path('user_images/' . $this->image));
    }
    return $profile_photo;
}

Please add following code to your navigation area;

<div class="login_area">
    @if (Auth::check())
        <img src="{{ auth()->user()->profile_photo }}" height="40px" width="40px" alt="" class="pull-left img-circle login"> 
        <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"><i class="fa fa-sign-in" aria-hidden="true"></i> Logout</a>
        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
        </form>
    @else 
        <a href="http://www.abc.in\login">Login</a>
        <a href="http://abc.in/register">Registration</a> 
    @endif
</div>

I think this will solve your problem.

FGDeveloper
  • 1,030
  • 9
  • 23