使用邮递员将图像发送到laravel api但是变为空

时间:2016-05-13 17:54:12

标签: php api laravel-5.2 postman

您好我试图通过laravel api发布新的个人资料。除了填充NULL的图像文件外,所有数据都被提交到数据库。我试过这个Send file with postman to Laravel API,但仍然没有解决问题。我的标题是Accept:application / json和Authorization:Bearer token 。正文是形式数据。

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

}

这是我的个人资料.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);
    }
}

我在ProfilesController.php中的函数store()处尝试dd()并获取此

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
}

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

1 个答案:

答案 0 :(得分:0)

您需要在Postman中删除请求调用中的标头。邮递员会自动为请求和回复设置标题。

请参阅:ctx.action