Here is my controller for creating a post.
public function store(Request $request)
{
$data = request()->validate([
'postTitle' => 'required',
'postDescription' => 'required',
'postTradeValue' => 'required|integer',
'postTradeOption' => 'required',
'postCategory' => 'required',
'postTradeInterest' => 'required',
'postTags' => 'required',
'postImage.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
'thumbnail.*' => '',
]);
foreach($request->file('postImage') as $postImage) {
$basename = Str::random();
$original = $basename . '.' . $postImage->getClientOriginalExtension();
$thumbnail = $basename . '_thumb.' . $postImage->getClientOriginalExtension();
Image::make($postImage)
->fit(250,250)
->save(public_path('storage/photos/' . $thumbnail));
$postImage->move(public_path("storage/photos/"), $original);
}
auth()->user()->posts()->create([
'postTitle' => ucfirst($data['postTitle']),
'postDescription' => $data['postDescription'],
'postTradeValue' => $data['postTradeValue'],
'postTradeOption' => $data['postTradeOption'],
'postCategory' => $data['postCategory'],
'postTradeInterest' => $data['postTradeInterest'],
'postTags' => strtolower($data['postTags']),
'postImage' => $original,
'thumbnail' => $thumbnail,
]);
return redirect('/profile/'. auth()->user()->id);
}
public function show(\App\Post $post)
{
return view('post.show', compact('post'));
}
here is the migration
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('postTitle');
$table->longText('postDescription');
$table->string('postCategory');
$table->string('postTradeValue');
$table->string('postTradeOption');
$table->string('postTradeInterest');
$table->string('postTags');
$table->string('postImage');
$table->string('thumbnail')->nullable();
$table->timestamps();
$table->index('user_id');