图像未显示

问题描述 投票:0回答:2
public function index()
{
    //
    $categories = Category::with('children')->get();
    return view('dashboard.categories.index',compact('categories'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
    $categories = Category::all();
    return view('dashboard.categories.create',compact('categories'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
    $category = new Category();
    $category->title = $request->title;
    $category->content = $request->content;
    $filename = sprintf('thumbnail_%s.jpg',random_int(1, 1000));
    if ($request->hasFile('thumbnail')) 
     $filename = $request->file('thumbnail')->storeAs('images',$filename,'public');
 else
    $filename = null;
    $category->thumbnail = $filename;
    $category->parent_id = $request->parent_id;
    $save = $category->save();
    if ($save) {
        return redirect()->route('categories.index');
    }
}

这是控制器和索引功能,用于视图列表创建,用于表单存储,用于保存值

@extends('dashboard.layout')
@section('content')
 <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h2 class="h2">categories Section</h2>
        <div class="btn-toolbar mb-2 mb-md-0">
          <div class="btn-group mr-2">
            <a href="{{route('categories.create')}}" type="button" class="btn btn-sm btn-outline-secondary">Add New category</a>
          </div>
        </div>
      </div>

 @if(!$categories->isEmpty())
      <div class="table-responsive">
        <table class="table table-striped table-sm">
          <thead>
            <tr>
              <th>#</th>
              <th>Title</th>
              <th>Content</th>
              <th>Thumbnail</th>
              <th>Created At</th>
              <th>Updated At</th>
              <th>Children</th>
              <th>Action</th>
            </tr>
          </thead>
       <tbody>
        @foreach($categories as $category)
        <tr>
            <td>{{$category->id}}</td>
            <td>{{$category->title}}</td>
          <td>{{$category->content}}</td>
          <td><img src="{{asset($category->thumbnail)}}" width="100" height="100" /></td>
            <td>{{$category->created_at}}</td>
            <td>{{$category->updated_at}}</td>
            <td>
            @if(!$category->children->isEmpty())
            @foreach($category->children as $children)
            {{$children->title}}
            @endforeach
            @else
            {{'Parent Category'}}
            @endif
          </td>
            <td>
                <div class="btn btn-group" category="group" aria-label="Basic example">
                     <a category="button" href="{{route('categories.edit',$category->id)}}" class="btn btn-link">Edit</a>
                | <form method="post" action="{{route('categories.destroy',$category->id)}}">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-link">Delete</button>
                </form>
                <a category="button" href="{{route('categories.show',$category->id)}}" class="btn btn-link">Show</a>
            </td>
        </tr>
        @endforeach
       </tbody>
        </table>
      </div>
@else
<p class="alert alert-info">No categories Found...</p>
@endif
@endsection

这是视图

我已使用此代码,并且用于创建和查看创建功能的工作正常,但查看功能也有效,但是图像未显示任何内容,告诉我路径错误在哪里以及如何解决此问题

php laravel
2个回答
0
投票

尝试此

  <td><img src="{{asset('public/images/'.$category->thumbnail)}}" width="100" height="100" /></td>

OR

  <td><img src="{{asset('images/'.$category->thumbnail)}}" width="100" height="100" /></td>

0
投票

使用storage_path()

<img src="{{storage_path().'app/public/images/'.$category->thumbnail}}" width="100" height="100" />

并且如果您将存储路径与public路径(例如Link]链接在一起>

php artisan storage:link

<img src="{{asset('storage/images/'.$category->thumbnail)}}" width="100" height="100" />
© www.soinside.com 2019 - 2024. All rights reserved.