Trying to get property 'image' of non-object in Laravel - laravel

I am working on a laravel project. In this project, when i try to view and edit the profile page. I am getting this error.
ErrorException
Trying to get property 'image' of non-object (View: C:\xampp\htdocs\HomeServices\resources\views\livewire\sprovider\sprovider-profile-component.blade.php)
http://127.0.0.1:8000/sprovider/profile
SproviderProfileComponent.php :-
<?php
namespace App\Http\Livewire\Sprovider;
use App\Models\ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class SproviderProfileComponent extends Component
{
public function render()
{
$sprovider = ServiceProvider::where('user_id',Auth::user()->id)->first();
return view('livewire.sprovider.sprovider-profile-component',['sprovider'=>$sprovider])->layout('layouts.base');
}
}
Models/ServiceProvider.php :-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ServiceProvider extends Model
{
use HasFactory;
protected $fillable = ['user_id'];
public function category()
{
return $this->belongsTo(ServiceCategory::class,'service_category_id');
}
}
sprovider-profile-component.blade.php
<div class="panel-body">
<div class="row">
<div class="col-md-4">
#if($sprovider->image)
<img src="{{asset('images/sproviders')}}/{{$sprovider->image}}" width="100%" />
#else
<img src="{{asset('images/sproviders/default.jpg')}}" width="100%" />
#endif
</div>
<div class="col-md-8">
<h3>Name: {{Auth::user()->name}}</h3>
<p>{{$sprovider->about}}</p>
<p><b>Email: </b>{{Auth::user()->email}}</p>
<p><b>Phone: </b>{{Auth::user()->phone}}</p>
<p><b>City: </b>{{$sprovider->city}}</p>
<p><b>Service Category: </b>
#if($sprovider->service_category_id)
{{$sprovider->category->name}}
#endif
</p>
<p><b>Service Locations: {{$sprovider->service_locations}}</b></p>
Edit Profile
</div>
</div>
</div>

The only reason this could happen is because $sprovider is null.
When it happens, $sprovider->image will translate to (null)->image, which is indeed Trying to get property 'image' of non-object.
You could do this to prevent $sprovider from being null:
$sprovider = ServiceProvider::where('user_id',Auth::user()->id)->firstOrFail();
By using firstOrFail instead of first, you ensure $sprovider will never be null (like the name suggests, if it doesn't find any provider, it will fail).
You will have another error saying that no provider could be found, this is another issue, probably because you don't have any provider for this user or something like that.

Two possibilities
$sprovider is null and
image key is not available in $sprovider ($sprovider->image) may be you mis-spelled image in database table so for that just use dd($sprovider); or print_r($sprovider); and check image key is available or not.

Related

Laravel 9 Livewire The photo failed to upload

I started learning Laravel 9 livewire. I am trying to make an image upload application. I did the exact example in the Livewire documentation, but I get an error that the image could not be uploaded. When I do the same application on my mac computer, it works without any problems. It doesn't work when I do it on my windows computer.
ImageUpload.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithFileUploads;
class ImegaUpload extends Component
{
use WithFileUploads;
public $photo;
public function save()
{
$this->validate([
'photo' => 'image|max:1024', // 1MB Max
]);
$this->photo->store('photos');
}
public function render()
{
return view('livewire.imega-upload');
}
}
image-upload.blade.php
<div>
<h1>Image upload</h1>
<form wire:submit.prevent="save">
<input type="file" wire:model="photo">
#error('photo')
<span class="error">{{ $message }}</span>
#enderror
<button type="submit">Save Photo</button>
</form>
</div>
Error
I'm doing the example in the Livewire documentation but still getting the error. Not even the livewire-temp folder is created.
Look like your PHP.ini is missing
sys_temp_dir
and
upload_tmp_dir
uncomment and then set its value.
mine example.
Laragon on win 10:
sys_temp_dir = "c\laragon\tmp"
upload_tmp_dir = "c\laragon\tmp"
all temp files will go to that location (not just uploaded)
In
App\Http\Middleware\TrustProxies
change
protected $proxies;
to
protected $proxies = '*';
it helped me

Laravel Relationship not working with belongsTo

Hello Guys, I am just passing my query to notification blade, but its gave error. I dont know what i did wrong with bellow code. If you guys fix this issue i will be very glad. Thanks in advance
Notification seen model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class notificationseen extends Model
{
use HasFactory;
protected $table = 'notificationseens';
public function Notification()
{
return $this->belongsTo(Notification::class, 'notificationID');
}
}
Notification Model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
protected $table = 'notifications';
public function notificationseen()
{
return $this->belongsTo(notificationseen::class, 'notificationID');
}
}
View Blade
#foreach( $notification as $notify )
#if($notify->Notification->seen == 0)
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}" id="notifysee" data-id="{{ $notify->id }}">
<div class="alert unread custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#else
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}">
<div class="alert custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#endif
#endforeach
Table structure
$table->increments('id');
$table->integer('userid');
$table->integer('notificationID');
$table->integer('seen')->default('0')->comment("0 for unseen 1 for seen");
$table->timestamps();
Can you please help me out. I cant see any issue but its me error "Attempt to read property "seen" on null"
Ok, some things:
I would use belongsTo in the notificationseen class unless one notificationseen could have more than one Notifications to belong to ;)
Do a Notification have more than one notificationseen references? I do not think so, so in your Notification change the reference to hasOne.
In your blade, use #if($notify->notificationseen->seen == 0) or you call better "Notification::with('notificationseen')->get()" in your controller and then pass it to your view.
You can try to add an isset to avoid the error :
#if(isset($notify->Notification->seen) && $notify->Notification->seen == 0)
(...)
#else
(...)
#endif
EDIT : in your code, you defined 2 belongsTo methods, but according to the official Laravel documentation, you must define a hasOne method and the inverse of it, the belongsTo method.

