I'm trying to relate the post with the client by sending "client_id" through request post then when I create it, it gives this exception: SQLSTATE[HY000]: General error: 1364 Field 'client_id' doesn't have a default value (SQL: insert into posts
(content
, updated_at
, created_at
) values (sadadad, 2020-09-17 18:26:38, 2020-09-17 18:26:38))
<form role="form" action="" method="POST" enctype="multipart/form-data">
@csrf
@include('includes.errors')
<input type="hidden" name="client_id" value="">
<textarea type="text" class="form-control form-control-md" placeholder="what's in your mind ?" name="content"></textarea>
<div style="margin: 10px 0;">
<input type="file" hidden id='upload-photo' name="image">
<label for="upload-photo" class="post-adds" ><i class="far fa-file-image fa-lg"></i></label>
<input type="file" hidden id='upload-video' name="video">
<label for="upload-video" class="post-adds" ><i class="far fa-file-video fa-lg"></i></label>
<button class="btn btn-sm btn-outline-primary " type="submit" style="display: inline; float:right;">Post</button>
</div>
</form>
migrations:
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('content')->nullable();
$table->integer('likes')->default(0);
$table->integer('shares')->default(0);
$table->string('image')->nullable();
$table->string('video')->nullable();
$table->timestamps();
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
});
controller:
$request->validate([
'video'=> 'mimes:mp4| max:20000',
'image'=>'image | max:2000',
'content'=>'required',
]);
$request_data = $request->except('video','image');
if($request->image){
$img = Image::make($request->image);
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save(public_path('dist/img/posts/'.$request->image->hashName()),60);
$request_data['image'] = $request->image->hashName();
}
if($request->video){
$vid = $request->video;
$vid_name = rand().'.'.$vid->getClientOriginalExtension();
$vid->move(public_path('dist/vid/posts'),$vid_name);
$request_data['video'] = $vid_name;
}
Post::create($request_data);
session()->flash('success',__('site.post_created_successfully'));
return redirect()->route('posts.index');
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire