简体   繁体   English

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

[英]Laravel repository store image name does not work

I'm trying to create a user profile and store it into database using laravel repositories . 我正在尝试创建一个用户配置文件,并使用laravel存储库将其存储到数据库中。 below is my controller code : 下面是我的控制器代码:

<?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();
        //
    }
}

The model is also as below 该模型也如下

<?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',
    ];
}

My repository 我的资料库

<?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();
    }
}

In my save function when i simply write $user->image=inputs['image'] it works but instead of the image name its store a path to my socket . 在我的保存函数中,当我简单地编写$user->image=inputs['image']它就起作用了,但是它代替了图像名,而是存储了我套接字的路径。 how can i use getClientOriginalName() here to get the client image and store it in the database ? 我如何在这里使用getClientOriginalName()来获取客户端图像并将其存储在数据库中?

any idea ? 任何想法 ?

Thanks 谢谢

Change your store method call to this. 将您的存储方法调用更改为此。

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

PS : The loop checking if the file already exists is useless. PS:循环检查文件是否已经存在是没有用的。 The loop will never end if an image with the same name as the uploaded file already exists. 如果已经存在与上传文件同名的图像,则循环永远不会结束。

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

To fix this you should throw in some random name generator here. 要解决此问题,您应该在此处添加一些随机名称生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM