I want to upload images with laravel.

Asked 2 years ago, Updated 2 years ago, 49 views

When I tried to register the image on the new registration screen, I checked it with verification and
Failed to load resource: the server responded with a status of 404 (Not Found) This error occurred.
After thinking about various things, I think that the index.blade and the controller have not been able to deliver them.
I would appreciate it if you could point it out.
Also, while I was trying, I was able to confirm that the specified image will be reflected using asset etc. in index.blade.
As per the issue, we would like users to freely select images on the new registration screen.
Share the current notation.Additional information is supported.
Thank you in advance.

@extends('layouts.app')

@section('content')

<h1>Product Information List Screen</h1>

<a href="create">New Registration</a>

<table class="table table-striped">
 <thead>
  <tr>
   <th>id</th>
   <th>Product Images</th>
   <th>Product Name</th>
   <th>Price</th>
   <th>Inventory </th>
   <th> Manufacturer </th>
  </tr>
 </thead>
 <tbody>
  @foreach($products as$products)
  <tr>
   <td>{{$products->id}}</td>
   <td><img src="{asset('/storage/img'.$products->img_path)}}" width="25px">/td>
   <td>{{$products->product_name}}</td>
   <td>{{$products->price}}</td>
   <td>{{$products->stock}}</td>
   <td>{{$products->comment}}</td>
   <td><a href="class="btn btn-primary">Detailed view</a></td>
   <td><Button type="button" class="btn btn-danger">Delete</button></td>
  </tr>
  @endforeach
 </tbody>
<table>
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\products;

class productsController extensions Controller
{
    public function index()
    {
        $query=Products::query();
        // Get all cases
        // $users=$query->get();
        // Page Nation
        $products=$query->orderBy('id', 'desc')->paginate(10);
        return view('products.index') ->with('products',$products);
    }

    public function create()
    {
        //Forward to create
        return view('products.create');
    }

    public function store(Request$request)
    {
        $products=new Products();

        $img = $request->file('img_path');
        $path=$img->store('img','public');

        // Register Value
        $products->product_name = $request->product_name;
        $products->price=$request->price;
        $products->stock=$request->stock;
        $products->img_path=$request->img_path;
        // Save
        $products->save();

        // Redirect to List
        return redirect('products');
    }
}

Enter a description of the image here

php laravel

2022-09-30 10:29

1 Answers

@foreach($products as$products)
  <tr>
   <td>{{$products->id}}</td>
   <td><img src="{asset('/storage/img'.$products->img_path)}}" width="25px">/td>
   <td>{{$products->product_name}}</td>
   <td>{{$products->price}}</td>
   <td>{{$products->stock}}</td>
   <td>{{$products->comment}}</td>
   <td><a href="class="btn btn-primary">Detailed view</a></td>
   <td><Button type="button" class="btn btn-danger">Delete</button></td>
  </tr>
  @endforeach

Try modifying the $products as$products part of the foreach above to $products as$product and correcting the variable name inside.

@foreach($products as$product)
  <tr>
   <td>{{$product->id}}</td>
   <td><img src="{asset('/storage/img'.$product->img_path)}}" width="25px">/td>
   <td>{{$product->product_name}}</td>
   <td>{{$product->price}}</td>
   <td>{{$product->stock}}</td>
   <td>{{$product->comment}}</td>
   <td><a href="class="btn btn-primary">Detailed view</a></td>
   <td><Button type="button" class="btn btn-danger">Delete</button></td>
  </tr>
  @endforeach


2022-09-30 10:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.