2016-05-13 64 views
1

嗨,我试图创建新的配置文件,通过邮件通过laravel api。所有的数据都被提交给数据库,除了填充NULL的图像文件。我试过这个Send file with postman to Laravel API但仍然没有解决问题。我的头文件是Accept:application/json和Authorization:Bearer token。正文是表单数据。发送图像到laravel api使用邮差,但变得空

这是我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; 
    } 
} 

这是我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; 
    } 

} 

这是我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); 
    } 
} 

我已经尝试DD()在ProfilesController.php中的函数store()处得到这个

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 
} 

我不知道什么是错,我需要帮助。谢谢。

回答

相关问题