更新图像变为空 Laravel

Posted

技术标签:

【中文标题】更新图像变为空 Laravel【英文标题】:Updating Image Becomes Null Laravel 【发布时间】:2020-11-03 11:00:35 【问题描述】:

我正在尝试使用 Yajra DataTabel 更新 laravel 中的图像,当我在数据库和文件夹中插入图像时,它似乎工作正常,因为它存储在文件夹中并显示在 yajra 数据表中,但是当我编辑图像它不显示图像并且不存储在数据库中。无论如何,这些是我的代码。提前谢谢你

控制器:

public function edit($id)

    if (request()->ajax()) 
        $shippings = Shipping_data::findOrFail($id);
        return response()->json(['result' => $shippings]);
    


/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request)

    $shippingReceipt_name = $request->hidden_shippingReceipt;
    $shippingReceipt = $request->file('shippingReceipt');
    if($shippingReceipt != '')
        $rules = array(
        'name' ,
        'mobile',
        'items',
        'shippingAddress',
        'amount',
        'facebookLink',
        'facebookName',
        "modePayment",
        "shipVia"=> 'required',
        "trackingNumber"=> 'required',
        "shippingReceipt" => 'required',
        "shippingCharged"=> 'required',
        "shippingWeight"=> 'required'
    );
     $error = Validator::make($request->all(), $rules);
        if($error->fails())
        
            return response()->json(['errors' => $error->errors()->all()]);
        

        $shippingReceipt_name = rand() . '.' . $shippingReceipt->getClientOriginalExtension();
        $shippingReceipt->move(public_path('receipt'), $shippingReceipt_name);
     
     else 
         $rules = array(
        'name' ,
        'mobile',
        'items',
        'shippingAddress',
        'amount',
        'facebookLink',
        'facebookName',
        "modePayment",
        "shipVia"=> 'required',
        "trackingNumber"=> 'required',
        "shippingReceipt" => 'required',
        "shippingCharged"=> 'required',
        "shippingWeight"=> 'required',
    );

      $error = Validator::make($request->all(), $rules);

        if($error->fails())
        
            return response()->json(['errors' => $error->errors()->all()]);
        
    

    $form_data = array(
        'name' => $request->name,
        'mobile'=> $request->mobile,
        'items'=> $request->items,
        'shippingAddress'=> $request->shippingAddress,
        'amount'=> $request->amount,
        'facebookLink'=> $request->facebookLink,
        'facebookName'=> $request->facebookName,
        "modePayment"=> $request->modePayment,
        "shipVia"=> $request->shipVia,
        "trackingNumber"=> $request->trackingNumber,
        "shippingReceipt"=> $shippingReceipt_name,
        "shippingWeight"=> $request->shippingWeight,
        "shippingCharged"=> $request->shippingCharged,
    );

    Shipping_data::whereId($request->hidden_id)->update($form_data);

    return response()->json(['success' => 'Shipping Data is successfully updated']);

