public function run() { $faker = Faker::create(); foreach (range(1, 100) as $index) { Materi::create(['title' => $faker->sentence(), 'path' => $faker->url, 'category_id' => Category::orderByRaw('RAND()')->first()->id, 'user_id' => User::orderByRaw('RAND()')->first()->id]); } }
/** * Store a newly created resource in storage. * POST /membermateris * * @return Response */ public function store() { // validasi $rules = ['title' => 'required', 'category_id' => 'required', 'path' => 'required']; $v = Validator::make(Input::all(), $rules); if ($v->fails()) { return Redirect::route('member.materi.create')->withErrors($v)->withInput(); } // cek file apakah ini pdf $file = Input::file('path'); if ($file->getClientOriginalExtension() != 'pdf') { return Redirect::route('member.materi.create')->withErrors(['File materi harus berupa pdf'])->withInput(); } $fileName = uniqid() . "-" . $file->getClientOriginalName(); $file->move(public_path('download'), $fileName); // save database $data = Input::only('title', 'category_id'); $data['user_id'] = Auth::user()->id; $data['path'] = "download/" . $fileName; Materi::create($data); // redirect return Redirect::route('member.materi.index'); }