namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function create()
{
return view('posts/create');
}
public function store(Request $request)
{
$this->validate($request, [
'caption' => 'required',
'image' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imagePath = request('image')->store('uploads', 'public');
auth()->user()->posts()->create([
'caption' => $this['captions'],
'image' => $imagePath,
]);
return redirect('/profile/' . auth()->user->id);
}
}
uj5u.com熱心網友回復:
我的假設是您正在嘗試訪問在創建Postfor時已驗證的輸入之一User:
// getting the inputs that were validated as an array
$data = $this->validate(...);
...
auth()->user()->posts()->create([
'caption' => $data['caption'], // <------- accessing data array
'image' => $imagePath,
]);
如果您的 Laravel 版本沒有從validate呼叫中回傳經過驗證的資料,您可以從Request自身訪問資料:
'caption' => $request->input('caption')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/392928.html
上一篇:Laravel中的Accessor/Mutators和Casting有什么區別?
下一篇:Laravel路由銷毀回傳問題
