简体   繁体   中英

Laravel repository store image name does not work

I'm trying to create a user profile and store it into database using laravel repositories . 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 . how can i use getClientOriginalName() here to get the client image and store it in the database ?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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