using parameterized function in laravel - php

I am very new to laravel. The route is as:
Route::get('/imagess','cont#imagess');
Route::get('/imagess/details/{id}','cont#image_details');
the functions inside the controller files are:
public function imagess() {
return view('imagess');
}
public function image_details($id) {
return view('image_details');
}
the view file is imagess.blade.php
#extends('inside.main')
#section('content')
<img src="{{asset('images/play.png')}}" height="100px" width="100px">View image 1<br>
<img src="{{asset('images/eat.png')}}" height="100px" width="100px">View image 2<br>
<img src="{{asset('images/sleep.png')}} height="10px" width="10px"">View image 3<br>
tihsajf
<img src="{{asset('images/read.png')}}" height="100px" width="100px">View image 4<br>
#endsection
What should be my imagess_details.blade.php to display basic details of each of those images? I am not trying to display exact details using database. just displaying anything. for eg. display 'hello' if i click image1, 'begineer' if i click image2, 'here' if i click image3 and so on..
if i make images_details as follows. it displays 'hello' whether i click image1, image2, or any other image.
#extends('inside.main')
#section('content')
<h2>hello</h2>
#endsection

If I understood you correctly, you're asking about transferring data from controller to the view:
public function image_details($id) {
$images = Images::all(); // get information about all images from database
return view('image_details')-with('images', $images); // transfer data to the view
}
The view gets all data inside $images collection, now you can iterate and get data:
#foreach ($images as $image) // iterate all images
<img src="{{ $image->src }}" alt="{{ $image->altText }}" height="100px" width="100px">{{ $image->name )}}<br>
#endforeach

Related

laravel: looping images folder with index value

i am trying to show an image from the images folder however the image is not showing although i am able to see it in an array if i die and dump the "$image". The images are located in "public/images" folder
blade file
<div class="gallery">
#foreach($posts as $post)
<div class="round-post">
#foreach ($post->images as $image)
........
#endforeach
#endforeach
</div>[![enter image description here][1]][1]
when i <img src="/images/ <?php dd($image[0]); i get
this bunch of code get your images that exist in your images folder:
public function index(){
$images = \File::allFiles(public_path('images'));
return View('pages.form')->with(array('images'=>$images));
}
this will show the images in your blade.
<ul>
#foreach ($images as $image)
<li style="width:80px;display:inline-block;margin:5px 0px">
<input type="checkbox" name="images[]" value="{{$image->getFilename()}}"/>
<img src="{{ asset('images/' . $image->getFilename()) }}" width="50" height="50">
</li>
#endforeach
</ul>
for more information you can read this article.
Good luck my friend!

Displaying image from database in grid - laravel

I have my image path in my database and now, i am trying to get the image into my html grid. But when i try to fetch the image, i get an error
Property [path] does not exist on this collection instance.
But then, there is path attribute in my table in the database. Has it got to be an error from my models? My model seems right to me. Anyway what am i not doing right here, please?
Thanks for your concern
HTML
<figure>
<div class="snipcart-item block">
<div class="snipcart-thumb">
<img src="{{$product->images->path}}" alt=" " class="img-responsive" />
<p>{{$product->name}}</p>
<h4>GH₵ {{ number_format($product->price,2) }} </h4>
</div>
<div class="snipcart-details top_brand_home_details">
<form action="#" method="post">
<fieldset>
</fieldset>
</form>
</div>
</div>
</figure>
Image
public function products()
{
return $this->belongsTo(Product::class);
}
Product_images
public function images()
{
return $this->hasMany(Image::class);
}
You need to make a foreach as it's give you a collection.
Hence, try to display it like:
#foreach($product->images as $image)
#if($image->path)
<img src="{{ $image->path }}" alt=" " class="img-responsive" />
#else
<p>No image to display!</p>
#endif
#endforeach
Hope this will helps you!
This happens because the Product model hasMany Images.
In order to reach the path property you should do this:
#foreach($product->images as $image)
{{$image->path}}
#endforeach
Or do this in your Product Model and change your database structure
public function image()
{
return $this->hasOne(Image::class);
}
I hope it helps.
If your path does not any issue, the following is the correct way to display an image in Laravel blade template.
<img src="{{URL::asset('directory/name_of_image_file.file_format')}}">
You don't have field path in DB images
You code must be:
<img src="{{$product->images->[you field in Db images]}}" alt=" " class="img-responsive" />
HTML
#foreach($products as $product)
<figure>
<div class="snipcart-item block">
<div class="snipcart-thumb">
<img src="{{$product->images->first()->path}}" alt=" " class="img-responsive" />
<p>{{$product->name}}</p>
<h4>GH₵ {{ number_format($product->price,2) }} </h4>
</div>
<div class="snipcart-details top_brand_home_details">
<form action="#" method="post">
<fieldset>
</fieldset>
</form>
</div>
</div>
</figure>
#endforeach
<a href="{{ route('product.view', $product->slug)}}">
<img src="{{$product->images->first()->path}}" alt=" " class="img-responsive" />
</a>
$product->images is a collection, you need to get the image out of the collection. for now I have used the first() which will fetch the first image, you may want to loop through the collection depending on your scenario. You may want to read about an answer have written here

Laravel edit dropzone

