I am working on a Laravel 5.7 application that involves a form that edits the profile of the logged in user. After I submit the form, the action properly updates the Auth object in the session, and then redirects back to the form page correctly.
However when I am redirected back to the form view, the updated attributes in the Auth object, are not updating correctly when they are being displayed. The same variables are being used twice in the same view, once on the parent view, and once from a sub-view being included with the @include blade directive, that contains the form itself.
The instance of the variables that are being used in the parent view blade update properly, and display the updated values. However the instance of the same variables being echoed inside the sub-view blade do not update correctly.
Dumping the contents of the Auth object confirms that the values are being updated/refreshed properly in the session for the attributes in question.
Referencing the Laravel documentation states that all variables in a parent view will be available in sub-views as well:
Including Sub-Views:
Blade's @include directive allows you to include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view:
So why do the updated values display correctly in the parent view blade, but incorrectly in the sub-view blade, when they are both using the same variable?
The variables in question are: and . They are being echoed once in the value attribute of the corresponding form fields from within the sub-view, and once as text in the wrapping parent view blade. The variable instance outputted as text in the parent view update properly, however the instances when they are used in the form input value attributes in the sub-view do not update correctly...
The contents of the parent view wrapping the form:
@section('content')
<div class="d-flex w-100">
<div class="well well-sm w-100">
<div class="row">
<div class="col-md-3">
<div class="row">
<div class="col-sm-12">
<div class="jarviswidget jarviswidget-sortable" id="wid-id-0">
<header class="ui-sortable-handle">
<div class="widget-header">
<h2><i class="fa fa-user"> </i></h2>
</div>
</header>
<!-- widget div-->
<div role="content">
<!-- widget content -->
<div class="widget-body">
<div class="row">
<div class="col-md-7">
<strong class="text-orange">Current Delivery Address:</strong>
<hr class="simple" />
<?php
$address = $addresses->filter(function($address) {
return $address->default == 1;
});
$a = $address->first();
?>
<address>
<strong></strong><br />
@if($a->address2) @endif<br />
,
</address>
</div>
<div class="col-md-5">
<img src="" />
</div>
</div>
</div>
<!-- end widget content -->
</div>
<!-- end widget div -->
</div>
</div>
</div>
<div class="well well-light well-sm">
<div class="row">
<div class="col-sm-12">
<h4 class="pl-2 pt-2 text-orange"><i class="fa fa-cutlery"></i> My Favorite Restaurants</h4>
<hr class="simple" />
@if($favorite_restaurants->count())
<div class="text-center img-loader"><i class="fa fa-gear fa-4x fa-spin text-orange"></i></div>
<div id="favoriteRestaurantsAccordion" class="d-none">
@foreach($favorite_restaurants as $f)
<h3 class="text-green-dark">
@if($f->isOpen)
<span class="label label-success pull-right"><i class="fa fa-check-square-o"></i> Restaurant Open</span>
@else
<span class="label label-danger pull-right"><i class="fa fa-ban"></i> Restaurant Closed</span>
@endif
</h3>
<div>
<div class="row align-middle">
<div class="col-md-7">
<a href=""><img src="" /></a>
</div>
<div class="col-md-5">
<button class="btn btn-block btn-labeled bg-green-light text-white"><span class="btn-label"><i class="fa fa-trash-o"></i></span> Remove</button>
<button class="btn btn-block btn-labeled bg-green-light text-white"><span class="btn-label"><i class="fa fa-cutlery"></i></span> View Menu</button>
</div>
</div>
</div>
@endforeach
</div>
@else
<div class="alert alert-warning"><i class="fa fa-exclamation-triangle"></i> You have no favorite restaurants!</div>
@endif
</div>
</div>
</div>
</div>
<div class="col-md-9">
<div class="text-center img-loader"><i class="fa fa-gear fa-4x fa-spin text-orange"></i></div>
<div id="tabs" class="d-none">
<ul>
<li><a href="#my-orders"><i class="fa fa-shopping-cart"></i> My Orders</a></li>
<li><a href="#edit-profile"><i class="fa fa-edit"></i> Edit Profile</a></li>
<li><a href="#addresses"><i class="fa fa-truck"></i> My Delivery Addresses</a></li>
<li><a href="#payment-profiles"><i class="fa fa-credit-card"></i> Payment Profiles</a></li>
</ul>
<div id="my-orders">
@include('User/dashboard/orders')
</div>
<div id="edit-profile">
@include('User/dashboard/edit-profile')
</div>
<div id="addresses">
@include('User/dashboard/addresses')
</div>
<div id="payment-profiles">
@include('User/dashboard/payment-profiles')
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
The contents of the sub-view that contains the form:
<div class="row">
<div class="col-md-6 border-right">
<form method="POST" action="" class="smart-form" novalidate>
@csrf
<div class="form-group row">
<label for="fname" class="col-md-4 col-form-label text-md-right">:</label>
<div class="col-md-6">
<input id="fname" type="text" class="form-control" name="fname" value="" required autofocus>
@if ($errors->has('fname'))
<span class="invalid-feedback" role="alert"><strong></strong></span>
@endif
</div>
</div>
<div class="form-group row">
<label for="lname" class="col-md-4 col-form-label text-md-right">:</label>
<div class="col-md-6">
<input id="lname" type="text" class="form-control" name="lname" value="" required autofocus>
@if ($errors->has('lname'))
<span class="invalid-feedback" role="alert"><strong></strong></span>
@endif
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">:</label>
<div class="col-md-6">
<label class="input mb-1">
<i class="icon-append fa fa-phone"></i>
<input id="phone" type="text" class="form-control" name="phone" value="" required autofocus>
</label>
@if ($errors->has('phone'))
<span class="invalid-feedback" role="alert"><strong></strong></span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">:</label>
<div class="col-md-6">
<label class="input mb-1">
<i class="icon-append fa fa-envelope-o"></i>
<input id="email" type="email" class="form-control" name="email" value="" required>
</label>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert"><strong></strong></span>
@endif
</div>
</div>
<div class="form-group row">
<div class="offset-md-4 col-md-6 text-left">
<label for="newsletter" class="vcheck mb-3">
<input type="hidden" name="newsletter" value="0" />
<input id="newsletter" type="checkbox" class="align-bottom" name="newsletter" value="1" @if(auth()->user()->newsletter == 1) checked @endif />
<span></span>
</label>
</div>
</div>
<hr class="simple" />
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button id="btnUserRegister" type="submit" class="btn btn-primary"><span class="fa fa-check-square-o"></span> </button>
</div>
</div>
</form>
</div>
<div class="col-md-6">
<form method="POST" action="" class="smart-form" novalidate>
@csrf
<div class="form-group row">
<label for="current_password" class="col-md-4 col-form-label text-md-right">:</label>
<div class="col-md-6">
<label class="input mb-1">
<i class="icon-append fa fa-lock"></i>
<input id="current_password" type="password" class="form-control" name="current_password" required>
</label>
@if ($errors->has('current_password'))
<span class="invalid-feedback" role="alert"><strong></strong></span>
@endif
</div>
</div>
<hr class="simple" />
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">:</label>
<div class="col-md-6">
<label class="input mb-1">
<i class="icon-append fa fa-lock"></i>
<input id="password" type="password" class="form-control" name="password" required>
</label>
@if ($errors->has('password'))
<span class="invalid-feedback" role="alert"><strong></strong></span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password_confirmation" class="col-md-4 col-form-label text-md-right">:</label>
<div class="col-md-6">
<label class="input mb-1">
<i class="icon-append fa fa-lock"></i>
<input id="password_confirmation" type="password" class="form-control" name="password_confirmation" required>
</label>
@if ($errors->has('password_confirmation'))
<span class="invalid-feedback" role="alert"><strong></strong></span>
@endif
</div>
</div>
<hr class="simple" />
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button id="btnUserRegister" type="submit" class="btn btn-primary"><span class="fa fa-check-square-o"></span> </button>
</div>
</div>
</form>
</div>
</div>
The action the form submits to that updates the Auth object:
public function updateProfile() {
request()->validate([
'fname' => 'required|string|max:255',
'lname' => 'required|string|max:255',
'phone' => ['required', 'regex:/^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$/'],
'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore(auth()->user()->id)],
'newsletter' => 'nullable'
]);
auth()->user()->update(request()->all());
Auth::setUser(\App\User::find(Auth::id()));
session()->flash('successMsg', 'Profile updated successfully.');
return back();
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire