Laravel自定义表单请求获取ID

时间:2019-01-15 10:27:37

标签: php laravel

我在控制器中有控制器方法:存储和更新:

public function store(AddEditShopRequest $request)
{
    $extra = ['user_id' => Auth::user()->id, 'slug' => str_slug($request->title)];
    $shop = Shop::create(array_merge($request->all(), $extra));

    return redirect()->back();
}

public function update(AddEditShopRequest $request, Shop $shop)
{
    $shop->update($request->all());

    return redirect()->back();
}

我有请求:AddEditShopRequest:

public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'title' => 'required|string|min:3|max:15|unique:shops,title,' . 
    $this->id,
    ]
}

当用户更新商店时,$this->id让我为空,我尝试$this->route('shop')->id并工作,但是当用户存储商店时,laravel让我出错:Trying to get property 'id' of non-object

我该如何解决?

0 个答案:

没有答案