blade.php

  <form method="post" id="shipping_form" class="form-horizontal" enctype='multipart/form-data'>
                @csrf
                @if(Auth::check() && Auth::user()->role == "admin")
                <div class="form-group">
                    <label>Name</label>
                    <input type="text" class="form-control" name="name" placeholder="Please Enter Client Name"
                        id="name">
                    </select>
                </div>
                <div class="form-group">
                    <label>Mobile</label>
                    <input type="text" class="form-control" name="mobile" placeholder="Please Enter Mobile Number"
                        id="mobile">
                </div>
                <div class="form-group">
                    <label>Items Order</label>
                    <input type="text" class="form-control" name="items" placeholder="Please Enter Items Order"
                        id="items">
                </div>
                <div class="form-group">
                    <label>Shipping Address</label>
                    <input type="text" class="form-control" name="shippingAddress" id="shippingAddress"
                        placeholder="Please Enter Shipping Address">
                </div>
                <div class="form-group">
                    <label>Amount</label>
                    <input type="text" class="form-control" name="amount" placeholder="Please Enter Amount"
                        id="amount">
                </div>
                <div class="form-group">
                    <label>Facebook Link</label>
                    <input type="text" class="form-control" name="facebookLink" id="facebookLink"
                        placeholder="Please Enter Facebook Link">
                </div>
                <div class="form-group">
                    <label>Facebook Name</label>
                    <input type="text" class="form-control" name="facebookName" id="facebookName"
                        placeholder="Please Enter Facebook Name">
                </div>

                <div class="form-group">
                    <label>Mode of Payment</label>
                    <input type="text" class="form-control" name="modePayment" id="modePayment"
                        placeholder="Please Enter Mode of Payment">
                </div>
                @endif
                @if(Auth::check() && Auth::user()->role == "employee")
                <div class="form-group">
                    <label>Name</label>
                    <input type="text" class="form-control" name="name" placeholder="Please Enter Name" id="name"
                        READONLY>
                </div>
                <div class="form-group">
                    <label>Mobile</label>
                    <input type="text" class="form-control" name="mobile" placeholder="Please Enter Mobile Number"
                        id="mobile" READONLY>
                </div>
                <div class="form-group">
                    <label>Items Order</label>
                    <input type="text" class="form-control" name="items" placeholder="Please Enter Items Order"
                        id="items" READONLY>
                </div>
                <div class="form-group">
                    <label>Shipping Address</label>
                    <input type="text" class="form-control" name="shippingAddress" id="shippingAddress" READONLY
                        placeholder="Please Enter Shipping Address">
                </div>
                <div class="form-group">
                    <label>Amount</label>
                    <input type="text" class="form-control" name="amount" placeholder="Please Enter Amount"
                        id="amount" READONLY>
                </div>
                <div class="form-group">
                    <label>Facebook Link</label>
                    <input type="text" class="form-control" name="facebookLink" id="facebookLink" READONLY
                        placeholder="Please Enter Facebook Link">
                </div>
                <div class="form-group">
                    <label>Facebook Name</label>
                    <input type="text" class="form-control" name="facebookName" id="facebookName" READONLY
                        placeholder="Please Enter Facebook Name">
                </div>

                <div class="form-group">
                    <label>Mode of Payment</label>
                    <input type="text" class="form-control" name="modePayment" id="modePayment" READONLY
                        placeholder="Please Enter Mode of Payment">
                </div>
                @endif
                <div class="form-group">
                    <label>Shipping Via</label>
                    <input type="text" class="form-control" name="shipVia" placeholder="Please Enter Shipping Via"
                        id="shipVia">
                </div>
                <div class="form-group">
                    <label>Tracking Number</label>
                    <input type="text" class="form-control" name="trackingNumber" id="trackingNumber"
                        placeholder="Please Enter Tracking Number">
                </div>
                <div class="form-group">
                    <label>Shipping Receipt</label>
                    <br>
                    <input type="file" class="form-control file-form" name="shippingReceipt" id="shippingReceipt">
                </div>
                <div class="form-group">
                    <label>Shipping Charged</label>
                    <input type="text" class="form-control" name="shippingCharged" id="shippingCharged"
                        placeholder="Please Enter Shipping Charged">
                </div>
                <div class="form-group">
                    <label>Shipping Weight</label>
                    <input type="text" class="form-control" name="shippingWeight" id="shippingWeight"
                        placeholder="Please Enter Shipping Weight">
                </div>

                <br />
                <div class="form-group" align="center">
                    <input type="hidden" name="action" id="action" value="Add" />
                    <input type="hidden" name="hidden_id" id="hidden_id" />
                    <input type="submit" name="action_button" id="action_button" class="btn btn-success"
                        value="Add" />
                </div>
            </form>

路线:

Route::resource('shipping', 'ShippingController');

Route::post('shipping/update', 'ShippingController@update')->name('shipping.update');

这是我使用 dd 时的结果:

