Binod Paneru
Binod Paneru

Reputation: 113

How to get a value of particular column from the object variable in laravel

From the model TagModel i can filter the value which i want it, but value is stored in the form of object variable. To play with those data in controller i am not able to pluck the particular id. This is my model

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

This is my controller code:

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

the value which i want is stored in the variable $productTagIds which is object variable. How can I pluck just id from this?

Upvotes: 1

Views: 1867

Answers (5)

Vin
Vin

Reputation: 103

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


    }

The code might flash a certain error that have to troubleshoot by yourself. I have kept
$productTagIds[]=$objectTagProduct->id; after $objectTagProduct->save(); please try it

Upvotes: 1

Binod Paneru
Binod Paneru

Reputation: 113

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


    }

I came to knew that to get the data from object class variable $productTagIds[]=$objectTagProduct->id; But i am confused with its flow and where to execute this code.

Upvotes: 0

Binod Paneru
Binod Paneru

Reputation: 113

to get the particular value from the object class variable

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

Upvotes: 0

Bhargav Kaklotara
Bhargav Kaklotara

Reputation: 1458

public static function getLatestTag($name){
    return App\TagModel::where('is_deleted',0)->where('name',$name)->first()->id;
}

$ids = [];
foreach ($newTags as $newTag) {
   $productTagId = TagModel::getLatestTag($newTag);
   array_push($ids,$productTagId);
}
 $productId = (int)ProductManagementModel::getMaxId();
 foreach ($ids as $productTagId) {
    $productTag = new ProductTagModel;
    $productTag ->product_id = $productId;
    $productTag ->tag_id = $productTagId;
    $productTag->save();    
 }

try to use eloquent like this

Upvotes: 0

Bhargav Kaklotara
Bhargav Kaklotara

Reputation: 1458

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

Upvotes: 0

Related Questions