如何从laravel

时间:2018-01-02 11:12:19

标签: php laravel

从模型TagModel我可以过滤我想要的值,但值以对象变量的形式存储。要在控制器中使用这些数据,我无法获取特定的ID。 这是我的模特

public static function getLatestTag($name){
    return $tagIds =DB::table('tags')
                    ->where ('is_deleted',0)
                    ->where('name',$name)
                    ->get();
}

这是我的控制器代码:

foreach ($newTags as $newTag) {
                $productTagIds[] = TagModel::getLatestTag($newTag);
                dd($id);
            }

            $productId = (int)ProductManagementModel::getMaxId();
            foreach ($productTagIds as $productTagId) {
            $productTag = new ProductTagModel;
            $productTag ->product_id = $productId;
            $productTag ->tag_id = $productTagId;
            $productTag->save();    
            }

我想要的值存储在变量 $ productTagIds 中,它是对象变量。我该如何从中获取id?

5 个答案:

答案 0 :(得分:1)

public function addProduct()
    {       
            foreach(Input::get('tagName') as $checkTag){

                $newTags[]=$checkTag;
            }
            foreach ($newTags as $newTag) {
                if(TagModel::checkExist($newTag)){
                    $tagExist[] =  TagModel::checkExist($newTag);
                    $message = 'The tag <b>'.$checkTag.'</b>  already exist';
                Session::flash('class', 'alert alert-error');
                Session::flash('message', $message);    
                return View::make('admin.product_management.add');
                }
                else {  

                        $objectTagProduct = new TagModel;
                        $objectTagProduct ->name = $newTag;
                        $objectTagProduct->save();
                        $productTagIds[]=$objectTagProduct->id;

                }
            }
                $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; 
                }

                $objectProduct->save();

                $productId = (int)ProductManagementModel::getMaxId();
                //dd($productId);
                foreach ($productTagIds as $productTagId) {
                    //dd($productTagIds);
                $productTag = new ProductTagModel;
                $productTag ->product_id = $productId;
                $productTag ->tag_id = $productTagId;

                $productTag->save();    
                }


                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');
                }


    }

代码可能会闪现某个必须自行排除故障的错误。我一直保持着 $productTagIds[]=$objectTagProduct->id;$objectTagProduct->save();之后,请尝试

答案 1 :(得分:0)

public static function getLatestTag($name){
    return $tagIds = DB::table('tags')
                    ->where ('is_deleted',0)
                    ->where('name',$name)
                    ->pluck(id);
}

答案 2 :(得分:0)

(async function () {
  try {
    console.log("sql connecting......")
    let pool = await sql.connect(sqlConfig)
    let result = await pool.request()
      .query('select * from Subject')  // subject is my database table name

    console.log(result )

  } catch (err) {
    console.log(err);
  }
})()

尝试像这样使用雄辩的

答案 3 :(得分:0)

从对象类变量中获取特定值

$productTagIds[]=$objectTagProduct->id;`

答案 4 :(得分:0)

public function addProduct()
    {       
            foreach(Input::get('tagName') as $checkTag){

                $newTags[]=$checkTag;
            }
            foreach ($newTags as $newTag) {
                if(TagModel::checkExist($newTag)){
                    $tagExist[] =  TagModel::checkExist($newTag);
                    $message = 'The tag <b>'.$checkTag.'</b>  already exist';
                Session::flash('class', 'alert alert-error');
                Session::flash('message', $message);    
                return View::make('admin.product_management.add');
                }
                else {  

                        $objectTagProduct = new TagModel;
                        $objectTagProduct ->name = $newTag;
                        $objectTagProduct->save();

                }
            }
                $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; 
                }

                $objectProduct->save();

                $productId = (int)ProductManagementModel::getMaxId();
                //dd($productId);
                foreach ($productTagIds as $productTagId) {
                    //dd($productTagIds);
                $productTag = new ProductTagModel;
                $productTag ->product_id = $productId;
                $productTag ->tag_id = $productTagId;
                $productTagIds[]=$objectTagProduct->id;
                $productTag->save();    
                }


                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');
                }


    }

我开始知道从对象类变量$productTagIds[]=$objectTagProduct->id; 获取数据 但我对它的流程以及执行此代码的位置感到困惑。