Laravel更新用户的数据

时间:2018-06-13 11:25:57

标签: php laravel

我正在构建一个旅行应用程序,用户可以在错位的情况下编辑自己的出发日期。目前,用户可以编辑每个人的出发日期。虽然它只应该编辑自己的。一切正常,但更新功能应该在哪里寻找Auth::user();。知道我该怎么做吗?

以下是代码: -

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

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

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>

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());


}

LocationUser型号

<?php

命名空间App;

使用Carbon \ Carbon; 使用Illuminate \ Database \ Eloquent \ Model;

类LocationUser扩展了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);
}

}

1 个答案:

答案 0 :(得分:1)

您可以使用

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

}