首页 文章

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

提问于
浏览
2

嗨,我正试图通过 laravel api 发布新的个人资料。除了填充 NULL 的图像文件外,所有数据都被提交到数据库。我试试这个使用邮递员将文件发送到 Laravel API但仍然没有解决问题。我的标题是 Accept:application/json 和 Authorization:Bearer 标记。身体是 form-data。

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

我在 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 回答

相关问题