Laravel does not display uploaded image - laravel

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>

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

Trying to get property 'image' of non-object in 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.

Simple qrcode to generate in DOMPDF

Hi i cant generate simpleqrcode in dompdf
Here is my blade.php
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string')) !!}">
in my controller
public function printpdf($isbn)
{
$data = [
'isbn' => $isbn
];
$pdf = PDF::loadView('main.inventory.view_pdf ', $data);
return $pdf->stream($isbn.'.pdf');
}
i tried this image and successfully render
<img src="{{ public_path('/uploads/image/1578635297.jpg')}}" style="width:30%;height:50%;">
Dont know why i cant generate qrcode to dompdf but when i generate in other blade its working but not in dompdf
I got mine working by initializing the base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string')) in a variable within a controller, and passing it to the view.
Controller:
public function foobar() {
$qrcode = base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string'));
$pdf = PDF::loadView('main.inventory.view_pdf', compact('qrcode'));
return $pdf->stream();
}
View:
<img src="data:image/png;base64, {!! $qrcode !!}">
Used laravel-snappy instead of laravel-dompdf. https://github.com/barryvdh/laravel-snappy
If you're still unable to generate a pdf file, then temporarily change the read-write permissions of the folder with chmod -R 777 <folder_path>.

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'));

How do i retrieve the content data to blade from this json

i'm encountered with an blade issue.
[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]
This is the json i'm getting from webservice, i need to get each image url and need to assign to a tag . how could i achive it?
#foreach($json->content as $img)
<img src="{{$img}}" alt="alt text">
#endforeach
From the information provided that is the most I can give you.
For the given json, you can try something like this on your controller:
public function index()
{
$json_string = '[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]';
$json = json_decode($json_string, true);
$images = json_decode($json[0]['content']);
return view('index', compact('images'));
}
Then, on your blade view:
#foreach($images as $image)
<img src="$image">
#endforeach

Resources