array:15 [
  "_token" => "lxEGrS8FOvM4zeuqGSJSSjNEhjexj3TkvM11JDCv"
  "name" => "Alif Khan"
  "mobile" => "9293000245"
  "items" => "Nike Men's T-Shirt"
  "shippingAddress" => "#8D Barcelona"
  "amount" => "100"
  "facebookLink" => "https://www.facebook.com/123"
  "facebookName" => "Mohammad Khan"
  "modePayment" => "GCash"
  "shipVia" => "12"
  "trackingNumber" => "12"
  "shippingCharged" => "12"
  "shippingWeight" => "12"
  "action" => "Edit"
  "hidden_id" => "2"
];

型号:

【问题讨论】:

好吧,if ($fileNameToStore = '') 是一个赋值,所以每次都会以相同的方式解决,并且您不会将键值数组传递给 validate ... @lagbox 那我该怎么办 @user3647971 我已经编辑过了可以吗? @user3647971 是的,请 @user3647971 用于插入数据我会转到另一个页面,每当我插入图像时,图像就会显示,但每当我更新它时,我的验证表明需要运输收据,这是我的图像。我真的不知道,老实说,我只是 laravel 的新手 【参考方案1】:

您已经定义了资源路由,默认情况下会为详细解释的那些定义一些路由和方法here。 此外,您还定义了一条额外的路线,以使用与Route::resource 路线PATCH 方法相同的代码,该路线将使用名为shipping.update 的路线。请注意,附加路由定义会覆盖资源路由POST 方法定义。 如果Route::post('shipping/update', 'ShippingController@update')-&gt;name('shipping.update'); 仅与更新当前数据有关,您可以从路由中删除它。

//编辑你现在也可以插入

您可以将其用作更新数据的模态模板:

