![](/img/trans.png)
[英]Can't display the values from my data base with Laravel Livewire
[英]My code in laravel can't call back data from data base to update
你好朋友我是laravel框架的新手我正在嘗試構建一個crud應用程序所以遇到了這個問題當我在索引頁面中單擊Edit btn它調用特定ID中的數據來編輯它時它顯示“錯誤404頁面未找到”我仍然找不到問題所在所以請幫忙
產品控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\product;
use Illuminate\Support\Facades\DB;
/*call product by specific id to update */
public function edit($id){
$product = DB::table('products')->where('id',$id)->first();
return view('product.edit',compact('product'));
}
/* product update */
public function update(request $request ,$id){
$oldlogo = $request->old_logo;
$data = array();
$data['product_name'] = $request->product_name;
$data['product_code'] = $request->product_code;
$data['product_details'] = $request->product_details;
$image = $request->file('product_logo');
if ($image){
unlink($oldlogo);
$image_name = date('dmy_H_s_i');
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/media/';
$image_url = $upload_path.$image_full_name;
$data['product_logo'] = $image_url;
$success =$image->move($upload_path,$image_full_name);
$data['product_logo'] =$image_url;
$product = DB::table('products')->where('id'.$id) -> update($data);
}
return redirect()->route('product.index')
->with('success','Product updated successfully');
}
編輯 btn `
<a class="btn btn-primary" href="{{ URL :: to ('edit/product'.$pro->id) }}">Edits</a>
編輯頁面
@extends('product.layout')
@section('content')
<br><br><br>
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
</div>
<br><br><br>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('product.index') }}">Back</a>
</div>
<form action="" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-row">
<div class="col-xs-12 col-xm-12 col-md-12">
<div class="form-group">
<strong>Product name</strong>
<input type="text" name="product_name" calss="form-control" value="{{ $product ->product_name }}">
</div>
</div>
<div class="col-xs-12 col-xm-12 col-md-12">
<div class="form-group">
<strong>Product Code</strong>
<input type="text" name="product_code" calss="form-control" value="{{ $product ->product_code }}">
</div>
</div>
<div class="col-xs-12 col-xm-12 col-md-12">
<div class="form-group">
<strong> Detials </strong>
<textarea class="form-control" name="Details" style="height:150px" >
{{ $product ->product_details" }}</textarea>
</div>
<div class="col">
<div class="form-group">
<strong>Product image</strong>
<input type="file" name="logo">
</div>
</div>
<div class="col">
<div class="form-group">
<strong>Product old image</strong>
<img src ="{{ URL::to($product->product_logo) }}" height="70px" width="80px" alt="logo">
<input type="hidden" name=" old_logo" value="{{ $product->product_logo }}">
</div>
</div>
<button type="button" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
@endsection
** 編輯路線 **
Route::get('edit/product/{id}','ProductController@edit');
Route::post('update/product/{id}','ProductController@update');
Laravel 模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
protected $fillable = [
'product_name', 'product_details', 'product_code','product_logo'
];
}
我認為您的問題就在這里: {{ URL :: to ('edit/product'.$pro->id) }}
因為它會打印例如: /edit.product23
。 您想要的是/edit/product/23
,因此將您的網址更改為{{ URL::to('edit/product/'.$pro->id) }}
或{{ url('edit/product/'.$pro->id) }}
。 還要確保像這樣設置路由Route::get('/edit/product/{id}, 'ControllerName@edit');
在您的 web.php 路由文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.