I have a file upload input field, works fine and files get saved the whole form gets reset after it except the file input field. Can someone help me.
File Uploads input field not clearing after saving file
Hi, thanks. Here it is.
`<?php
namespace App\Http\Livewire;
use App\Models\ClientDeliveriesPlanned;
use App\Models\Tips;
use App\Models\UserPaiementAccount;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Livewire\WithFileUploads;
class ShipperSection extends Component
{
use WithFileUploads;
public $showCreateShipmentModal = false;
public $showDepositModal = false;
public $fromAddress;
public $destinations;
public $height;
public $width;
public $depth;
public $weight;
public $content;
public $tripsId;
public $photo;
public $fromAdresses;
public $distanceText;
public $distanceValue;
public $durationText;
public $durationValue;
public $deposit;
public $balance;
public $cost;
public $totalCost;
public $tip=0.00;
public $hastip10 = 0;
public $hastip15 = 0;
public $hastip20 = 0;
public $iteration;
public $originAdresses;
public $selectedFromAddress = null;
public $selectedToAddress = null;
public $receiverName = null;
public $hasInProgress = 0;
protected $listeners = ['addTip','payTrip','distances','inprogress'];
public function mount($selectedToAddress = null)
{
$this->hasInProgress = 0;
$this->originAdresses = $this->read()->pluck('origin_address','id');
$this->destinations = collect();
$this->selectedToAddress = $selectedToAddress;
}
public function inprogress()
{
$this->hasInProgress = 1;
}
public function updatedSelectedFromAddress($originAdresses)
{
if (!is_null($originAdresses)) {
$this->destinations = ClientDeliveriesPlanned::where('id', $originAdresses)->first()->destination;
$this->receiverName = ClientDeliveriesPlanned::where('id', $originAdresses)->first()->receiver_name;
}
$this->distances();
}
public function resetVars()
{
$this->tip=0.00;
$this->cost = 0.00;
$this->totalCost = $this->cost;
$this->hastip10 = 0;
$this->hastip15 = 0;
$this->hastip20 = 0;
$this->tripsId = 0;
$this->photo = null;
$this->height = null;
$this->width = null;
$this->depth = null;
$this->weight = null;
$this->content = null;
$this->tripsId = null;
$this->distanceText = null;
$this->distanceValue = 0.00;
$this->durationText = null;
$this->durationValue = 0.00;
$this->selectedFromAddress = null;
$this->destinations = null;
$this->iteration++;
}
public function addTip($tip)
{
$this->tip = $this->cost * ($tip/100);
$this->totalCost = ($this->cost * ($tip/100)) + $this->cost;
$this->hastip10 = $tip === 10 ? 1 : 0;
$this->hastip15 = $tip === 15 ? 1 : 0;
$this->hastip20 = $tip === 20 ? 1 : 0;
}
public function createShipment()
{
$this->showCreateShipmentModal = true;
}
public function distances()
{
$this->validate([
'destinations'=>'required|string',
'selectedFromAddress'=>'required|string',
]);
$destinations = $this->destinations;
$this->tripsId = $this->selectedFromAddress;
$originAddress = $this->fromAdresses[$this->selectedFromAddress];
$this->fromAddress = $originAddress;
if (!empty($originAddress) && !empty($destinations)){
$this->getDistance($originAddress, $destinations, now());
}
}
public function getDistance($origin, $destination, $date)
{
$apyKey = env('GOOGLE_MAPS_API_KEY');
$url = 'https://maps.googleapis.com/maps/api/directions/json?origin='.str_replace(' ','+',$origin).'&destination='.str_replace(' ','+',$destination).'&key='.$apyKey;
$result = json_decode(file_get_contents($url));
$this->distanceText = $result->routes[0]->legs[0]->distance->text;
$this->distanceValue = $result->routes[0]->legs[0]->distance->value/1000;
$this->durationText = $result->routes[0]->legs[0]->duration->text;
$this->durationValue = $result->routes[0]->legs[0]->duration->value;
$rate = $this->distanceValue > 50 ? 0.10 : 0.45;
$this->cost= $this->distanceValue*$rate;
$this->urlDirections = "https://www.google.com/maps/dir/".str_replace(' ','+',$origin)."/".str_replace(' ','+',$destination);
}
public function deposit($id)
{
$this->deposit = null;
$this->showDepositModal = true;
}
public function depositAmount($id)
{
$newBalance = $this->balance + $this->deposit;
DB::table('user_paiement_accounts')->updateOrInsert(['user_id'=>$id],['deposit'=>$this->deposit,'balance'=> $newBalance,'withdrawal'=>0.00,'created_at'=>now()]);
DB::table('paiement_transactions')->insert(['user_id'=>$id,'deposit'=>$this->deposit,'balance'=> $newBalance,'withdrawal'=>0.00,'created_at'=>now()]);
$this->showDepositModal = false;
}
public function payTrip($tripId)
{
$this->validate([
'destinations'=>'required|string',
'fromAddress'=>'required|string',
'height'=>'required|numeric',
'width'=>'required|numeric',
'depth'=>'required|numeric',
'weight'=>'required|numeric',
'content'=>'required|string',
'photo' => 'required|image|max:1024',
]);
$newBalance = $this->balance - $this->totalCost;
DB::table('user_paiement_accounts')->updateOrInsert(['user_id'=>Auth::id()],['deposit'=>0,'balance'=> $newBalance,'withdrawal'=>$this->totalCost === null ? 0.00 : $this->totalCost]);
DB::table('paiement_transactions')->insert(['user_id'=>Auth::id(),'deposit'=>0.00,'balance'=> $newBalance,'withdrawal'=>$this->totalCost === null ? 0.00 : $this->totalCost,'created_at'=>now(),'trip_id'=>$tripId]);
$this->tripsId = $tripId;
$this->photo = $this->getImageUrl('photo', $this->photo);
DB::table('packages')->insert($this->modelData());
$this->showCreateShipmentModal = false;
$this->resetVars();
}
public function modelData()
{
return [
'user_id'=> Auth::id(),
'height'=>$this->height,
'width'=>$this->width,
'depth'=>$this->depth,
'weight'=>$this->weight,
'content'=>$this->content,
'photo'=>$this->photo,
'verified_receiver'=>0,
'verified_driver'=>0,
'receiver_name'=>'',
'receiver_code'=>'',
'driver_code'=>'',
'receiver_received'=>0,
'trip_id'=>$this->tripsId,
'package_received_code'=>'',
'driver_id'=>0,
'created_at'=>now(),
];
}
public function getImageUrl($imageName, $image)
{
if(!is_string($image)){
$this->validate([$imageName => 'image|max:2024']);
$url = $image->storePublicly('package-photos','public');
$image=null;
$this->iteration++;
return $url;
}
else{
return $image;
}
}
public function read()
{
return ClientDeliveriesPlanned::where('user_id',Auth::id())->get();
}
public function render()
{
$locale = App::getLocale();
$destinationsAddress = $this->read()->pluck('destination', 'id');
$this->fromAdresses = $this->read()->pluck('origin_address','id');
$tips = Tips::all();
$userPaiementInfo = UserPaiementAccount::where('user_id',Auth::id())->first();
$this->balance = $userPaiementInfo['balance'];
return view('livewire.shipper-section',[
'data'=>$this->read(),
'locale'=>$locale,
'destinationsAddress'=>$destinationsAddress,
'fromAdresses'=>$this->fromAdresses,
'tip'=>$this->tip,
'tips'=>$tips,
'userPaiementInfo'=>$userPaiementInfo,
]);
}
}
`