I am newbie learning laravel and i am trying to upload an image but i also need the user who uploaded the image. Here is my photos table
`public function up()
{
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->unsignedinteger('user_id')->index();
$table->string('title');
$table->string('image');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('photos');
}`
This is my Photos model:
protected $fillable = [
'user_id','title','image'
];
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
And this is my controller:
public function upload(Request $request)// store the image
{
$user=Auth::user()->id;
$this->validate($request, [
'title' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$input['image'] = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $input['image']);
$input['title'] = $request->title;
Photos::create($input);
return back()
->with('success','Image Uploaded successfully.')
->with($user);
}
The error i get is :SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into
photos(
image,
title,
updated_at,
created_at)
The uploading is working without having a user_id row. Can you please help me what did I do wrong?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire