Laravel存储库存储库映像名称不起作用

时间:2017-06-02 18:30:33

标签: php laravel laravel-5.3

我正在尝试使用laravel存储库创建用户配置文件并将其存储到数据库中。  下面是我的控制器代码:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UsercreateRequest;
use App\Http\Requests\UserupdateRequest;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;

class UserController extends Controller
{

    protected $userRepository;
    protected $nbrPerPage=4;


    public function __construct(UserRepository $UserRepository)
    {
        $this->userRepository=$UserRepository;
    }

    public function index()
    {
        return view('signup');
        //
    }


    public function create()
    {
        return view('signup');
        //
    }

    public function store(UsercreateRequest $request)
    {

        $image =$request->file('image');
        if($request->hasFile('image'))
        {
            if($image->isValid())
            {

                $way=public_path('images');

                $extension=$image->getClientOriginalExtension();

                do
                {
                    $name=$image->getClientOriginalName();
                    //echo $name;
                }while(file_exists($way.'/'.$name));

                if($image->move($way,$name))
                {

                    echo'ok '; //75485205
                    //echo $name;
                    $user=$this->userRepository->store($request->all(), $request);
                    return redirect('dashboard')->withOk(" L'enrisgrement n'a pas abouti !");
                }

            }

        }
        return redirect('signup')->withOk(" L'enrisgrement n'a pas abouti !");
        //
    }

    public function show($id)
    {
        $user=$this->userRepository->getByid($id);

        return view('dashboard', compact('user'));
        //
    }

    public function edit($id)
    {
        $user=$this->userRepository->getByid($id);

        return view('dashboard', compact('user'));
        //
    }

    public function update(UserupdateRequest $request, $id)
    {
        $this->userRepository->update($id, $request->all());

        return view('dashboard');
        //
    }

    public function destroy($id)
    {
        $this->userRepository->destroy($id);
        return back();
        //
    }
}

模型如下

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','image',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

我的存储库

<?php

namespace App\Repositories;

use App\User;
use App\Http\Requests\UsercreateRequest;

class UserRepository{


    protected $user;

    public function __construc(User $user)
    {
        $this ->user=$user;
    }
    private function save (User $user, Array $inputs, UsercreateRequest $request)
    {


        $user->name=$inputs['name'];
        $user->email=$inputs['email'];
        $image=$request->file('image');
        $name=$image->getClientOriginalName();
        $user->image=$name;
        $user->save();
    }
    public function store(Array $inputs,UsercreateRequest $request)
    {
        $user= new User();

        $user->password=bcrypt($inputs['password']);
        $this->save($user,$inputs,$request);
    }
    public function getByid ($id)
    {
        return $this->user->findOrfail($id);
    }
    public function update($id, Array $inputs)
    {
        $this->save($this->getByid($id),$inputs);
    }

    public function destroy ($id)
    {
        $this->getByid($id)->delete();
    }
}

在我的保存功能中,当我简单地写$user->image=inputs['image']时,它可以工作但不是图像名称,它存储了我的套接字的路径。我如何在这里使用getClientOriginalName()来获取客户端图像并将其存储在数据库中?

任何想法?

谢谢

1 个答案:

答案 0 :(得分:0)

将商店方法调用更改为此。

$data = array_merge($request->all(), ['image' => $name]);
$user = $this->userRepository->store($data, $request);

PS:检查文件是否已存在的循环无用。如果已存在与上传文件同名的图像,则循环将永远不会结束。

do {
    $name = $image->getClientOriginalName();
} while(file_exists($way.'/'.$name));

要解决这个问题,你应该在这里输入一些随机名称生成器。