2016-05-13 11 views
1

こんにちは、私はlaravel apiから投稿で新しいプロフィールを作成しようとしています。すべてのデータは、NULLで埋まる画像ファイルを除いてデータベースに提出されました。私はこれを試しましたSend file with postman to Laravel APIしかし、まだ問題を解決していません。私のヘッダーはAccept:application/jsonとAuthorization:Bearer トークンです。ボディはフォームデータです。郵便配達員を使用してイメージをlaravel apiに送信しますが、nullを取得します。

これはこれはこれは)私は(DDを試してきた私の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); 
    } 
} 

である私の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; 
    } 

} 

である私の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; 
    } 
} 

です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 
} 

私は何が間違っているのかわかりません。私は助けが必要です。ありがとうございました。

答えて

0

あなたは、郵便配達所であなたのリクエストコールからヘッダーを削除する必要があります。 Postmanは要求と応答のためのヘッダを自動的に設定します。

この参照先:Send file with postman to Laravel API

関連する問題