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 InsertPortfolio() {
$portfolio = Input::except('_token');
$portfolio['id'] = Input::get('id');
$image['id_portfolio'] = Input::get('id');
$validation = Validator::make($portfolio, Portfolio::$portfolio, ImagePortfolio::$image);
if ($validation->passes()) {
if($user = Portfolio::find($portfolio['id'])) {
$user -> update($portfolio);
Session::flash('editportfolio', 'Portfolio edit with success');
return Redirect::to('backend/portfolio');
}else{
if($request->hasFile('image')){
$file = array_get($portfolio,'image');
$destinationPath = 'image/profile/';
$extension = $request->file('image')->getClientOriginalExtension();
$fileName = rand(1111,9999) . '.' . $extension;
$file['image'] = $fileName;
Image::make($file)->resize(400, 400)->save($destinationPath.$fileName);
}
$user = Portfolio::insert($portfolio);
}
Session::flash('portfolio', 'Portfolio insert with success');
return Redirect::to('backend/portfolio');
} else {
return Redirect::to('backend/portfolio/edit/'.$portfolio['id'])->withInput()->withErrors($validation);
}
}
Model
class ImagePortfolio extends Model
{
protected $table = 'imageportfolio';
protected $fillable = ['id','id_portfolio','image'];
public static $imageportfolio = array(
'image' => 'image|max:3000|mimes:jpeg,jpg,png'
);
}
class Portfolio extends Model
{
protected $table = 'portfolio';
protected $fillable = ['id','title','year','description','sex'];
public function imageportfolio(){
return $this->hasMany('App\ImagePortfolio','id_portfolio','id')->first();
}
public static $portfolio = array(
'title' => 'required',
'year' => 'required',
'description' => 'required',
'sex' => 'required',
);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire