Hi i'm trying to create new profile by post through laravel api. All data was submitted to database except image file that fill with NULL. I've try this Send file with postman to Laravel API but still didn't solve the problem. My header is Accept:application/json and Authorization:Bearer token. Body is form-data.
This is my ProfilesController.php
<?php
namespace App\Http\Controllers\Api\V1;
use App\Acls\ProfilesAcl;
use App\Http\Controllers\Controller;
use App\Repositories\ProfilesRepository;
use App\Profile;
use App\Validators\ProfilesValidator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Response;
class ProfilesController extends Controller
{
//
public function index()
{
return $this->repository->lists();
}
public function store()
{
return $this->repository->create();
}
public function show(Profile $profile)
{
return $profile->load('user');
}
public function update(Profile $profile)
{
return $this->repository->update($profile);
}
public function destroy(Profile $profile)
{
$this->repository->delete($profile);
}
public function __construct(ProfilesRepository $repository, Request $request)
{
parent::__construct();
$this->middleware([
'acl:' . ProfilesAcl::class,
'validate:' . ProfilesValidator::class,
]);
$this->repository = $repository;
$this->request = $request;
}
}
This is my ProfilesRepository.php
<?php
namespace App\Repositories;
use App\Profile;
use App\User;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
class ProfilesRepository extends BaseRepository
{
protected function extractData($extracted = [])
{
$extracted = [];
if ($this->request->has('fullname')) {
$extracted['fullname'] = $this->request->get('fullname');
}
if ($this->request->has('bdate')) {
$extracted['bdate'] = $this->request->get('bdate');
}
if ($this->request->has('numphone')) {
$extracted['numphone'] = $this->request->get('numphone');
}
if ($this->request->hasFile('image')) {
$image_file = $this->request->file('image');
$image_extension = $image_file->getClientOriginalExtension();
$extracted['image_filename'] = $image_file->getFilename().'.'.$image_extension;
$extracted['original_image_filename'] = $image_file->getClientOriginalName();
$extracted['image_mime'] = $image_file->getClientMimeType();
Storage::disk('local')->put($image_file->getFilename().'.'.$image_extension, File::get($image_file));
}
return $extracted;
}
public function lists()
{
$profile = $this->user->profiles();
if ($fullname = $this->request->get('fullname')) {
$profile->where('fullname', 'like', "%{$fullname}%");
}
if ($bdate = $this->request->get('bdate')) {
$profile->where('bdate', 'like', "%{$bdate}%");
}
if ($numphone = $this->request->get('numphone')) {
$profile->where('numphone', 'like', "%{$numphone}%");
}
return $profile->paginate();
}
public function create()
{
return $this->user->profiles()->create($this->extractData());
}
public function update(Profile $profile)
{
$profile->update($this->extractData());
return $profile;
}
public function delete(Profile $profile)
{
$profile->delete();
return $profile;
}
}
This is my Profile.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Profile extends Authenticatable
{
//
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'fullname', 'bdate', 'numphone', 'image',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
I've try dd() at the function store() in the ProfilesController.php and get this
Profile {#276
#fillable: array:4 [
0 => "fullname"
1 => "bdate"
2 => "numphone"
3 => "image"
]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [
"fullname" => "Abdul"
"bdate" => "date"
"numphone" => "0174670850"
"user_id" => 1
"updated_at" => "2016-05-13 17:14:26"
"created_at" => "2016-05-13 17:14:26"
"id" => 11
]
#original: array:7 [
"fullname" => "Abdul"
"bdate" => "date"
"numphone" => "0174670850"
"user_id" => 1
"updated_at" => "2016-05-13 17:14:26"
"created_at" => "2016-05-13 17:14:26"
"id" => 11
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: true
}
I don't know whats wrong and i need help. Thank You.