PHP Lumen Upload Media
Dalam blog ini, akan di jelaskan tutorial mengenai PHP Lumen Upload Media
Tutorial ini melanjutkan tutorial projek sebelumnya yang ada pada link berikut:
https://nashrulahsanunadiya.blogspot.com/2019/12/tutorial-php-lumen-resource-relationship.html
Jadi, Anda harus menyelesaikan tutorial diatas sebelum mengikuti tutorial ini. Jika sudah melakukan tutorial diatas, silahkan ikuti tulorial PHP Lumen Upload Media berikut.
Membuat Table profiles
Jalankan command berikut ini pada terminal:
php artisan make:migration create_profiles_table
jika sudah, maka hasilnya akan seperti ini:
Buka file database/migrations/...create_profiles_table.php, dan ubah menjadi seperti ini :
Jalankan kembali command dibawah ini pada terminal:
php artisan migrate
maka hasilnya akan seperti ini
Membuat Model Profile
Buat file baru dengan nama app/Models/Profile.php, codenya seperti dibawah ini:
Membuat Fungsi Create dan Update Profile
Buka file routes/web.php, tambahkan code dibawah ini. Lihat line 43.
Buat file baru app/Http/Controllers/ProfilesController.php, code nya seperti dibawah ini.
Test fungsi create or update profiles menggunakan postman dengan menambah data sesuai tabel.
Membuat Fungsi Get Profile
Buka file routes/web.php, tambahkan code dibawah ini, simpan di paling bawah.
Buat file baru app/Http/Controllers/ProfilesController.php, tambahkan function show, code nya seperti dibawah ini.
Jalankan test fungsi get profiles menggunakan postman.
Membuat Fungsi Get Image Profile
Buka file routes/web.php, tambahkan code dibawah ini. Line 58
Buka file app/Http/Controllers/ProfilesController.php, tambahkan fungsi image, code nya seperti dibawah ini.
Test fungsi get image profiles menggunakan postman. Kalau image nya tidak ada seperti ini.
Selesai
Tutorial ini melanjutkan tutorial projek sebelumnya yang ada pada link berikut:
https://nashrulahsanunadiya.blogspot.com/2019/12/tutorial-php-lumen-resource-relationship.html
Jadi, Anda harus menyelesaikan tutorial diatas sebelum mengikuti tutorial ini. Jika sudah melakukan tutorial diatas, silahkan ikuti tulorial PHP Lumen Upload Media berikut.
Membuat Table profiles
Jalankan command berikut ini pada terminal:
php artisan make:migration create_profiles_table
jika sudah, maka hasilnya akan seperti ini:
Buka file database/migrations/...create_profiles_table.php, dan ubah menjadi seperti ini :
Jalankan kembali command dibawah ini pada terminal:
php artisan migrate
maka hasilnya akan seperti ini
Membuat Model Profile
Buat file baru dengan nama app/Models/Profile.php, codenya seperti dibawah ini:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model{
protected $fillable = array('user_id', 'first_name', 'last_name', 'summary', 'image');
public $timestamps = true;
public function user(){
return $this->belongsTo('App\Models\User');
}
}
?>
Membuat Fungsi Create dan Update Profile
Buka file routes/web.php, tambahkan code dibawah ini. Lihat line 43.
Buat file baru app/Http/Controllers/ProfilesController.php, code nya seperti dibawah ini.
<?php
namespace App\Http\Controllers;
use App\Models\Profile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ProfilesController extends Controller{
public function store(Request $request){
$input = $request->all();
$validationRules = [
'first_name' => 'required|min:2',
'last_name' => 'required|min:2',
'summary' => 'required|min:10',
];
$validator = \Validator::make($input, $validationRules);
if ($validator->fails()){
return response()->json($validator->errors(), 400);
}
$profile = Profile::where('user_id', Auth::user()->id)->first();
if(!$profile){
$profile = new Profile;
$profile->user_id = Auth::user()->id;
}
$profile->first_name = $request->input('first_name');
$profile->last_name = $request->input('last_name');
$profile->summary = $request->input('summary');
if ($request->hasFile('image')){
$firstName = str_replace(' ', '_', $request->input('first_name'));
$lastName = str_replace(' ', '_', $request->input('last_name'));
$imagName = Auth::user()->id . '_' . $firstName . '_' . $lastName;
$request->file('image')->move(storage_path('uploads/image_profile'), $imagName);
$current_image_path = storage_path('avatar') . '/' . $profile->image;
if (file_exists($current_image_path)){
unlink($current_image_path);
}
$profile->image = $imagName;
}
$profile->save();
return response()->json($profile, 200);
}
}
?>
Test fungsi create or update profiles menggunakan postman dengan menambah data sesuai tabel.
Membuat Fungsi Get Profile
Buka file routes/web.php, tambahkan code dibawah ini, simpan di paling bawah.
Buat file baru app/Http/Controllers/ProfilesController.php, tambahkan function show, code nya seperti dibawah ini.
Jalankan test fungsi get profiles menggunakan postman.
Membuat Fungsi Get Image Profile
Buka file routes/web.php, tambahkan code dibawah ini. Line 58
Buka file app/Http/Controllers/ProfilesController.php, tambahkan fungsi image, code nya seperti dibawah ini.
Test fungsi get image profiles menggunakan postman. Kalau image nya tidak ada seperti ini.
Selesai


Komentar
Posting Komentar