Reputation:
I'm building a traveling app and the user has the possibility to edit their own departure date in case of a misplacement. At the moment, the user can edit the departure date from everyone. While it only should edit their own. Everything is working, but the update function where it should look for Auth::user();
. Any idea how I should make this?
Here is the code:-
DepartureController.php
public function edit(LocationUser $LocationUsers)
{
return view('departure.edit', compact('LocationUsers'));
}
public function update(Request $request, LocationUser)
{
$LocationUsers = Auth::user();
$LocationUsers->update($request->all());
return redirect()->route('home', $LocationUsers)
->withSuccess('Departure updated!');
}
The web.php
//Backend Departure date
Route::get('/departure/create', 'DepartureController@create')->name('departure.create');
Route::post('/departure/create', 'DepartureController@store')->name('departure.store');
Route::get('/departure/edit/{LocationUsers}', 'DepartureController@edit')->name('departure.edit{id}');
Route::patch('/departure/edit/{LocationUsers}', 'DepartureController@update')->name('departure.update');
The edit.blade.php
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card card-default">
<div class="card-header">
{{$LocationUsers->departure_date}}
Update my departure
</div>
<div class="card-body">
@if (session('success'))
<div class="alert alert-dismissible alert-success">
{{ session('success') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" action="{{route('departure.update', $LocationUsers)}}">
{!!csrf_field().method_field('patch')!!}
<div class="form-group"> <!-- Date input -->
<label class="control-label" for="departure_date"></label>
Date
<input class="form-control" id="departure_date" name="departure_date"
placeholder="DD/MM/YYYY" type="text"/>
</div>
<input type="submit" class="btn btn-primary" value="Update departure">
</form>
</div>
</div>
</div>
</div>
</div>
The User model
public function daysUntilDeparture()
{
if ($this->location()->count() < 1) {
return "No departure date has been given";
}
$location = $this->location[0];
$userLocation = LocationUser::where('user_id', $this->id)
->where('location_id', $location->id)
->first();
$departure_date = $userLocation->departure_date;
return $departure_date->diffInDays(Carbon::now());
}
The LocationUser
model
<?php
namespace App;
use Carbon\Carbon; use Illuminate\Database\Eloquent\Model;
class LocationUser extends Model { const DATE_FORMAT = 'd-m-Y';
protected $fillable = ['departure_date', 'user_id'];
protected $dates = ['departure_date'];
protected $table = 'location_users';
public function user() {
return $this->belongsTo('\App\User', 'id', 'user_id');
}
public function location() {
return $this->hasOne('\App\Location', 'id', 'location_id');
}
public function setDepartureDateAttribute($date)
{
$this->attributes['departure_date'] = Carbon::createFromFormat(self::DATE_FORMAT, $date);
}
}
Upvotes: 0
Views: 3820
Reputation: 274
You can use
public function update(Request $request)
{
$date = Carbon::parse($request->departure_date);
$LocationUsers = LocationUser::where('user_id', Auth::id())->update(['departure_date' => $date]);
if($LocationUsers){
return redirect()->route('home', $LocationUsers)->withSuccess('Departure updated!');
}
}
Upvotes: 1