dongxi1943 2018-06-13 11:25
浏览 119

Laravel更新用户的数据

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

}

  • 写回答

1条回答 默认 最新

  • doutao1171 2018-06-13 11:55
    关注

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

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