jeudi 30 novembre 2017

laravel perform two query in one function

Simply i have a two table

GALLARIES AND MEDIA

In a GALLARIES table id,title,venueId i have saved gallary folder name for the particular venue. In MEDIA Table I have id,imagepath,is_thumb(0 or 1),gallery_Id

What i want to do is when i set is_thumb_image(1) then i have call two function

1 st for unset image all with gallery_id and after i call second function for set is_thumb_image for particular image.

Is it possible to call one function only and perform both functionalty.

Here is my Controller code.

        $albumId = $request->album_id; //table galleries id  - album name
        if($request->is_thumb_image == "true") { 
            $media1->UnsetThumbImage($albumId); // first unset thumb_image 
            $media->setThumbImage($media->id); // call for set thumb_image 
        } else {
            $request->is_banner_image = false;
        }

Here is my model functions

 public function setThumbImage($mediaId) {
   try {
        DB::table('media')
            ->where('id', $mediaId)
            ->update(['is_thumb_image' => 1]);
        $this->is_thumb_image = 1;
    } catch (\Exception $ex) {
        echo $ex->getMessage();
        dd($ex->getTraceAsString());
    }
}

public function UnsetThumbImage($albumid) {
    DB::table('media')
    ->where('gallery_id', $albumid)
    ->update(['is_thumb_image' => 0]);
    $this->is_thumb_image = 1;
}

How can i do it calling only one function.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire