我似乎在上传图片时遇到问题。我不断收到 "Cannot use object of type App\Http\Controllers\PostsController as array. "
I seem to have issues uploading images. i keep getting "Cannot use object of type App\Http\Controllers\PostsController as array. "
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);
}
}
我的假设是您正在尝试访问在为 User
创建 Post
时验证的输入之一:
// 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')
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);
}
}
我的假设是您正在尝试访问在为 User
创建 Post
时验证的输入之一:
// 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')