in laravel, i have to link the storage using php artisan storage:link so i can show the image that when user to post a article with a image, the image will be shown. but for now, the image didn't show up. How do i fix it.
here's the code from views/post/index.blade.php
<tbody>
#foreach($posts as $post)
<tr>
<td>
<img src="{{ asset($post->image) }}" alt="">
</td>
<td>
{{ $post->title }}
</td>
<td>
<a href=" {{ route('categories.edit', $post->category->id) }}">
{{ $post->category->name }}
</a>
</td>
when the user post a image, the image will be stored in the linked storage that i've doing before. so here's the code from postscontroller.php if i change the 'posts' to 'post' it says error. because i didn't suitable for the function 'posts' that i write before.
public function store(CreatePostRequest $request)
{
// upload image omegalul
$image = $request->image->store('posts');
// create the post
$post = Post::create([
'title' => $request->title,
'description' => $request->description,
'content' => $request->content,
'image' => $image,
'published_at' => $request->published_at,
'category_id' => $request->category,
'user_id' => auth()->user()->id,
]);
if ($request->tags) {
$post->tags()->attach($request->tags);
}
// flash a message
session()->flash('success', 'Post created successfully');
// redirect the user
return redirect(route('posts.index'));
}
if you guys see any problems that i didn't appeared here, just ask me. Thank you guys so much
If you are working with Laravel Storage it's recommended to use the storage functions to store, get or delete media files.
Use theses commands to do it:
To store:
Storage::disk(config('filesystems.default'))->put($path, $file);
To get:
Storage::disk(config('filesystems.default'))->url($file);
To remove:
Storage::disk('local')->delete($path);
Your code will be like this:
<tbody>
#foreach($posts as $post)
<tr>
<td>
<img src="{{ Storage::disk(config('filesystems.default'))->url($post->image) }}" alt="">
</td>
<td>
{{ $post->title }}
</td>
<td>
<a href=" {{ route('categories.edit', $post->category->id) }}">
{{ $post->category->name }}
</a>
</td>
And:
public function store(CreatePostRequest $request)
{
// upload image omegalul
$image = Storage::disk(config('filesystems.default'))->put('posts', $request->image);
Note: Don't forget to check if your filesystem is setted like 'local' - if you are using local storage.
Related
I'm creating a shop, with an upload image in Laravel 9.
Everything is on my database, and my image is sent on file storage/app/public/nameofthefile/nameoftheimage.png.
But my image does not appear on my website like this :
As you can see on the console, the image has the correct direction I give you the code of my controller, my html page :
My CONTROLLER :
public function CreateArticleAction(Request $request) {
$request->validate([
'nom' => ['required'],
'description' => ['required'],
'prix' => ['required'],
'quantité' => ['required'],
'img' => ["required", "image", "mimes:jpg,jpeg,png,gif,svg,webp", "max:2048"],
]);
$path = $request->file('img')->store('boutique-storage', 'public');
$iduser = Session::get('iduser');
$article = new Boutique();
$article->iduser = $iduser;
$article->nom = request('nom');
$article->description = request('description');
$article->prix = request('prix');
$article->quantité = request('quantité');
$article->img = $path;
$article->save();
return redirect('/boutique');
}
My HTML page :
#foreach ($boutiques as $boutique)
<div class="produit">
<img src="{{ asset('storage/app/public/' . $boutique->img) }}" alt="Produit" class="img-produit" />
<p>{{ $boutique->nom }}</p>
<p>{{ $boutique->description }}</p>
<p>{{ $boutique->prix }}</p>
<p>{{ $boutique->quantité }}</p>
<div>
<a class="text-danger" href="/delete-article/{{ $boutique->idproduit }}">Supprimer</a>
<a class="text-primary" href="/FormUpdate/{{ $boutique->idproduit }}">Modifier</a>
</div>
</div>
#endforeach
And the DIRECTION :
I think you can see everything is ok, each data is sent correctly on the database but why my image does not appear ??
Thanks a lot !
Try run this command
php artisan storage:link
Ah ok so I forgot to add this on .env file : FILESYSTEM_DISK=public
Now with the command php artisan storage:link it works :D
replace
<img src="{{ asset('storage/app/public/' . $boutique->img) }}"
with
<img src="{{ asset($boutique->img) }}"
In my laravel project I tried to load images from database. But it displays the broken link. But when I inspect the image it shows the link. But when i clicked the link it shows 404 error.....
this is my codes
this is the code of view
.............
#extends('layouts.app')
#section('content')
<div class="d-flex justify-content-end">
Add Posts
</div>
<div class="card card-default">
<div class="card-header">Posts</div>
<div class="card-body">
<table class="table">
<thead>
<th>Image</th>
<th>Title</th>
</thead>
<tbody>
#foreach ($posts as $post)
<tr>
<td><img src="{{ asset($post->image) }}"></td>
<td>{{ $post->title }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endsection
.............
and this is the code of controller
public function store(CreatePostsRequest $request)
{
//Upload the image
$image = $request->image->store('public/img');
//create the post
Post::create([
'title' => $request->title,
'description' => $request->description,
'content' => $request->content,
'image' => $image
]);
//flash a message with seasson
session()->flash('success','Post created successfully');
//redirect user
return redirect(route('posts.index'));
}
and i linked the storage to the public folder. But still its not working
You need to give the absolute path and not use {{ asset... }}. Try something like this:
<img src="/xx/storage/app/public/FinalFolder/{{$post->image}}">
I'm learning Laravel 5.4 and for now I'm trying to upload image and show uploaded image in html.
It seems files are uploaded successfully but when I want to show the image in Html, there's 404 error for images and nothing will display.
This is my settings and code:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'desc' => 'required|string',
'width' => 'required|numeric',
'height'=> 'required|numeric',
'artist'=> 'required|exists:artists,id',
'photo' => 'required|file'
]);
$artistInfo = Artist::find($request->artist);
$fileExtension = $request->file('photo')->extension();
$originalFileName = $request->file('photo')->getClientOriginalName();
$filePath = Storage::putFile('public', $request->file('photo'), 'public');
Photo::create([
'name' => $request->name,
'origName' => $originalFileName,
'url' => $filePath,
'artist_id' => $request->artist,
'desc' => $request->desc,
'width' => $request->width,
'height' => $request->height
]);
return $filePath;//this for test
}
And my Blade:
#forelse($photos as $photo)
<tr>
<td>{{ $photo->name }}</td>
<td>
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
<td>{{ $photo->artist->name . ' ' . $photo->artist->family }}</td>
<td>{{ $photo->desc }}</td>
<td>{{ $photo->width }}</td>
<td>{{ $photo->height }}</td>
<td class="text-center">
<a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
<i class="material-icons">edit</i>
</a>
</td>
<td class="text-center">
<a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
<i class="material-icons">assignment_turned_in</i>
</a>
</td>
</tr>
#empty
<tr>
<td>There's no photo Yet</td>
<td>—</td>
<td>—</td>
<td>—</td>
<td>—</td>
</tr>
#endforelse
NOTE :
I made a symbol link to my public folder using php artisan storage:link and symlink works fine...
I changed this line in blade:
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
and tried these manners:
<img class="img" src="{{ asset(storage_path() . ($photo->url)) }}" alt="{{ $photo->name }}" /></td>
<img class="img" src="{{ storage_path() . ($photo->url) }}" alt="{{ $photo->name }}" /></td>
$photo->url has image filename for example jDHPOTh7iejFABANSzasNbBWJ09DqLbqJb8ymJhv.png
Would you please let me know which part is my problem?
Thanks in Advance
UPDATE :
I'm using PhpStorm Ide and when I download my public folder to my project -or synchronize this- my symbol link does not appear in public folder list...
This is ls from my public folder :
And this is my phpStorm project view AFTER download a fresh copy of public folders in my host:
And as a note :
I started to develop this website in windows at first and then I did move entire project to my linux remote server...
For anyone looking for answer to this question, please follow this link contains great answer
https://stackoverflow.com/a/30191854/417899
Thanks to all for participating, but in my case there was a tricky issue...
I was logged in to my server using root and when I did type php artisan storage:link, the symbol link was made by root as owner and it seems Apache didn't have permission to access this symbol link and because of that, the files was not accessible in http.
SOLUTION
I have a vds that serve 5 different websites, so
I did simply change the owner of symbol link's owner to my relative username and the problem solved...
chown -R usergroup:username /path/to/link
That's it...
I think the issue is here:
$filePath = Storage::putFile('public', $request->file('photo'), 'public');
here you are putting your file on public folder and here:
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
you are trying to get it from public/storage/. So change the path like:
<img class="img" src="{{ asset( 'public/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
and try again.
Note: Here uploaded images path is stored in public/image.extension, so fetching it from the same public directory
I have problem with displayin one picture in my home page.
Currently, I loaded some photos. I created a column in the database 'thumbnail'. Wants to do so, to load the photo - thumbnail where the column 'thumbnail
have record = 1.
My Homecontroller:
public function getHome(){
return View::make('pages.home')
->with('offers', Offer::take(8)->orderBy('created_at', 'DESC')->get());
}
my view:
#foreach($offers as $offer)
<div class="col-sm-3" style="border: 1px solid #AADFFF; height:350px; ">
#foreach($offer->photosofoffers as $photosofoffer)
<a href="{{ route('pages.view', $offer->id) }}">
<!--<a href="/pages/view/{{ $offer->id }}">-->
{{ HTML::image($photosofoffer->file, $photosofoffer->title, array( 'class'=>'feature', 'width'=>'240', 'height'=>'127')) }}
</a>
#endforeach
<h3><a href="{{ route('pages.view', $offer->id) }}">{{ $offer->title }}</h3>
<p> {{ $offer->description }} </p>
<h5>Availability: <span class="{{ Availability::displayClass($offer->availability) }}">
{{ Availability::display($offer->availability) }}
</span>
</h5>
<p>Cena <span>{{ $offer->price }}</span>
</p>
#endforeach
Where should I add something?
At the moment loads all the images of the announcement. He wants to load one image, where the 'thumbnail' = 1.
You can eager load the relations and apply a constraint. Something like:
Offer::with(array('photosofoffers' => function($q) {
$q->where('thumbnail', 1)->take(1);
}))->take(8)->orderBy('created_at', 'DESC')->get();
http://laravel.com/docs/5.0/eloquent
If you want to load only those offers that have a photo/thumbnail
Offer::with('photosoffers')->whereHas('photosofoffers', function($q) {
$q->where('thumbnail', 1)->take(1);
})->take(8)->orderBy('created_at', 'DESC')->get();
Im having problems trying to upload photos and any kind of file on my app, because the do upload but as .tmp files and they dont display properly on my view
1.-My form,Im trying to upload a member with name, group, email, description and a photo
{{Form::open(array('action' => 'AdminController#addMember','files'=>true)) }}
{{ Form::label('file','Agregar Imagen',array('id'=>'','class'=>'')) }}
{{ Form::file('file','',array('id'=>'','class'=>'')) }}
<br/>
{{Form::text('name','',array('class' => 'form-control','placeholder'=> 'Nombre'))}}
{{Form::text('group','',array('class' => 'form-control','placeholder'=> 'Cargo'))}}
{{Form::text('email','',array('class' => 'form-control','placeholder'=> 'Correo'))}}
{{Form::textarea('description','',array('class' => 'form-control','placeholder'=>''))}}
<!-- submit buttons -->
{{ Form::submit('Guardar') }}
<!-- reset buttons -->
{{ Form::reset('Reset') }}
{{ Form::close() }}
2.-My upload function in the controller
public function addMember()
{
$name = Input::file('file')->getClientOriginalName();
$newname = Input::file('file')->getFilename();
Input::file('file')->move(storage_path(),$name);
$subpath = storage_path();
$path = $subpath.'/'.$newname2;
$name2 = Input::get('name');
$email = Input::get('email');
$description = Input::get('description');
$group = Input::get('group');
DB::table('contactgroups')->insert(
array('group' => $group, 'name' => $name2, 'path' => $path, 'email' => $email, 'description' => $description)
);
$members = DB::table('contactgroups')->get();
return View::make('admin.members',['members' => $members]);
}
I know i should be using a Model to upload things on my database but that's not the problem right now
3.- My display view
#extends('layouts.main')
#section('content')
#foreach($members as $member)
<div class = "row fondue">
<h3><div class="col-md-12"><b><?=$member->name ?></b></div></h3>
<div class="col-md-4"> <img src="<?=$member->path ?>" alt="Image" class = "contact-img"></div>
<div class="col-md-4"><?=$member->description ?></div>
<div class="col-md-4"><?=$member->email ?></div>
</div>
#endforeach
#stop
and that's all...the info is saved in the database but the images are not showing properly on the view and, the files are uploaded as tmp files i dont know why
From the Laravel documentation
Moving An Uploaded File
Input::file('photo')->move($destinationPath);
Input::file('photo')->move($destinationPath, $fileName);
Source: http://laravel.com/docs/4.2/requests#files