Home > Enterprise >  Laravel 8 don't see picture
Laravel 8 don't see picture

Time:11-13

I try to create forum, In dashboard admin i try to show in category when click in eye for see picture, title and descritpion of category but don't see the picture. I don't understand why

single_category.blade.php


@section('content')
      <!--main content start-->
      <section id="main-content">
        <section class="wrapper">

              <!--overview start-->
          <div class="row">
            <div class="col-lg-12">
              <h3 class="page-header"><i class="fa fa-laptop"></i> Forum Categories</h3>
              <ol class="breadcrumb">
                <li><i class="fa fa-home"></i><a href="/dashboard/home">Home</a></li>
                <li><i class="fa fa-users"></i>Categories</li>
              </ol>
            </div>
          </div>

          <div class="row">

            <div class="col-lg-12 col-md-12">
              <div class="panel panel-default">
                <div class="panel-heading">
                  <h2><i class="fa fa-flag-o red"></i><strong>Forum Categories</strong></h2>
                  <div class="panel-actions">
                    <a href="/dashboard/home" class="btn-setting"><i class="fa fa-rotate-right"></i></a>
                    <a href="/dashboard/home" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
                    <a href="/dashboard/home" class="btn-close"><i class="fa fa-times"></i></a>
                  </div>
                </div>
                <div class="panel-body">
                    <div class="container">
                        <div class="row">
                            <div class="col-lg-3">
                                <h4>{{$category->title}}</h4>
                                <img src="{{asset('storage/images/categories/'.$category->image)}}">
                                <p>{!!$category->desc!!}</p>
                            </div>
                        </div>
                    </div>
                </div>
  
              </div>
  
            </div>
            
            </div>
            <!--/col-->
  
          </div>
  


        </section>


      </section>
      <!--main content end-->
@endsection 

categories.blade.php


@section('content')
      <!--main content start-->
      <section id="main-content">
        <section class="wrapper">

              <!--overview start-->
          <div class="row">
            <div class="col-lg-12">
              <h3 class="page-header"><i class="fa fa-laptop"></i> Forum Categories</h3>
              <ol class="breadcrumb">
                <li><i class="fa fa-home"></i><a href="/dashboard/home">Home</a></li>
                <li><i class="fa fa-users"></i>Categories</li>
              </ol>
            </div>
          </div>

          <div class="row">

            <div class="col-lg-12 col-md-12">
              <div class="panel panel-default">
                <div class="panel-heading">
                  <h2><i class="fa fa-flag-o red"></i><strong>Forum Categories</strong></h2>
                  <div class="panel-actions">
                    <a href="/dashboard/home" class="btn-setting"><i class="fa fa-rotate-right"></i></a>
                    <a href="/dashboard/home" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
                    <a href="/dashboard/home" class="btn-close"><i class="fa fa-times"></i></a>
                  </div>
                </div>
                <div class="panel-body">
                  <table class="table bootstrap-datatable countries">
                    <thead>
                      <tr>
                        <th>Title</th>
                        <th>Image</th>
                        <th>Description</th>
                        <th>View</th>
                        <th>Edit</th>
                        <th>Delete</th>
                        
                      </tr>
                    </thead>
                    <tbody>
                        @if (count($categories)> 0)
                            @foreach ($categories as $category)
                            <tr>
                                <td>{{$category->title}}</td>
                                <td>Image</td>
                                <td>{!!$category->desc!!}</td>
                                <td><a href="{{route('category',$category->id)}}"><i class="fa fa-eye text-success"></i></a></td>
                                <td><a href="#"><i class="fa fa-edit text-info"></i></a></td>
                                <td><a href="#" class="text-danger"><i class="fa fa-trash"></i>Delete</a></td>
                               
                              </tr>
                            @endforeach
                        @endif
                    </tbody>
                  </table>

                  {{ $categories->links() }}
                </div>
  
              </div>
  
            </div>
            
            </div>
            <!--/col-->
  
          </div>
  


        </section>


      </section>
      <!--main content end-->
@endsection 

CategoryController


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Category;
use Illuminate\Support\Facades\Session;

class CategoryController extends Controller
{
   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function index()
   {
       $categories = Category::latest()->paginate(20);

       return view('admin.pages.categories', \compact('categories'));
   }

   /**
    * Show the form for creating a new resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function create()
   {
       return view('admin.pages.new_category');
   }

   /**
    * Store a newly created resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @return \Illuminate\Http\Response
    */
   public function store(Request $request)
   {
       $request->validate([
           'title' => 'required',
           'image' => 'required',
           'desc' => 'required'
       ]);

       $category = new Category;
       $category->title = $request->title;
       $category->desc = $request->desc;
       $category->user_id = auth()->id();
       $category->image = "test1230"; 
       $category->save();
       Session::flash('message', 'Category Created Successfully');
       Session::flash('alert-class', 'alert-success');
       return back();
   }

   /**
    * Display the specified resource.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
   public function show($id)
   {
       $category = Category::find($id);
       return view('admin.pages.single_category', \compact("category"));
   }

   /**
    * Show the form for editing the specified resource.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
   public function edit($id)
   {
       //
   }

   /**
    * Update the specified resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
   public function update(Request $request, $id)
   {
       //
   }

   /**
    * Remove the specified resource from storage.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
   public function destroy($id)
   {
       //
   }
}

Model


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;
}

categories.blade.php

@extends('layouts.dashboard')

@section('content')
     <!--main content start-->
     <section id="main-content">
       <section class="wrapper">

             <!--overview start-->
         <div class="row">
           <div class="col-lg-12">
             <h3 class="page-header"><i class="fa fa-laptop"></i> Forum Categories</h3>
             <ol class="breadcrumb">
               <li><i class="fa fa-home"></i><a href="/dashboard/home">Home</a></li>
               <li><i class="fa fa-users"></i>Categories</li>
             </ol>
           </div>
         </div>

         <div class="row">

           <div class="col-lg-12 col-md-12">
             <div class="panel panel-default">
               <div class="panel-heading">
                 <h2><i class="fa fa-flag-o red"></i><strong>Forum Categories</strong></h2>
                 <div class="panel-actions">
                   <a href="/dashboard/home" class="btn-setting"><i class="fa fa-rotate-right"></i></a>
                   <a href="/dashboard/home" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
                   <a href="/dashboard/home" class="btn-close"><i class="fa fa-times"></i></a>
                 </div>
               </div>
               <div class="panel-body">
                 <table class="table bootstrap-datatable countries">
                   <thead>
                     <tr>
                       <th>Title</th>
                       <th>Image</th>
                       <th>Description</th>
                       <th>View</th>
                       <th>Edit</th>
                       <th>Delete</th>
                       
                     </tr>
                   </thead>
                   <tbody>
                       @if (count($categories)> 0)
                           @foreach ($categories as $category)
                           <tr>
                               <td>{{$category->title}}</td>
                               <td>Image</td>
                               <td>{!!$category->desc!!}</td>
                               <td><a href="{{route('category',$category->id)}}"><i class="fa fa-eye text-success"></i></a></td>
                               <td><a href="#"><i class="fa fa-edit text-info"></i></a></td>
                               <td><a href="#" class="text-danger"><i class="fa fa-trash"></i>Delete</a></td>
                              
                             </tr>
                           @endforeach
                       @endif
                   </tbody>
                 </table>

                 {{ $categories->links() }}
               </div>
 
             </div>
 
           </div>
           
           </div>
           <!--/col-->
 
         </div>
 


       </section>


     </section>
     <!--main content end-->
@endsection

database.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoriesTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('categories', function (Blueprint $table) {
           $table->id();
           $table->string('title');
           $table->text('desc');
           $table->string('image');
           $table->unsignedBigInteger('user_id');
           $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
           $table->boolean('is_deleted')->default(0);
           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists('categories');
   }
}

How can I resolve this? Or someone can show and explain my error. Thanks if someone give little help for resolve this

CodePudding user response:

From the code you provided, I guess that you are seeing the string "Image" instead of the real picture. Then the solution is really simple:

In categories.blade.php, replace <td>Image</td> by <td>{{$category->image}}</td>.

CodePudding user response:

There can always be several reasons why you don't see an image. The most common reason is that you have not yet set the symlink to the public folder. php artisan storage:link If this has happened, check:

First, check if you really have an image in your storage folder. I suspect not. Then output your blade @dd($category->image). What do you see there?

I haven't found a place in your controller where you store the image in the storage. with Storage::put or with the ->store() function. Please check.

  • Related