Laravel make arguments optional in a File - php

<div class="images section clearfix">
<h4>Images</h4>
{!! Form::_image("image_id", "Logo", (isset($_tournament))? $_tournament->logo : null) !!}
{!! Form::_image("image_large_id", "Large Logo", (isset($_tournament))? $_tournament->logoLarge : null) !!}
{!! Form::_image("image_square_id", "Square Logo", (isset($_tournament))? $_tournament->logoSquare : null) !!}
#endif
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-6">
<p>
{!! Form::hidden($name, null, ["id" => $name]) !!}
#if(isset($image))
<img width=100 id="{{ $name }}" src="{!! staticFileUrl('img', imagePath($image->folder, $image->filename)) !!}" />
#else
<img width=100 id="{{ $name }}" src="" />
#endif
</p>
<p>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#media_select" data-fieldname="{{ $name }}">Select {{ $display_name }}</button>
<button type="button" class="btn btn-default" id="clear-media">Clear {{ $display_name }}</button>
</p>
</div>
</div>
<script type="text/javascript">
$("button#clear-media").click(function() {
$form_element = $(this).parents("div.form-group");
$form_element.find("input#{{ $name }}").val(null);
$form_element.find("img#{{ $name }}").attr("src", "");
});
</script>
I got a thrown into a laravel project and I have no experience in PHP so I have no idea to fix this.
I need to add a default image for these fields and I just wonder is there a way to make all arguments optional staticFileUrl('img', imagePath($image->folder, $image->filename) optional so I can write something like this staticFileUrl('img', imagePath("random string", "other random string")?

Those already are variables based on the values set in the $image model.
If all you need is a default, change the #else
#if(isset($image))
<img width="100" id="{{ $name }}" src="{!! staticFileUrl('img', imagePath($image->folder, $image->filename)) !!}" />
#else
<img width="100" id="{{ $name }}" src="http://www.example.org/your-image.jpg" />
#endif
It currently displays nothing if there is no image object. Adding the src and an image will display that as your default unless it has an image.

Related

Laravel can't find the second passed parameter

I'm passing two parameters to my ShopController. one is a lang parameter that is in a route group as a prefix for all routes and the second one is a slug passed through a link.
Here's my controller:
function show($lang, $slug)
{
$product = Product::where('slug', $slug)->first();
return view('shop_show')->with(['product' => $product]);
}
If I pass only one parameter the function takes it as the prefix of the route group, aka the {lang} parameter and returns this error:
Attempt to read property "name" on null. so I have to pass two.
I die and dumped both $lang and $slug parameters and they are showing the correct values. $lang is "en" and $slug is "laptop-1" or any other slug I pass in. however after going through the function, it returns an error:
Missing required parameter for [Route: shopShow] [URI: {lang}/shop/{product}] [Missing parameter: product].
This is the route:
Route::group(['prefix' => '{lang}'], function () {
Route::get('/shop/{product}', [ShopController::class, 'show'])->name('shopShow');
});
this is the URL of the page:
http://127.0.0.1:8000/en/shop/laptop-9
Any ideas?
UPDATE: Here's my shop_show.blade.php view file:
<x-layout :title="$product->name">
<section class="navbar py-2 border-bottom border-2" style="background-color: #eee; font-weight: 500">
<div class="container w-75">
<ul class="breadcrumb">
<li class="breadcrumb-item">
<a class="text-decoration-none" href="{{ route('landingPage', App::getLocale()) }}">Home</a>
</li>
<li class="breadcrumb-item">
<a class="text-decoration-none" href="{{ route('shopIndex', App::getLocale()) }}">Shop</a>
</li>
<li class="breadcrumb-item">
<a class="text-muted text-decoration-none" href="{{ route('shopShow', ['product' => $product->slug, 'lang' => App::getLocale()]) }}">{{$product->name}}</a>
</li>
</ul>
</div>
</section>
<section class="my-5">
<div class="container w-75 d-flex justify-content-between align-items-start">
<div class="w-50">
<div>
<div>
<img id="currentImage" class="active border border-2 rounded-0 p-5 w-75" src='{{ $product->image && file_exists('storage/' . $product->image) ? asset('storage/' . $product->image) : asset('images/not-found.jpg') }}' alt="">
</div>
<div class="product-images">
<div class="product-thumbnails selected">
<img class="w-75" src='{{ $product->image && file_exists('storage/' . $product->image) ? asset('storage/' . $product->image) : asset('images/not-found.jpg') }}' alt="">
</div>
#if ($product->images)
#foreach (json_decode($product->images, true) as $image)
<div class="d-flex align-items-center product-thumbnails">
<img class="w-75" src="{{ file_exists('storage/' . $image) ? asset('storage/' . $image) : asset('images/not-found.jpg') }}" alt="">
</div>
#endforeach
#endif
</div>
</div>
</div>
<div class="card border-0 w-50">
<div>
<h3 class="mb-5">
{{$product->name}}
</h3>
<span class="text-muted" style="font-weight: 500">
{{ $product->details }}
</span>
<div class="h3" style="font-weight: 700">
${{$product->price / 100}}
</div>
<p class="my-3">
{!! $product->description !!}
</p>
<form action="{{ route('cartStore', App::getLocale()) }}" method="POST">
#csrf
<input type="hidden" name="name" value="{{$product->name}}">
<input type="hidden" name="price" value="{{$product->price}}">
<input type="hidden" name="id" value="{{$product->id}}">
<button class="btn py-2 px-3 border border-2 rounded-0 border-secondary" type="submit">Add to Cart</button>
</form>
</div>
</div>
</div>
</section>
#include('partials/might_also_like')
<script>
const currentImage = document.querySelector('#currentImage');
const images = document.querySelectorAll('.product-thumbnails');
images.forEach((element) => element.addEventListener('click', thumbnailClick));
function thumbnailClick(e) {
currentImage.classList.remove('active');
currentImage.addEventListener('transitionend', () => {
currentImage.src = this.querySelector('img').src;
currentImage.classList.add('active');
})
thumbnails.forEach((element) => element.classList.remove('selected'));
this.classList.add('selected');
}
</script>
SOLVED: There was a link in a component within a component within my layout component (3 times nested) that passed only one param, I fixed it by adding another param.
It's always a better idea to do localization before defining your routes so that you notice an error immediately instead of having to spend hours going through all your view files.

Too few arguments to function App\Http\Controllers\Home\ClientLogoController::EditClientImage() Controller Laravel

I am using Laravel 9 and I am getting this error I tried various methods to fix this issue but I'm stuck on this . Please help me.
Here is my code:
#php($i=1)
#foreach($client_image as $item)
<tr>
<td>{{ $i++ }}</td>
<td>
<img style="width:60px;height:50px;" src="{{ asset($item->client_logo_images) }}"/>
</td>
<td>
<a href="{{ route('edit.client.logoimage',$item->id) }}">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a class="btn btn-danger sm" href="">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
#endforeach
I have passed Id with the route and capture. Here is the code. :-
Route::get('/edit/client/image/{id}','EditClientImage')->name('edit.client.logoimage');
And this is my controller code .
public function EditClientImage($id) {
$client_logo=ClientLogoImage::findOrFail($id);
return view('admin.home.edit_client',compact('client_logo'));
}
and an error is showing in line 51. This is my line 51. public function EditClientImage($id)
and I am redirecting it to edit_client Here is the code for edit_client 
<form method="post" action="{{ route('update.client.image') }}" enctype="multipart/form-data">
#csrf
<input name="id" type="hidden" value="{{ $client_logo->id }}" >
<div class="card">
<div class="card-header">
<h3 class="card-title">Edit Profile</h3>
</div>
<div class="card-body">
<div class="row">
<div class="form-group">
<label for="formFile" class="form-label mt-0">Profile Image Upload</label>
<input name="client_image" id="image" class="form-control" type="file" >
</div>
</div>
<button class="btn btn-primary" type="submit">Update</button>
</div>
</form>
Thank You
<a href="{{ route('edit.client.logoimage', ['id' => $item->id]) }}">
You need to name it as id...
the problem is with the way you are using name routing:
if you are using routed names you need to pass the parameter the same as the route:
Route::get('/edit/client/image/{id}','EditClientImage')>name('edit.client.logoimage');
in this, you have the id parameter and your link have to be as:
<a href="{{ route('edit.client.logoimage', ['id' => $item->id]) }}">
<i class="glyphicon glyphicon-edit"></i>
</a>
the other way you can use is just use the URL:
<a href="{{ url('/edit/client/image/' . $item->id) }}">
<i class="glyphicon glyphicon-edit"></i>
</a>

Remove entity not working inside the form easy admin bundle

I have this code :
{{ form_start(form, {'attr': {'class': 'form-vertical edit-form', 'id': 'new-users-form', 'data-view': 'new'}}) }}
<div class="row">
<div class="field-group col-xs-12 col-sm-4">
<div class="box box-default">
<div class="box-body " id="box-body-_easyadmin_form_design_element_0">
<div class="row">
<div class="col-xs-12 ">
<div class="form-group field-text">
{{ form_row(form.value, {'attr': {'class': 'form-control' }}) }}
</div>
</div>
<div class="col-xs-12 ">
<div class="form-group field-text">
{{ form_row(form.comment, {'attr': {'class': 'form-control' }}) }}
</div>
</div>
<div class="col-xs-12 form-actions">
<div class="form-group">
<div id="form-actions-row">
<button type="submit" class="btn btn-primary action-save pull-right">
<i class="fa fa-check"></i> {{ 'general.buttons.confirm'|trans }}
</button>
<a class="btn btn-danger pull-right action-delete" style="margin-right: 1%" title=""
href="{{ path('admin', {'entity' : 'User', 'action' : 'delete', 'id' : entity.id}) }}" target="_self">
<i class="fa fa-trash-o"></i>
{{ 'general.buttons.delete'|trans }}
</a>
<a name="list" class="btn btn-secondary pull-right" title=""
href="{{ path('admin', {'entity' : 'User', 'action' : 'list'}) }}" target="_self">
{{ 'general.buttons.back_to_liste'|trans }}
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{{ form_widget(form._token) }}
{{ form_end(form,{'render_rest': false}) }}
Add and Back links works just fine. The only problem is with delete button. When I try to delete I'm redirect to the liste of User entity whitout delete the current User. I don't have any errors. Please help me. Thx in advance

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php)

when I try to pass variable data to view I get this error, I can't find any document about this problem
My controller(CarouselController.php)
public function index()
{
$carousels = Carousel::orderBy('created_at', 'asc')->paginate(12);
return view('admin.carousels.index')->withCarousels($carousels);
}
My view(index.blade.php)
<div class="row">
<div class="col-md-9">
<h1>All Images</h1>
</div>
<div class="col-md-3">
Create New carousel
</div>
<div class="col-md-12">
<hr>
</div>
</div>{{-- end of the row --}}
<div class="row">
<div class="col-md-12">
<div class="row">
#foreach($carousels as $photo)
<div class="col-xs-6 col-md-3">
{!! Form::open( array('route'=>array('carousels.destroy', $carousels->id),'method'=>'DELETE')) !!}
{!! Form::submit('Delete', array('class'=>"btn btn-danger btn-sm tours-delete tour-index-delete"))!!}
{!! Form::close() !!}
<a href="{{ url($photo->path) }}" class="thumbnail" data-lity>
<img class="img-responsive" src="{{ $photo->path }}" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="text-center">
{!! $carousels->links(); !!}
</div>
</div>
You foreach loop contains the code:
$carousels->id;
which seems to be getting a single object from collection, which isn't the right way, you should try this:
#foreach($carousels as $carousel)
<div class="col-xs-6 col-md-3">
{!! Form::open( array('route'=>array('carousels.destroy', $carousel->id),'method'=>'DELETE')) !!}
{!! Form::submit('Delete', array('class'=>"btn btn-danger btn-sm tours-delete tour-index-delete"))!!}
{!! Form::close() !!}
<a href="{{ url($carousel->path) }}" class="thumbnail" data-lity>
<img class="img-responsive" src="{{ $carousel->path }}" alt="">
</a>
</div>
#endforeach
Hope this helps!

laravel/blade cycle in template

I need your help. Here is my template file. How can I make to work that template? Main problem is that I don't know how to change div class values. Now I can make working only <div class="thumb-left"> but I also need <div class="thumb-middle"> and <div class="thumb-right">.
Thank you.
#extends('_layouts.main')
#section('content')
<div id="content-section">
#foreach ($uploads as $upload)
<div class="thumb-left">
<img alt="{{ $upload->image_title }}" title="{{ $upload->image_title }}" src="/files/images/{{ $upload->imagefile }}" height="250" width="300">
<div class="thumb-info">
{{ $upload->image_title }}
<a class="cat" href="#">PHOTOS</a>
</div>
</div>
#endforeach
<div class="thumb-middle">
<img alt="sunset" title="Sunset HDR" src="img/thumb.jpg" height="250" width="300">
<div class="thumb-info">
{{ $upload->image_title }}
<a class="cat" href="#">PHOTOS</a>
</div>
</div>
<div class="thumb-right">
<img alt="sunset" title="Sunset HDR" src="img/thumb.jpg">
<div class="thumb-info">
City Sunset HDR
<a class="cat" href="#">PHOTOS</a>
</div>
</div>
</div>
#stop
You could do:
#foreach ($uploads as $upload)
<div class="thumb-{{ array('left', 'middle', 'right')[($loop->iteration-1)%3] }}">
<img alt="{{ $upload->image_title }}" title="{{ $upload->image_title }}" src="/files/images/{{ $upload->imagefile }}" height="250" width="300">
<div class="thumb-info">
{{ $upload->image_title }}
<a class="cat" href="#">PHOTOS</a>
</div>
</div>
#endforeach
The $loop variable is a build in blades variable - check the docs:
When looping, a $loop variable will be available inside of your loop.
This variable provides access to some useful bits of information such
as the current loop index and whether this is the first or last
iteration through the loop. For instance: $loop->iteration - the current loop
iteration (it starts at 1).
You probably want to change your foreach to also use a key, so you can know at which index in your array you are:
<div id="content-section">
#foreach ($uploads as $index => $upload)
<div class="thumb-left">
<img alt="{{ $upload->image_title }}" title="{{ $upload->image_title }}" src="/files/images/{{ $upload->imagefile }}" height="250" width="300">
<div class="thumb-info">
{{ $upload->image_title }}
<a class="cat" href="#">PHOTOS</a>
</div>
</div>
#endforeach
</div>
Now, if I understand properly, what you're looking for is to switch from thumb-left, to thumb-middle and then finally thumb-right, and then loop again. You could use modulo to assign your class name based on the current index. This is untested code but you could probably use something along the lines of:
<div id="content-section">
{{ $classnames = array("thumb-left", "thumb-middle", "thumb-right"); }}
#foreach ($uploads as $index => $upload)
<div class="{{ $classnames[$index % count($classnames)] }}">
<img alt="{{ $upload->image_title }}" title="{{ $upload->image_title }}" src="/files/images/{{ $upload->imagefile }}" height="250" width="300">
<div class="thumb-info">
{{ $upload->image_title }}
<a class="cat" href="#">PHOTOS</a>
</div>
</div>
#endforeach
</div>

Categories