简体   繁体   中英

Laravel make arguments optional in a File

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM