How to change the mark when there is no image (I want to change it to text)

Asked 1 years ago, Updated 1 years ago, 274 views

"For example, when there is no image, such as """" or ""replacement text"" when a user does not post an image."
I'm thinking about using text this time because the image might be heavy.

I'm in trouble because I can't find a reference article because I'm not stuck in the search.
I think it will be displayed after conditional branching and setting the return value in return...

Enter a description of the image here

PostsController.php

/**
     * Post Listing Action
     */
    public function index (Request$request)
    {
        $searchword=$request->searchword;

        $posts=Post::with(['comments','category'])#Query Adjustment
            ->orderBy('created_at','desc')
            ->fuzzyNameMessage($searchword)
            ->paginate(10); 
        
        US>return view('bbs.index',[ 
            'posts' = > $posts,
            'searchword' = > $searchword
        ]);
    }

    /**
    * Learn More
    */
    public function show (Request $request, $id)
    {
        $user=auth()->user();
        $post=Post::findOrFail($id);

        return view('bbs.show', compact('post'));
    }

index.blade.php

@foreach($posts as$post)
            <tr>
                <td>{{optional($post)->id}}</td>
                <td>{{optional($post->created_at)->format('Y.m.d')}}</td>
                <td>{{$post->user->name}}</td>
                <td>{{optional($post)->subject}}</td>
                <td><img src="{asset('/storage/'.$post->image_file)}}" width="150px">/td>
                <td>{!!nl2br(e(Str::limit($post->message,100)))!!}
                @ if($post->comments->count()>=1)
                    <p><span class="badge badge-primary">Comments: {{optional($post->comments)->count()}}} cases</span></p>
                @endif
                </td>
                <td class="text-nowrap">
                    <p><a href="{ action('PostsController@show', $post->id)}}" class="btn btn-primary btn-sm">Learn more</a>>/p>
                    @can('edit',$post)
                        <p><a href="{ action('PostsController@edit', $post->id)}}" class="btn btn-info btn-sm">Edit<a>>/p>
                        <p>
                            <form method="POST" action="{action('PostsController@destroy', $post->id)}}">
                                @csrf
                                @method('DELETE')
                                <button class="btn btn-danger btn-sm">Delete </button>
                            </form>
                        </p>
                    @endcan
                </td>
            </tr>
        @endforeach

php laravel

2022-09-30 21:56

1 Answers

$post->image_file answers assuming it contains the image path

If the image_file is empty (or NULL) in View, it is good to display alternative text.

index.blade.php

{--omitted--}}
<td>{{optional($post)->subject}}</td>
<td>
  @ if(empty($post->image_file))
    No image
  @else
    <img src="{{asset('/storage/'.$post->image_file)}}"width="150px">
  @endif
</td>
<td>{!!nl2br(e(Str::limit($post->message,100)))!!}
{{-- Omitted --}}


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.