0

I'm trying to achieve to update the image in laravel using Yajra DataTabel, when I insert image in database and folder it seems to be working fine because it stores in the folder and shows in the yajra datatable but when I edit the image it doesn't show image and doesn't store in the database. Anyways these are my code. Thank you in Advance

Controller:

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:

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

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

this is my result when I use 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"
];

Model:

  • What have you tried to debug the problem? – Nico Haase Jul 14 '20 at 08:20
  • 1
    well `if ($fileNameToStore = '')` is an assignment, so that will always resolve the same way every time and you are not passing a key value array to `validate` ... – lagbox Jul 14 '20 at 08:21
  • 1
    @lagbox so what am I supposed to do –  Jul 14 '20 at 08:29
  • @AlifKhan There is an alternative built in laravel. You can do [validation](https://stackoverflow.com/questions/37777265/required-if-laravel-5-validation) which checks if certain form value is present then have the requirements apply so you don't have to do it in a if else block. – user3647971 Jul 15 '20 at 04:59
  • You don't have any element with class `edit`. It's in the html? – user3647971 Jul 15 '20 at 05:06
  • 1
    @user3647971 I already edited it is it ok? –  Jul 15 '20 at 05:10
  • @AlifKhan So the blade template is for modal and you insert values which are gotten from ajax request. Would you like to return the entire template for the modal each time edit button is pressed? So you'd insert the values as blade variables instead of jquery? – user3647971 Jul 15 '20 at 05:14
  • 1
    @user3647971 yes please –  Jul 15 '20 at 05:15
  • 1
    @user3647971 for inserting data I go to a different page the image shows whenever I Insert an image but whenever I update it my validation says that the shipping receipt is required which is my image. I really don't know, I'm just new in laravel to be honest –  Jul 15 '20 at 05:19

1 Answers1

0

You have defined resource routes which by default define a few routes and methods for those explained in detail here. Also you have defined an additional route to use the same code as Route::resource route PATCH method would with route named shipping.update. Note that the additional route definition overrides resource routes POST method definition. You can remove the Route::post('shipping/update', 'ShippingController@update')->name('shipping.update'); line from your routes if it is only related to updating the current data.

//EDIT you can insert now too

You can use this as your modal template for updating data:

<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>

In your edit controller method you can return a blade view with data:

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

and in your create controller method you can return the view without data:

return view('this.template');

Your controller should be set up like this:

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');
}

Model:

<?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"];
}
user3647971
  • 1,069
  • 1
  • 6
  • 13
  • 1
    Route::post('shipping/update', 'ShippingController@update')->name('shipping.update'); –  Jul 15 '20 at 06:13
  • I've edited it a bit to make it compatible with creating new data and updating existing one. Let me know if there are any errors. – user3647971 Jul 15 '20 at 06:16
  • @AlifKhan Okay I added the parameter to the route function, but I'm not sure which is the right id for it? `hidden_id`? See the form action attribute and insert the correct one. – user3647971 Jul 15 '20 at 06:27
  • 1
    syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) (View: C:\wamp64\www\osm\resources\views\shipping_data.blade.php) has error –  Jul 15 '20 at 06:32
  • @AlifKhan Seems like there's one unclosed if somewhere, couldn't find it from my code tho. All are closed properly with endif – user3647971 Jul 15 '20 at 06:39
  • 1
    Missing required parameters for [Route: shipping.update] [URI: shipping/{shipping}]. (View: C:\wamp64\www\osm\resources\views\shipping_data.blade.php) –  Jul 15 '20 at 06:55
  • @AlifKhan {{route('shipping.update',$data->hidden_id)}} do you have this in the form action like above? – user3647971 Jul 15 '20 at 07:03
  • @AlifKhan I also added how the controller should be defined for those all to work. – user3647971 Jul 15 '20 at 07:04
  • @AlifKhan Could you also add the model definition for Shipping_data to the question aswell? If you don't get the receipt with all the other data the problem is probably there. – user3647971 Jul 15 '20 at 07:09
  • The update method requires the receipt upload each time it is edited? Is this intentional? – user3647971 Jul 15 '20 at 07:11
  • 1
    Yes i have the same action in the form –  Jul 15 '20 at 07:17
  • 1
    @AlifKhan I edited the controller update method for not requiring a file upload on each edit. It gets the current receiptName if no new one is uploaded and I removed the requirements for the upload. – user3647971 Jul 15 '20 at 07:26
  • 1
    @AlifKhan I switched the $form_data array and update method into Eloquent model equivalents. Working with Eloquent models is a breeze compared to that array one. – user3647971 Jul 15 '20 at 07:34
  • 1
    can u please send it to me –  Jul 15 '20 at 07:34
  • 1
    let's further chat on Facebook if you want –  Jul 15 '20 at 07:35