Laravel does not display uploaded image

I'll need your help there, I have a form when I submit an image, it's working fine, but when I try to display the image on an another page, its display a white square (I can see that it's working because I can see the name of the image in the console).
This is my app.blade.php :
<div class="menu_connect_big_screen">
<img src="{{Auth::user()->image}}"/>
<p>{{Auth::user()->firstName}} {{Auth::user()->lastName}}</p>
Mon compte | Se déconnecter
</div>
And this is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
class ProfilePictureController extends Controller
{
public function update(Request $request)
{
$request = $request->All();
User::where('id', Auth::user()->id)
->update(
[
'image' => $request['image']]
);
return redirect()->to('/')->with(['image-changed' => 'Photo de profil modifiée !']);
}
}
I'm kinda new to laravel so any help would be thankful.
Thank you.
If the image is stored in blob you should use base64 in your image tag like so;
<img src="data:image/jpeg;base64,{{base64_encode( Auth::user()->image )}}"/>
However, this is not specific to Laravel.
For example user images stored in public/user_images directory
<div class="menu_connect_big_screen">
<img src="{{ asset('user_images/' . Auth::user()->image) }}" height="50px" width="50px">
<p>{{Auth::user()->firstName}} {{Auth::user()->lastName}}</p>
Mon compte | Se déconnecter
</div>

Unable to Pass Variable from Controller to Drop Down

I'm trying to get data from a table called solutions and view the solution names in a dropdown list on a form called products.
I've created a provider called DynamicDropdown in App/Providers.
<?php
namespace App\Providers;
use App\Dropdown;
use Illuminate\Support\ServiceProvider;
class DynamicDropdown extends ServiceProvider
{
public function boot()
{
view()->composer('*', function ($view) {
$view->with('product_array', Dropdown::all());
});
}
}
Controller
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Providers\DynamicDropdown;
use App\Product;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
public function index(Request $request)
{
$select = [];
foreach ($view as $data) {
$select [$data->id] = $data->solutionname;
}
return view('products.products', compact('select'));
}
}
I've called the select variable in the view but unfortunately, I'm getting an undefined variable error.
Blade
<div class="form-group <?php echo e($errors->has('solution') ? 'has-error' : ''); ?>">
<?php echo Form::label('solution', 'Solution', ['class' => 'control-label']); ?>
<?php echo Form::select('solution', $select, null,
('' == 'required') ? ['class' => 'form-control', 'required' => 'required'] : ['class' => 'form-control']); ?>
<?php echo $errors->first('solution', '<p class="help-block">:message</p>'); ?>
</div>
Error Message is...
ErrorException thrown with message "Undefined variable: select (View:
E:\Laravel\IBMint\resources\views\products\products\form.blade.php)
I tried many ways to fix it. But still could not find a solution. Appreciate if someone could assist.
Thank You.
It is because you are actually calling the $select variable on the wrong form. You return a view
return view('products.products', compact('select'));
which means get the view products.blade.php on the folder products under the view folder.
and based on the error you put the $select variable on the form.blade.php.
Fix will be you return the view form.blade.php like this.
return view('products.products.form', compact('select'));

Laravel Invalid argument supplied for foreach() fetching individual columns

can somebody help with this? I'm trying to pass some data from my Controller page to my index, but I'm getting this error "Invalid argument supplied for foreach()"
Here's my model
class Bookings extends Model
{
//Table name
protected $table = 'bookings';
//Primary key
public $primaryKey = 'book_id';
}
My Controller
use Illuminate\Http\Request;
use App\Bookings;
class PagesController extends Controller
{
public function index(){
$data = Bookings::orderBy('NameENG', 'DetailsENG')->get();
$data = Bookings::all();
$marks = 'NameENG';
$briefs = 'DetailsENG';
return view('pages/index', compact(['data', 'marks', 'briefs']));
}
}
and part of my index
<div class = "container">
#if(count($data) > 0)
#foreach($marks as $mark)
#foreach($briefs as $brief)
<div class="row">
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img class="card-img-top" alt="Thumbnail [100%x225]" style="height: 225px; width: 100%; display: block;" src="images/calle_yungay.png">
<div class="card-body">
<h5>{{$mark}}</h5>
<p>{{$brief}}</p>
Can someone help please?
On controller function:
$routes = 'tourNameENG'; // is a string
and on view, you are trying to iterate it:
#foreach($routes as $route)
that's why the error Invalid argument supplied for foreach().
foreach(): works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

Resources