I saved files(images) by DropzoneJS and I can get them everywhere I like, but my problem is how to edit them. I go to my products edit page I can see my image but have no idea how to edit them such as (delete some of them or add to them)
here is my code to get them:
public function edit($id)
{
$product = Product::findOrFail($id);
$images = DB::table('images')->where('imageable_id', '=', $product->id)->get();
// rest of codes
}
and I show my images in edit page like:
#foreach($images as $test)
<img src="{{url('/')}}/images/{{$test->name}}" alt="test" width="100" height="100">
#endforeach
screenshot
UPDATE
#linktoahref way works for me if I want to delete each of images, and he suggested to make new uploader for adding images into my product on edit page, not bad idea totally but it's not what I'm exactly looking for.
What I try to achieve is to use DropZone itself to return my exist
images of each product and be able to remove or add to it.
I read many questions,articles, etc. And I get that I have to use mockFile which i have no idea about it. Would be appreciate if someone can help me to get this done.
Thanks.
For DELETE, you could add a button next to the image that would fire the destroy method of the respective controller
#foreach($images as $test)
<img src="{{ url('/') }}/images/{{ $test->name }}" alt="test" width="100" height="100">
<a href="#" class="btn btn-danger"
onclick="event.preventDefault();
document.getElementById('image-{{ $test->id }}').submit();">
DELETE
</a>
<form id="image-{{ $test->id }}"
action="{{ route('images.destroy', ['id' => $test->id]) }}"
method="POST" style="display: none;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
#endforeach
and handle the deletion of image in your ImageController's destroy method (or your custom controller's method, make sure the method types are same)
public function destroy(Image $image)
{
// Destroy the Image
Storage::delete($image->name);
// Redirect Back
return redirect()->back();
}

How to get an image on the desired page by clicking on that image using Laravel?

I am working on the laravel 5.0. I am getting an error: "trying to get property of non-object".I want to get an image on the desired page by clicking on that particular image. There are so many images on the page, i just want after clicking on any image, it will be appear on the desired page.
here is my code:
My Routes:
Route::get('/image/big/{id}' ,[
'uses'=>'GalleryController#bigimage',
'as'=>'bigimage'
]);
My controller:
public function bigimage($id){
$images=image::findorFail($id);
return view('bigimage' , ['images'=>$images]);
}
My view from where i am calling the required Route:
#foreach($gallery->images as $image)
<li>
<a href="{{ URL('/image/big/'.$image->id) }}">
<img id="jumboimage2" src="{{ url($image->file_path) }}"></a>
</li>
<li>
<a href="{{ URL('/image/delete/'.$image->id) }}" id="margin">
<span id="margin" class="glyphicon glyphicon-remove-sign"></span></a>
</li>
#endforeach
My view where i want to get the required image:
<div class="row">
<section class="col-md-3">
#foreach($images as $image)
{{$image->id}}
#endforeach
</section>
</div>
try this one out
$image = image::findOrFail($id);
return view('bigimage' , ['image' => $image])
$images=image::findorFail($id); should be $images=image::findOrFail($id);.
findOrFail() returns a single model, i.e. one image, so I would have:
$image = image::findOrFail($id);
return view('bigimage' , ['image' => $image]);
You don't show where image:: comes from, however usually this is from a facade so is Image:: with a capital.
The view would then not have the #foreach loop as you'd only have a single image.
<div class="row">
<section class="col-md-3">
{{$image->id}}
</section>
</div>
You also need to elaborate on this to get from the id to an image but I'm assuming that's just not shown.
If you were able to give some detail about which line is causing the error this would make it easier to diagnose, as indicated by Maraboc there could be an issue with $gallery as well.

How can I concatenate to the end of a route using Laravel?

I'm using Laravel to create a project.
Is there a way to concatenate to the end of a url using {{ route('routename') }} in an <a> tag?
I created a list of thumbnail images that link to a collection of fullsize images using a foreach loop.
#foreach ($images as $image)
<a href="{{ route('images') }}"> // link to full size image collection
<img src="{{ $image->url }}">// thumbnail image
</a>
#endforeach
{{ route('image') }} is a link to a paginated page that only contains one image per page.
so the url for the first image is my.site/images?page=1, the second is my.site/images?page=2 and so on and so on.
I'm wanting to route the user to the correct page=# according to the thumbnail they click on so they don't have to start form the beginning of the fullsize images each time the click a link from the thumbnails.
Im thinking I can use a foreach loop to increment a variable by 1 and concatenate that on the end of each href to get the correct page number but I'm not sure how I can do this using Laravel?
Im wanting my resulting html to look like this
<img src="thumbnail1.jpg">
<img src="thumbnail2.jpg">
<img src="thumbnail3.jpg">
<img src="thumbnail4.jpg">
<img src="thumbnail5.jpg">
...
Does anyone know a way?
The method you found out yourself works perfectly fine, but there is a more elegant one. All parameters you pass as second argument that are no actual route parameters (like {page}) will be appended as query string.
{{ route('images', ['page' => $n]) }}
I was able to make this work by using this code
<?php $n = 1; ?>
#foreach ($images as $image)
<a href="{{ route('images') . '?page=' . $n }}"> // link to full size image collection
<img src="{{ $image->url }}">// thumbnail image
</a>
<?php $n++; ?>
#endforeach

Categories