Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 在数据透视表中插入数据_Laravel - Fatal编程技术网

Laravel 在数据透视表中插入数据

Laravel 在数据透视表中插入数据,laravel,Laravel,具有多对多关系的两个表tbl_product_manager和tbl_tags。我用雄辩的口才做出了相应模型之间的关系。我可以在这两个表中插入数据,但问题是数据透视表没有相应地更新。 Controller.php: 公共函数addProduct() { $rules=array('product_name'=>“必需”, “产品url”=>“必需”); $validator=validator::make(输入::all(),$rules); 如果($validator->fails()){

具有多对多关系的两个表tbl_product_manager和tbl_tags。我用雄辩的口才做出了相应模型之间的关系。我可以在这两个表中插入数据,但问题是数据透视表没有相应地更新。

Controller.php:

公共函数addProduct()
{
$rules=array('product_name'=>“必需”,
“产品url”=>“必需”);
$validator=validator::make(输入::all(),$rules);
如果($validator->fails()){
会话::flash('class','alert error');
会话::flash('消息','某些字段丢失');
返回视图::make('admin.product.add');
} 
否则{
$productName=Input::get('product_name');
$productUrl=Input::get('product_url');
$productUrl=preg_replace('/[^A-Za-z0-9\-]/','','$productUrl);
$productExist=ProductManagementModel::checkExist($productUrl);
如果(计数($productExist)!=0){
$message='product'.$productName.'with url'.$productUrl.'已存在';
会话::flash('class','alert error');
会话::flash('message',$message);
返回视图::make('admin.product.add');
}
否则{
$imageFile=Input::file('userfile');
$destinationPath='uploads/products/';
$rEFileTypes=“/^\(jpg | jpeg | gif | png){1}$/i”;
$maximum_filesize=1*1024*1024;
如果($imageFile){
$filename=$imageFile->getClientOriginalName();
$extension=strrchr($filename,'.');
$size=$imageFile->getSize();
$new_image_name=“products.”.time();
if($size move($destinationPath,$new\u image\u name.$extension);
}else if(preg_match($rEFileTypes,$extension)=false){
会话::flash('class','alert error');
会话::flash('message','Warning:无效的图像文件!');
返回视图::make('admin.product_management.add');
}else if($size>$maximum\u filesize){
会话::flash('class','alert error');
会话::flash('消息',“警告:图像大小不应超过1MB!”);
返回视图::make('admin.product_management.add');
}               
}
$logo=isset($attachment)$new\u image\u name.$extension:NULL;
$objectProduct=新产品管理模型;
$objectProduct->product_name=Input::get('product_name');
$objectProduct->product\u 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;
如果($logo!='')
{
$objectProduct->product_attachment=$logo;
}
$objectTags=新的TagModel;
$objectTags->size\u id=Input::get('size\u id');
$objectTags->brand_id=Input::get('brand_id');
$objectTags->color\u id=Input::get('color\u id');
$objectTags->food_id=Input::get('food_id');
$objectTags->save();
//$tag=新的TagModel::all();
$objectProduct->save();
if(设置($request->tags)){
$post->Tags()->sync($request->Tags,false);
}
如果($objectProduct->id){
会话::flash(“类”,“警报成功”);
会话::flash(“消息”,“产品已成功添加”);
返回视图::make('admin.product_management.add');
}否则{
会话::flash('class','alert error');
会话::flash('message','Something error');
返回视图::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();              
}

查看附加、分离或同步方法:

请注意,如果您尊重雄辩的命名约定,这将更容易实现

我正在遵循类似的命名约定。最近我按照上面链接中的说明进行了操作。但我仍然无法更新透视表。
 <?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();              
}
<?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');
    }

}