mercredi 25 novembre 2015

Multiple image in different tables laravel 5

I intend to create in my backend where it is possible to create workshops. In the workshops we have some data more images. How are multiple images created a table images with their relationship with the workshops. I can enter and edit the table workshops but when I want to insert images with the relationship with the portfolio I am no longer able. Someone can help me?

Controller

    public function InserirPortfolio() { 
        $portfolio = Input::except('_token'); 
        $portfolio['id'] = Input::get('id');
        $imagens['id_portfolio'] = Input::get('id');
        $validation = Validator::make($portfolio, Portfolio::$portfolio, ImagensPortfolio::$imagens); 
        if ($validation->passes()) {
            if($user = Portfolio::find($portfolio['id'])) {
                $user -> update($portfolio); 
                Session::flash('editarportfolio', 'Portfolio editado com sucesso'); 
                return Redirect::to('backend/portfolio');                   
            }else{
                if($request->hasFile('imagem')){
                    $file = array_get($profileData,'imagem');
                    $destinationPath = 'imagens/perfil/';
                    $extension =  $request->file('imagem')->getClientOriginalExtension();
                    $fileName = rand(1111,9999) . '.' . $extension;
                    $file['imagem'] = $fileName;
                    Image::make($file)->resize(400, 400)->save($destinationPath.$fileName);
                }                   
                $user = Portfolio::insert($portfolio); 
            }
            Session::flash('portfolio', 'Portfolio inserido com sucesso'); 
            return Redirect::to('backend/portfolio');
        } else { 
            return Redirect::to('backend/portfolio/editar/'.$portfolio['id'])->withInput()->withErrors($validation); 
        }   
    }

Model

class ImagensPortfolio extends Model
{
    protected $table = 'imagensportfolio';

protected $fillable = ['id','id_portfolio','imagens'];

public static $imagensportfolio = array(
        'imagens' => 'image|max:3000|mimes:jpeg,jpg,png'
        );      
}




 class Portfolio extends Model
{
    protected $table = 'portfolio';

    protected $fillable = ['id','titulo','ano','descricao','genero'];

    public function imagensportfolio(){ 
        return $this->hasMany('App\ImagensPortfolio','id_portfolio','id')->first(); 
    }

    public static $portfolio = array(
            'titulo' =>  'required',
            'ano' =>  'required',
            'descricao' =>  'required',
            'genero' =>  'required',
            );      
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1PZWobE
via IFTTT

Aucun commentaire:

Enregistrer un commentaire