@AungHtetPaing__ @sinnbeck As Per your Instructions I have created a new table as follows
create_image_table :
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('post_id');
$table->string('path');
$table->timestamps();
});
}
Post Model :
public function images()
{
return $this->hasMany(Image::class);
}
Post Controller :
public function store(Request $request)
{
$data = request()->validate([
'PostCaption' => 'required',
'image.*' => 'required|image ',
]);
$post = post::create([
'user_id' => auth()->user()->id,
'Caption' => $data['PostCaption'],
]);
if ($request->images) {
foreach ($request->images as $image) {
$path = $image->store('uploads', 'public');
$image = Image::make(public_path("storage/{$path}"))->fit(1200, 1200);
$image->save();
Image::create([
'post_id' => $post->id,
'path' => $path
]);
}
}
return redirect('/dashboard/' . auth()->user()->id);
}
create.blade.php :
<x-input-label for="image" :value="__('Image')" />
<input type="file" , class=" form-control-file ml-3 mt-2" id="image" name=" images[]" multiple>
<x-input-error :messages="$errors->get('image')" class="mt-2" />
</div>
and also here is my posts table :
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('Caption');
$table->string('image');
$table->unsignedBigInteger('view_count')->default(0);
$table->timestamps();
$table->index('user_id');
});
}
Now im getting this error :SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value
I alredy put image field in post model in protected fillable . Dont know what im doing wrong ?