new_gist_file.php
https://laravel.com/docs/5.4/validation
<?php
public function store(Request $request)
{
$tasks_count = Task::count() ;
if ( $tasks_count < 20 ) {
// dd( $request->all() ) ;
// dd($request->file('photos'));
// VALIDATION RULES
// 'sometimes' will make this field optional
$this->validate( $request, [
'task_title' => 'required',
'task' => 'required',
'project_id' => 'required',
'photos.*' => 'sometimes|required|mimes:png,gif,jpeg,txt,pdf,doc' // photos is an array
]) ;
// dd($request->all() ) ;
// First save Task Info
$task = Task::create([
'project_id' => $request->project_id,
'task_title' => $request->task_title,
'task' => $request->task,
'priority' => $request->priority
]);
// Then save files using the newly created ID above
if ( $request->file('photos' ) != '' ) {
foreach ($request->photos as $file) {
// To Storage
// $filename = $file->store(''); // /storage/app/taskfiles
// To public/images
$filename = $file->getClientOriginalName();
$file->move('images',$filename);
// Now create
TaskFiles::create([
'task_id' => $task->id, // then use the new available task->id to store the filename to DB
'filename' => $filename
]);
}
}
Session::flash('success', 'Task Created') ;
return redirect()->route('task.show') ;
}
else {
Session::flash('info', 'Please delete some tasks, Demo max tasks: 20') ;
return redirect()->route('task.show') ;
}
/*
task
--------------------------------------------------------------------
id | project_id | task_title | task | priority | completed
task_file
---------------------------
id | task_id | filename
*/
}