I did $table->string('image')->nullable(); in my migration
Apr 7, 2018
13
Level 14
Image store
Hi, I have image upload on my website, I',m trying to make it possible to have it nullable. Any ideas?
This is my store function:
public function store()
{
$this->validate(request(), [
'title' => 'required|unique:threads|max:255',
'body' => 'required|min:20',
'image' => 'image|nullable
]);
Thread::create([
'title' => request('title'),
'user_id' => Auth::user()->id,
'body' => request('body'),
'image' => request()->file('image')->store('frettir', 'public'),
]);
return redirect('/stjornbord')->with('flash', 'Fréttin var búin til!');
}
And this is my migration:
Schema::create('threads', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
});
Level 6
The problem is because you are trying to call a method that belongs to the File class in a null variable.
Try this when you create the thread.
Thread::create([
'title' => request('title'),
'user_id' => Auth::user()->id,
'body' => request('body'),
'image' => $request->hasFile('image') ? request()->file('image')->store('frettir', 'public') : null,
]);
Please or to participate in this conversation.