Home>

Environment: Windows10, XAMPP, HeidiSQL
Database: Table name: shyouhin Columns: id, name, price, gazou, updated_at, created_at

Database has profiles/Rolex Submariner. PNG and storage>app>profiles store images securely
It has been done.

I want to display the image, but it doesn't work.
I would appreciate it if you could teach me how to display it.

[Controller]

public function shyouhin (Request $request)
    {
        $data = new shyouhin ();
        $data->name = $request->name;
        $data->price = $request->price;
        $uploaded_image = $request->file ('gazou');
        if ($request->hasFile ('gazou')&&$uploaded_image->isValid ())
        {
            $file_name = $request->file ('gazou')->getClientOriginalName ();
            $path = $request->file ('gazou')->storeAs ('profiles', $file_name);
        }
        $data->gazou = $path;
        $data->save ();
        $validate_rule = [
            'name' =>'required',
            'price' =>'required',
        ];;
        $this->validate ($request, $validate_rule);
        return view ('shyouhin_add', ['msg' =>'Registration completed.']);

    }
    public function shyouhinlist (Request $request)
    {
        $items = DB :: select ('select * from shyouhin');
       return view ('shyouhin_list', ['items' =>$items]);
    }

[Blade]

<! DOCtYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>Shop</title>
</head>
<body>
Product list<br />
<br />
<table border = "2">
<tr>
<th>Products</th>
<th>Price</th>
<th>image</th>
</tr>
<form method = "post" action = "/ shop/delete">
@csrf
<tr>
<td>
@foreach ($items as $item)
<input type = "radio" name = "id" value = "{{$item->id}}">
{{$item->name}}<br />
@endforeach
</td>
<td>
@foreach ($items as $item)
{{$item->price}}<br />
@endforeach
</td>
<td>
@foreach ($items as $item)
 gazou}}"><br />
@endforeach
</td>
</tr>
</table>
<br />
<input type = "submit" value = "delete" id = "id">
<script>
var btn = document.getElementById ('id');
btn.addEventListener ('click', function ()
{
    window.confirm ('Are you sure I want to delete it?');
})
</script>
</form>
<button on click = "history.back ()">Back</button>
 Business management system 
</body>
</html>
  • Answer # 1

    "Profiles/Rolex Submariner.PNG"
    If it is saved as the character string.
    http://example.com/test/If it is accessed by something
    http://example.com/test/profiles/ Rolex Submariner.PNG
    I think I'm going to see it.
    Isn't it better to use the URL function of Laravel for the URL?
    You may also want to use symbolic links.

    * This is a little old article, but for reference only.
    I want to save an image file with Laravel

  • Answer # 2

    Change the save destination of the image [Controller]

    $uploaded_image = $request->file ('gazou');
            if ($request->hasFile ('gazou')&&$uploaded_image->isValid ())
            {
                $file_name = $request->file ('gazou')->getClientOriginalName ();
                $path = $request->file ('gazou')->storeAs ('public/gazou', $file_name);
            }


    Call image [blade]

    @foreach ($items as $item)
    <br />
    @endforeach


    * Only the specified image can be displayed yet, so we will correct it in the future.