我被困了 2 天,可能是一件显而易见的事情 - PHP + Laravel 图片更新
I'm stuck for 2 days with probably an obvious thing - PHP + Laravel image update
我开始创建一个电子商务平台,但我被一个问题困扰了 2 天无法解决。这可能是显而易见的,但我就是找不到解决方案。你能帮我解决这个问题吗?我收到此错误:
Missing required parameters for [Route: images.update] [URI: dashboard/images/{image}]. (View: C:\xampp\htdocs\ecommerce\ecommerce\resources\views\website\admin\product_image\update.blade.php)
update.blade.php
@extends('website.admin.layouts.main')
@section('content')
<div class="col-md-12 col-sm-12 ">
<div class="x_panel">
<div class="x_title">
<h2>Zaktualizuj obraz</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<br />
<form id="updateimage-form" data-parsley-validate="" class="form-horizontal form-label-left" novalidate="" enctype="multipart/form-data" method="POST" action="{{route('images.update', $productImage->id)}}">
@csrf
@method('PUT')
<div class="item form-group">
<label class="col-form-label col-md-3 col-sm-3 label-align" for="first-name">Produkt<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 ">
<select class="form-control" name="product_id">
@foreach ($product as $prodcat)
<option value="{{$prodcat -> id}}" name="product_id">{{ $prodcat -> product_name }}</option>
@endforeach
</div>
</div>
<div class="ln_solid"></div>
<div class="item form-group">
<label class="col-form-label col-md-3 col-sm-3 label-align" for="first-name">Nazwa obrazu<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 ">
<input type="text" id="img_title" name="img_title" placeholder="Image Title" value="{{ $productImage -> image_name }}" required="required" class="form-control ">
</div>
</div>
<div class="item form-group">
<label class="col-form-label col-md-3 col-sm-3 label-align" for="first-name">Wgraj obraz<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 ">
<input type="file" name="img" id="img" onchange="fileSelected();"/>
</div>
</div>
<div class="item form-group">
<div class="col-md-6 col-sm-6 offset-md-3">
<button class="btn btn-primary" type="reset">Wyczyść</button>
<button type="submit" class="btn btn-success">Zaktualizuj obraz</button>
</div>
</div>
</form>
</div>
</div>
</div>
@endsection
models\ProductImage.php
<?php
namespace App\Models\models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProductImage extends Model
{
protected $fillable = [
'product_id',
'image_name',
'image',
'slug',
'status'
];
public function product()
{
return $this -> belongsTo('App\Models\models\Product');
}
}
ProductImageController.php
<?php
namespace App\Http\Controllers;
use App\Models\models\Product;
use App\Models\models\ProductImage;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ProductImageController extends Controller
{
public function index()
{
$productImage = ProductImage::all();
return view('website.admin.product_image.index', compact('productImage'));
}
public function create()
{
$product = Product::all();
return view('website.admin.product_image.create', compact('product'));
}
public function store(Request $request)
{
$slug = Str::slug($request->image_name, '-');
$image = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $image);
ProductImage::create([
'image_name'=>$request->image_name,
'image'=>$image,
'product_id'=>$request->product_id,
'slug'=>$slug,
]);
return redirect()->route('images.index');
}
public function show(ProductImage $ProductImage)
{
//
}
public function edit(ProductImage $productImage)
{
$product = Product::all();
return view('website.admin.product_image.update',compact('productImage','product'));
}
public function update(Request $request, ProductImage $productImage)
{
$slug=Str::slug($request->image_name,'-');
if($request->image)
{
$image = time() .'.'. $request -> image -> extension();
$request->image->move(public_path('images'), $image);
}
else
{
$image=$productImage->image;
}
$productImage->update([
'image_name'=>$request->image_name,
'image'=>$image,
'product_id'=>$request->product_id,
'slug'=>$slug,
]);
return redirect()->route('images.index');
}
public function destroy(productImage $productImage)
{
$productImage->delete();
return redirect()->route('images.index');
}
}
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('website.shop.layouts.main');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/dashboard', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard.index');
Route::resource('/dashboard/categories', 'App\Http\Controllers\ProductCategoryController');
Route::resource('/dashboard/product', 'App\Http\Controllers\ProductController');
Route::resource('/dashboard/images', 'App\Http\Controllers\ProductImageController');
action="{{route('images.update', $productImage->id)}}"
是调用 route()
辅助函数的不正确方式。考虑以下片段;
action="{{route('images.update', [
'image' => $productImage->id
])}}"
有关 route()
助手的更多详细信息,请参阅此 link; https://laravel.com/docs/8.x/helpers#method-route
update.blade.php
<form method="post" action="{{route('images.update', $productImage->id)}}" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="form-group">
<label for="filename">Select your Image</label>
<input type="file" name="image" class="form-control">
<button type="submit" class="btn btn-primary btn-lg">Update</button>
</div>
</form>
ProductImageController.php
<?php
namespace App\Http\Controllers;
use App\Models\models\ProductImage;;
use Illuminate\Http\Request;
class DocumentController extends Controller
{
public function update(Request $request, $productId){
$product = ProductImage::findOrFail($productId);
if($request->hasfile('image'))
{
$file = $request->file('image');
$name=$file->getClientOriginalName();
$file->move(public_path().'/uploads/ProductImages/', $name);
$file = $name;
$product->image = $file;
}
$product->save();
return redirect()->back();
}
}
这里没有发生模型绑定,而是发生了依赖注入。要进行隐式路由模型绑定,您必须将方法的类型提示参数与路由参数的名称相匹配。在你的例子中,路由参数被命名为 image
,而不是 productImage
。因此,您将获得 ProductImage
的空新实例,而不是路由模型绑定的替换实例。
更改控制器的参数以匹配路由参数:
vvvvv
public function edit(ProductImage $image)
除非您想覆盖路由中使用的参数,否则您需要在所有需要隐式绑定的方法中更改此项。
vvvvv
public function update(Request $request, ProductImage $image)
目前您正在将一个空模型传递给您的视图,然后它试图使用它的“id”来生成一条路线,但“id”是 null
并且路线参数不能 null
.
我解决了这个问题,问题出在路由中的“图像”名称。我到处都把它改成了别的东西,现在可以用了。
我开始创建一个电子商务平台,但我被一个问题困扰了 2 天无法解决。这可能是显而易见的,但我就是找不到解决方案。你能帮我解决这个问题吗?我收到此错误:
Missing required parameters for [Route: images.update] [URI: dashboard/images/{image}]. (View: C:\xampp\htdocs\ecommerce\ecommerce\resources\views\website\admin\product_image\update.blade.php)
update.blade.php
@extends('website.admin.layouts.main')
@section('content')
<div class="col-md-12 col-sm-12 ">
<div class="x_panel">
<div class="x_title">
<h2>Zaktualizuj obraz</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<br />
<form id="updateimage-form" data-parsley-validate="" class="form-horizontal form-label-left" novalidate="" enctype="multipart/form-data" method="POST" action="{{route('images.update', $productImage->id)}}">
@csrf
@method('PUT')
<div class="item form-group">
<label class="col-form-label col-md-3 col-sm-3 label-align" for="first-name">Produkt<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 ">
<select class="form-control" name="product_id">
@foreach ($product as $prodcat)
<option value="{{$prodcat -> id}}" name="product_id">{{ $prodcat -> product_name }}</option>
@endforeach
</div>
</div>
<div class="ln_solid"></div>
<div class="item form-group">
<label class="col-form-label col-md-3 col-sm-3 label-align" for="first-name">Nazwa obrazu<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 ">
<input type="text" id="img_title" name="img_title" placeholder="Image Title" value="{{ $productImage -> image_name }}" required="required" class="form-control ">
</div>
</div>
<div class="item form-group">
<label class="col-form-label col-md-3 col-sm-3 label-align" for="first-name">Wgraj obraz<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 ">
<input type="file" name="img" id="img" onchange="fileSelected();"/>
</div>
</div>
<div class="item form-group">
<div class="col-md-6 col-sm-6 offset-md-3">
<button class="btn btn-primary" type="reset">Wyczyść</button>
<button type="submit" class="btn btn-success">Zaktualizuj obraz</button>
</div>
</div>
</form>
</div>
</div>
</div>
@endsection
models\ProductImage.php
<?php
namespace App\Models\models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProductImage extends Model
{
protected $fillable = [
'product_id',
'image_name',
'image',
'slug',
'status'
];
public function product()
{
return $this -> belongsTo('App\Models\models\Product');
}
}
ProductImageController.php
<?php
namespace App\Http\Controllers;
use App\Models\models\Product;
use App\Models\models\ProductImage;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ProductImageController extends Controller
{
public function index()
{
$productImage = ProductImage::all();
return view('website.admin.product_image.index', compact('productImage'));
}
public function create()
{
$product = Product::all();
return view('website.admin.product_image.create', compact('product'));
}
public function store(Request $request)
{
$slug = Str::slug($request->image_name, '-');
$image = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $image);
ProductImage::create([
'image_name'=>$request->image_name,
'image'=>$image,
'product_id'=>$request->product_id,
'slug'=>$slug,
]);
return redirect()->route('images.index');
}
public function show(ProductImage $ProductImage)
{
//
}
public function edit(ProductImage $productImage)
{
$product = Product::all();
return view('website.admin.product_image.update',compact('productImage','product'));
}
public function update(Request $request, ProductImage $productImage)
{
$slug=Str::slug($request->image_name,'-');
if($request->image)
{
$image = time() .'.'. $request -> image -> extension();
$request->image->move(public_path('images'), $image);
}
else
{
$image=$productImage->image;
}
$productImage->update([
'image_name'=>$request->image_name,
'image'=>$image,
'product_id'=>$request->product_id,
'slug'=>$slug,
]);
return redirect()->route('images.index');
}
public function destroy(productImage $productImage)
{
$productImage->delete();
return redirect()->route('images.index');
}
}
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('website.shop.layouts.main');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/dashboard', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard.index');
Route::resource('/dashboard/categories', 'App\Http\Controllers\ProductCategoryController');
Route::resource('/dashboard/product', 'App\Http\Controllers\ProductController');
Route::resource('/dashboard/images', 'App\Http\Controllers\ProductImageController');
action="{{route('images.update', $productImage->id)}}"
是调用 route()
辅助函数的不正确方式。考虑以下片段;
action="{{route('images.update', [
'image' => $productImage->id
])}}"
有关 route()
助手的更多详细信息,请参阅此 link; https://laravel.com/docs/8.x/helpers#method-route
update.blade.php
<form method="post" action="{{route('images.update', $productImage->id)}}" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="form-group">
<label for="filename">Select your Image</label>
<input type="file" name="image" class="form-control">
<button type="submit" class="btn btn-primary btn-lg">Update</button>
</div>
</form>
ProductImageController.php
<?php
namespace App\Http\Controllers;
use App\Models\models\ProductImage;;
use Illuminate\Http\Request;
class DocumentController extends Controller
{
public function update(Request $request, $productId){
$product = ProductImage::findOrFail($productId);
if($request->hasfile('image'))
{
$file = $request->file('image');
$name=$file->getClientOriginalName();
$file->move(public_path().'/uploads/ProductImages/', $name);
$file = $name;
$product->image = $file;
}
$product->save();
return redirect()->back();
}
}
这里没有发生模型绑定,而是发生了依赖注入。要进行隐式路由模型绑定,您必须将方法的类型提示参数与路由参数的名称相匹配。在你的例子中,路由参数被命名为 image
,而不是 productImage
。因此,您将获得 ProductImage
的空新实例,而不是路由模型绑定的替换实例。
更改控制器的参数以匹配路由参数:
vvvvv
public function edit(ProductImage $image)
除非您想覆盖路由中使用的参数,否则您需要在所有需要隐式绑定的方法中更改此项。
vvvvv
public function update(Request $request, ProductImage $image)
目前您正在将一个空模型传递给您的视图,然后它试图使用它的“id”来生成一条路线,但“id”是 null
并且路线参数不能 null
.
我解决了这个问题,问题出在路由中的“图像”名称。我到处都把它改成了别的东西,现在可以用了。