2017-12-18 54 views
-3

這兩個表tbl_product_managertbl_tags具有多對多關係。我用雄辯來建立相應模型之間的關係。我能夠在這兩個表中插入數據,但問題是數據透視表沒有相應更新。

Controller.php這樣:在數據透視表中插入數據

public function addProduct() 
    { 
     $rules = array('product_name' => 'required', 
         'product_url' => 'required'); 
     $validator = Validator::make(Input::all(), $rules); 
     if($validator->fails()){ 
      Session::flash('class', 'alert alert-error'); 
      Session::flash('message', 'Some fields are missing'); 
      return View::make('admin.product.add'); 
     } 
     else { 
      $productName = Input::get('product_name'); 
      $productUrl = Input::get('product_url'); 
      $productUrl = preg_replace('/[^A-Za-z0-9\-]/', '', $productUrl); 
      $productExist = ProductManagementModel::checkExist($productUrl); 
      if(count($productExist)!=0) { 
       $message = 'product <b>'.$productName.'</b> with url <b>'.$productUrl.'</b> is already exist'; 
       Session::flash('class', 'alert alert-error'); 
       Session::flash('message', $message);  
       return View::make('admin.product.add'); 
      } 
      else { 
       $imageFile = Input::file('userfile'); 
       $destinationPath = 'uploads/products/'; 
       $rEFileTypes = "/^\.(jpg|jpeg|gif|png){1}$/i"; 
       $maximum_filesize = 1 * 1024 * 1024; 

       if($imageFile) { 
        $filename = $imageFile->getClientOriginalName(); 
        $extension = strrchr($filename, '.'); 
        $size = $imageFile->getSize();     
        $new_image_name = "products" . "_" . time(); 

        if ($size <= $maximum_filesize && preg_match($rEFileTypes, $extension)) { 
         $attachment = $imageFile->move($destinationPath, $new_image_name.$extension);      
        } else if (preg_match($rEFileTypes, $extension) == false) { 
         Session::flash('class', 'alert alert-error'); 
         Session::flash('message', 'Warning : Invalid Image File!'); 
         return View::make('admin.product_management.add'); 
        } else if ($size > $maximum_filesize) { 
         Session::flash('class', 'alert alert-error'); 
         Session::flash('message', "Warning : The size of the image shouldn't be more than 1MB!"); 
         return View::make('admin.product_management.add'); 
        }    
       } 
       $logo = isset($attachment) ? $new_image_name . $extension : NULL; 

       $objectProduct = new ProductManagementModel; 
       $objectProduct->product_name  = Input::get('product_name'); 
       $objectProduct->product_url   = $productUrl; 
       $objectProduct->category_id   = Input::get('category_id'); 
       $objectProduct->product_cost  = Input::get('product_cost'); 
       $objectProduct->product_short_description = Input::get('product_short_description'); 
       $objectProduct->product_description = Input::get('product_description'); 
       $objectProduct->is_active = Input::get('is_active'); 
       $objectProduct->created_at = Auth::user()->id; 
       $objectProduct->updated_at = Auth::user()->id; 

       if($logo != '') 
       { 
       $objectProduct->product_attachment = $logo; 
       } 
       $objectTags = new TagModel; 
       $objectTags->size_id = Input::get('size_id'); 
       $objectTags->brand_id = Input::get('brand_id'); 
       $objectTags->color_id = Input::get('color_id'); 
       $objectTags->food_id = Input::get('food_id'); 
       $objectTags->save(); 

       //$tag = new TagModel::all(); 

       $objectProduct->save(); 

       if(isset($request->tags)) { 
        $post->Tags()->sync($request->tags, false); 
       } 
       if($objectProduct->id) { 
        Session::flash('class', 'alert alert-success'); 
        Session::flash('message', 'Product successfully added'); 
        return View::make('admin.product_management.add'); 
       } else { 
        Session::flash('class', 'alert alert-error'); 
        Session::flash('message', 'Something error'); 
        return View::make('admin.product_management.add'); 
       }    
      }   
     } 
    } 

ProductManagementModel.php

<?php 

use Illuminate\Auth\UserTrait; 
use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableTrait; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class ProductManagementModel extends Eloquent implements UserInterface, RemindableInterface { 

use UserTrait, RemindableTrait; 
protected $table = 'product_manager'; 

public function Tags(){ 
    return $this->belongsToMany('TagModel', 'product_tag', 'product_id', 'tag_id'); 
} 

public function Categories(){ 
    return $this->hasOne('CategoriesModel', 'id'); 
} 

public static function getAllProducts(){ 
     return $products = ProductManagementModel::with('categories','tags')->get(); 
} 

public static function checkExist($url) 
{  
    return $products = DB::table('product_manager') 
       ->where('is_deleted', 0) 
       ->where('product_url', $url) 
       ->first();    
} 

}

TagModel.php

<?php 
use Illuminate\Auth\UserTrait; 
use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableTrait; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class TagModel extends Eloquent implements UserInterface, RemindableInterface { 

    use UserTrait, RemindableTrait; 
    protected $table = 'tag'; 
    public function ProductManagents() { 
     return $this->belongsToMany('ProductManagentModel'); 
    } 

    public function Color(){ 
     return $this->hasOne('ColorModel', 'color_id'); 
    } 
    public function Brand() { 
     return $this->hasOne('BrandproModel','brand_id'); 
    } 

    public function size() { 
     return $this->hasOne('SizeModel','size_id'); 
    } 

    public function food() { 
     return $this->hasOne('FoodModel','food_id'); 
    } 

} 

在我的研究中,我發現使用同步函數將適用於更新數據透視表。但我沒有使用它。 我期待解決這個問題或者找出解決方案的新方法。 在此先感謝。

回答