<form method="POST" action="route('shipping.update',$data->id)" id="shipping_form" class="form-horizontal" enctype='multipart/form-data'>
    @csrf
    @method('PUT')
    @if(Auth::check() && Auth::user()->role == "admin")
    <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" name="name" placeholder="Please Enter Client Name"
            id="name" @isset($data) value="$data->name"@endisset>
        </select>
    </div>
    <div class="form-group">
        <label>Mobile</label>
        <input type="text" class="form-control" name="mobile" placeholder="Please Enter Mobile Number"
            id="mobile" @isset($data) value="$data->mobile"@endisset>
    </div>
    <div class="form-group">
        <label>Items Order</label>
        <input type="text" class="form-control" name="items" placeholder="Please Enter Items Order"
            id="items" @isset($data) value="$data->items"@endisset>
    </div>
    <div class="form-group">
        <label>Shipping Address</label>
        <input type="text" class="form-control" name="shippingAddress" id="shippingAddress"
            placeholder="Please Enter Shipping Address" @isset($data) value="$data->shippingAddress"@endisset>
    </div>
    <div class="form-group">
        <label>Amount</label>
        <input type="text" class="form-control" name="amount" placeholder="Please Enter Amount"
            id="amount" @isset($data) value="$data->amount"@endisset>
    </div>
    <div class="form-group">
        <label>Facebook Link</label>
        <input type="text" class="form-control" name="facebookLink" id="facebookLink"
            placeholder="Please Enter Facebook Link" @isset($data) value="$data->facebookLink"@endisset>
    </div>
    <div class="form-group">
        <label>Facebook Name</label>
        <input type="text" class="form-control" name="facebookName" id="facebookName"
            placeholder="Please Enter Facebook Name" @isset($data) value="$data->facebookName"@endisset>
    </div>

    <div class="form-group">
        <label>Mode of Payment</label>
        <input type="text" class="form-control" name="modePayment" id="modePayment"
            placeholder="Please Enter Mode of Payment" @isset($data) value="$data->modePayment"@endisset>
    </div>
    @endif
    @if(Auth::check() && Auth::user()->role == "employee")
    <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" name="name" placeholder="Please Enter Name" id="name"
            READONLY @isset($data) name="$data->name"@endisset>
    </div>
    <div class="form-group">
        <label>Mobile</label>
        <input type="text" class="form-control" name="mobile" placeholder="Please Enter Mobile Number"
            id="mobile" READONLY @isset($data) value="$data->mobile"@endisset>
    </div>
    <div class="form-group">
        <label>Items Order</label>
        <input type="text" class="form-control" name="items" placeholder="Please Enter Items Order"
            id="items" READONLY @isset($data) value="$data->items"@endisset>
    </div>
    <div class="form-group">
        <label>Shipping Address</label>
        <input type="text" class="form-control" name="shippingAddress" id="shippingAddress" READONLY
            placeholder="Please Enter Shipping Address" @isset($data) value="$data->shippingAddress"@endisset>
    </div>
    <div class="form-group">
        <label>Amount</label>
        <input type="text" class="form-control" name="amount" placeholder="Please Enter Amount"
            id="amount" READONLY @isset($data) value="$data->amount"@endisset>
    </div>
    <div class="form-group">
        <label>Facebook Link</label>
        <input type="text" class="form-control" name="facebookLink" id="facebookLink" READONLY
            placeholder="Please Enter Facebook Link" @isset($data) value="$data->facebookLink"@endisset>
    </div>
    <div class="form-group">
        <label>Facebook Name</label>
        <input type="text" class="form-control" name="facebookName" id="facebookName" READONLY
            placeholder="Please Enter Facebook Name" @isset($data) value="$data->facebookName"@endisset>
    </div>

    <div class="form-group">
        <label>Mode of Payment</label>
        <input type="text" class="form-control" name="modePayment" id="modePayment" READONLY
            placeholder="Please Enter Mode of Payment" @isset($data) value="$data->modePayment"@endisset>
    </div>
    @endif
    <div class="form-group">
        <label>Shipping Via</label>
        <input type="text" class="form-control" name="shipVia" placeholder="Please Enter Shipping Via"
            id="shipVia" @isset($data) value="$data->shipVia"@endisset>
    </div>
    <div class="form-group">
        <label>Tracking Number</label>
        <input type="text" class="form-control" name="trackingNumber" id="trackingNumber"
            placeholder="Please Enter Tracking Number" @isset($data) value="$data->trackingNumber"@endisset>
    </div>
    <div class="form-group">
        <label>Shipping Receipt</label>
        <br>
        <input type="file" class="form-control file-form" name="shippingReceipt" id="shippingReceipt">
         @if(isset($data) && !empty($data->shippingReceipt))
             <a href="url('/receipt/'.$data->shippingReceipt)">Receipt</a>
         @endif
    </div>
    <div class="form-group">
        <label>Shipping Charged</label>
        <input type="text" class="form-control" name="shippingCharged" id="shippingCharged"
            placeholder="Please Enter Shipping Charged" @isset($data) value="$data->shippingCharged"@endisset>
    </div>
    <div class="form-group">
        <label>Shipping Weight</label>
        <input type="text" class="form-control" name="shippingWeight" id="shippingWeight"
            placeholder="Please Enter Shipping Weight" @isset($data) value="$data->shippingWeight"@endisset>
    </div>

    <br />
    <div class="form-group" align="center">
        <input type="hidden" name="action" id="action" value="Edit" />
        <input type="hidden" name="hidden_id" id="hidden_id" @isset($data) value="$data->hidden_id"@endisset />
        <input type="submit" name="action_button" id="action_button" class="btn btn-success"
            value="Save" />
    </div>
</form>

在您的 edit 控制器方法中,您可以返回带有数据的刀片视图:

$data = Shipping_data::findOrFail($id);
return view('this.template',array('data' => $data));

在您的create 控制器方法中,您可以返回没有数据的视图:

return view('this.template');

你的控制器应该这样设置:

public function create(Request $request)// shipping.create route for returning template to create new data
    return view('shipping.create');


public function edit($id)// route shipping.edit get existing data to be edited
    return view('shipping.edit',array('data' => Shipping_data::findOrFail($id)));


public function store(Request $request)//route shipping.store this is for creating and storing new data
    


public function update(Request $request, $id)//route shipping.update 

    if($request->hasFile('shippingReceipt'))//check if request has file attached
        $shippingReceipt_name = $request->hidden_shippingReceipt;
        $shippingReceipt = $request->file('shippingReceipt');
        $rules = array(
            'name' ,
            'mobile',
            'items',
            'shippingAddress',
            'amount',
            'facebookLink',
            'facebookName',
            "modePayment",
            "shipVia"=> 'required',
            "trackingNumber"=> 'required',
            "shippingReceipt",//don't require the receipt on edit
            "shippingCharged"=> 'required',
            "shippingWeight"=> 'required'
        );
        $validator = Validator::make($request->all(), $rules);
        if($validator->fails())
            return response()->json(['errors' => $validator->errors()->all()]);
        

        $shippingReceipt_name = rand() . '.' . $shippingReceipt->getClientOriginalExtension();
        $shippingReceipt->move(public_path('receipt'), $shippingReceipt_name);
     else 
         $rules = array(
        'name' ,
        'mobile',
        'items',
        'shippingAddress',
        'amount',
        'facebookLink',
        'facebookName',
        "modePayment",
        "shipVia"=> 'required',
        "trackingNumber"=> 'required',
        "shippingReceipt",//don't require the receipt on edit
        "shippingCharged"=> 'required',
        "shippingWeight"=> 'required',
    );

      $validator = Validator::make($request->all(), $rules);

        if($validator->fails())
        
            return response()->json(['errors' => $validator->errors()->all()]);
        
    

    $shipping_data = Shipping_data::whereId($id)->first();//get the eloquent model with corresponding data
    $shipping_data->name = $request->name;
    $shipping_data->mobile = $request->mobile;
    $shipping_data->items = $request->items;
    $shipping_data->shippingAddress = $request->shippingAddress;
    $shipping_data->amount = $request->amount;
    $shipping_data->facebookLink = $request->facebookLink;
    $shipping_data->facebookName = $request->facebookName;
    $shipping_data->modePayment = $request->modePayment;
    $shipping_data->shipVia = $request->shipVia;
    $shipping_data->trackingNumber = $request->trackingNumber;
    $shipping_data->shippingReceipt = isset($shippingReceipt_name) ?  $shippingReceipt_name : $shipping_data->shippingReceipt;
    $shipping_data->shippingWeight = $request->shippingWeight;
    $shipping_data->shippingCharged = $request->shippingCharged;
    $shipping_data->save();// save the data into the database

    return redirect()->route('shipping.index');

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Shipping_data extends Model

    protected $fillable = ['name', 'mobile', 'items', 'shippingAddress', 'amount',
    'facebookLink', 'facebookName', "modePayment", "shipVia", "trackingNumber", "shippingReceipt","shippingCharged","shippingWeight"];

【讨论】:

Route::post('shipping/update', 'ShippingController@update')->name('shipping.update'); 语法错误,文件意外结束,期待 elseif (T_ELSEIF) 或 else (T_ELSE) 或 endif (T_ENDIF)(查看:C:\wamp64\www\osm\resources\views\shipping_data.blade .php) 有错误 缺少 [Route: shipping.update] [URI: shipping/shipping] 的必需参数。 (查看:C:\wamp64\www\osm\resources\views\shipping_data.blade.php) 是的,我在表单中有相同的操作 @AlifKhan 我编辑了控制器更新方法,不需要在每次编辑时上传文件。如果没有上传新的receiptName,它会获取当前的receiptName,并且我删除了上传的要求。

以上是关于更新图像变为空 Laravel的主要内容,如果未能解决你的问题,请参考以下文章

我来自服务器的图像变为 NULL

用户存在值从 asmack 变为空

为啥变量变为空?

UITableViewCell 中的 UIButton 在 iOS7 中不起作用

为啥我的 LocationListener 变为空?

NSManagedObject 变为空