Do you have the path to that image in your $userlist object? And one more thing: this is not correct HTML! This is never gonna work of course....
how to display the picture uploaded by the user beside the user's name in viewpage
i already edited the database for my photos with the user's id who uploaded the picture with unique name and successfully save with the id of the user.
the question in my mind now is that how can i display the correct image uploaded by the user beside the users name in my view page?
here is a part of my viewpage where suppose the image of the user will be displayed besides the username
foreach($users as $userlist) img src="" a href="{{ action('ProfileController@show', [$userlist->id]) }}" h2 {{ $userlist->name }} /h2 /a endforeach()
@blackbird i dont have directly have image path in my $userlist but i have a separate table that hold the path of the image and referenced by userId of the user who uploaded the image.
I know i posted an incorrect HTML but i just remove the < and > , by the way how to post a code here to include in my reply?
You use the high comma for that ` your code `
You should do that in your controller. Get all the stuff you need and send that to the view ;)
@dinis can you show me your controller and route
@blackbird
here is my controller, what i am suppose to do is like make a join query from 2 tables a photos and users
where the list of users in view page will have a picture beside their name
`
public function index()
{
$photos = Photo::all();
$users = User::latest('created_at')->get();
$homepage = view('profile',compact("users","photos"));
return $homepage;
}
`
If you using local server then you should not save path you have to only save photos name
@vipin93 im not saving path im done with the saving photos name in database, what im suppose to do now is to display a list of names of users beside with the image that they upload
In last two days back i have same problem but i fixed it by using Repository method
What I do is save the user's 'avatar' in public/avatars. On the users table, I have an image_path column. Then when displaying the user, I have
<img src="{{ asset($user->image_path) }}" />
@dinis here is my controller http://laravel.io/bin/9vRjG and repository http://laravel.io/bin/DelYx and view http://laravel.io/bin/PXxY1
maybe you dont get my explanation or i post a wrong question haha, i can already display the images in the view page with the help of @blackbird
what i did inside my database is that i separate the table of photos form users, i add another column to the photos table where the userId column of photos table and the id column of users table will be match.
Now the format i want to display in the view page is like this [image][user's name]
my problem is that how can i call the correct image beside the user's name? where the userId of image must be the same with the user's id in the list
it's like this [userId=1][user.id=1] where the two table have a relation via userId and user's id
@dinis i get your point before it you have make relationship between " User and Photo" Model and it will work
@vipin93 haven't noticed that on laravel 5 fundamentals, is there any link that you can give me to read or watch that has no conflict with the latest laravel 5?
actually i'm now using L4.2! have you try it?
@vipin93 nope not totally
I am assuming you have a one to one relationship between your User model and Photo model:
//create the relationship in your user model
public function photo()
{
return $this->hasOne('photo');
}
Do the same code in your photo model if you need the reverse (except function maybe user and instead of photo have 'user'
Where ever you have your query I would suggest eager loading the photo table:
$users = User::with('photo')->get();
In your view simply:
@foreach($users as $user)
<img src="{{$user->photo->image_path }}" alt="{{ $user->username}}"><p>{{ $user->username }}</p>
@endforeach
@jekinney thanks it works, is it possible that if the user upload a new picture, the latest uploaded picture will be displayed and the old will be deleted inside my public folder and database?
*note: im storing image name with uniqid() in database and public folder
@dinis its possible by form binding method!
@vipin93 dont just state that it is possible/working, add additional code how can it be possible.
@jekinney as i have noticed with your posted code it will produce "Trying to get property of non-object" error if the user does not upload any image
@dinis I would suggest the following:
- Create a media table to store the images, it can have a polymorphic relation with your user table
- Store only the image name in the table
- If you define the relationships correctly, your $user object would have a media object and can be accessed using $user->media
- To display the image in the frontend or anywhere, you should have a config setting (in your config file array('user_image_path'=> asset('images/users') ) something like that
- Config::get('user_image_path').'/'.$user->media->image_name should give you the full image path. should display the image
You can also use Intervention package and Intervention Image cache package to dynamically cache and resize the image on the fly.
@lalitesh i already have a photos table as my media table, and i store the image name only in the table. i was able to display the image correctly besides the user who upload the image.
my problem now is that when a newly registered user does not upload image after logging in they get a message "Trying to get property of non-object" because in my viewpage i have this
@foreach($users as $user) <img src="{{ $user->photo->profileimage }}"><p>{{ $user->name }}</p> @endforeach
@dinis here controller http://laravel.io/bin/OeMqK and edit view is like this http://laravel.io/bin/xKQyv and route http://laravel.io/bin/eDynb recently i did it
@vipin93 but my form for the image upload is separated, here is how my current code flow after register user will be login automatically and within the home a list of name with image of all user is displayed, the user can only upload image after register.
that's why im getting a "Trying to get property of non-object", maybe the solution is that when the user register the photo table must also be filled with default data. do you have idea coding that way?
@can you show me your image upload view and controller
@lalitesh sorry i was lost in your reply, where should i place that? i tried to place that on the view page part but still i got "Trying to get property of non-object"
controller `public function uploadTest(Request $request) {
$this->validate($request,['photoFilename'=>'required']);
// Check if there is an image
if (Input::hasFile('photoFilename'))
{
// Assuming the photo model has a field profileimage
// We then get the file and upload it
// as you can see we change the name of the file and set a unique_id there, so the image is unique
// In your view you can use the $photo->profileimage to get the correct source
$photo = Photo::create($request->all());
$request = $request->all();
$request['user_id'] = Auth::id();
$file = Input::file('photoFilename');
$photoname = uniqid();
$file->move(public_path() . '/hphotos/xpf1', uniqid(). '.' .$file->guessClientExtension());
$photo->profileimage = '/hphotos/xpf1/' . $photoname . '.' .$file->guessClientExtension();
$photo->user_id = $request['user_id'];
$photo->save();
}
// Redirect to another page
}`
view page
@foreach($users as $user) <img src="{{ $user->photo->profileimage }}"><p>{{ $user->name }}</p> @endforeach
@dinis try this http://laravel.io/bin/JxmBE
@vipin93 i still get the same "Trying to get property of non-object "
@dinis can you show me problem(error)
Please or to participate in this conversation.