Reputation: 111
Here, I retrieve two tables to be displayed on one view page. The images are in the users table. When I clicked the profile tab, it displayed an error message:
Trying to get property of non-object.
What is wrong with my codes referring to the error message?
upload.blade.php
<div id="templatemo_sidebar">
<tr>
<div id="login">logged in as :</div>
</tr>
@foreach($users as $users)
<div id="img">
<img src="{!! '/profiles/'.$users->filePath !!}">{{$users->filePath}}
</div>
@endforeach
{!! Form::open(['action'=>'ProfileController@store', 'files'=>true]) !!}
<div class="form-group">
{!! Form::label('image', 'Choose an image') !!}
{!! Form::file('image') !!}
</div>
<div class="form-group">
{!! Form::submit('Save', array( 'class'=>'btn btn-danger form-control' )) !!}
</div>
{!! Form::close() !!}
@foreach($profiles as $profile)
<div id="profile_sidebar">
<tr>
<td>{{$profile->student_name}}</td>
</tr>
<tr>
<td>{{$profile->student_id}}</td>
</tr><br>
<tr>
<td>{{$profile->student_ic}}</td>
</tr>
<tr><br>
<td><mark>Status : {{$profile->status}}</mark></td>
</tr>
@endforeach
</div>
ProfileController.php
public function store(Request $request)
{
$users = Auth::user();
if($request->hasFile('image')) {
$file = Input::file('image');
//getting timestamp
//$timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
//$name = $timestamp. '-' .$file->getClientOriginalName();
$name=$file->getClientOriginalName();
$users->filePath = $name;
$file->move(public_path().'/profiles/', $name);
}
$users->save();
$users = Auth::user();
$users = Profile::where('student_id', $users->student_id)->get();
$profiles = Profile::all();
return view('profile', compact('users', 'profiles'));
}
}
Profile.php
class Profile extends Model
{
protected $table='profiles';
protected $fillable = ['student_id','student_name', 'program','faculty'];
public function user()
{
return $this->belongsTo('users');
}
public function setFirstNameAttribute($value) {
$this->attributes['faculty'] = ucfirst($value);
}
public function setLastNameAttribute($value) {
$this->attributes['program'] = ucfirst($value);
}
public function profilePicture(){
return $this->belongsTo('User', 'student_id');
}
}
Upvotes: 2
Views: 2634
Reputation: 3217
In your first foreach you have to use a different variable for iterating through the users collection, like this:
@foreach($users as $user)
Upvotes: 2