Posts

Showing posts with the label laravel association

Download pdf by ajax or javascript or angular js

Click on button for generate pdf  $("#print_selected_label").on("click", function(){    data = []    $("input:checkbox.print_label:checked").each(function(){      data.push($(this).attr("id"));    });   //getting all information of checked checkbox    if(data.length > 0){    // if data is not null       In angular js controller         // var redirect_new = "/admin/v2/orders/print_multiple_label.pdf?" + jQuery.param(data);        // var pdf = window.open(redirect_new, 'Map PDF', '');      By Creating a link by javascript       // var mydiv = document.getElementById("download_pdf");      // var aTag = document.createElement('a');      // var link = " admin/v2/orders/print_multiple_label.pdf?id="+data ;      // aTag.setAttribute('href',link);      // aTag.innerHTML = "link text";      // mydiv.appendChild(aTag);      // aTag.click(); By making http request    

how to delete associated record in laravel

There are two ways to delete associated record or related record. First way is  Suppose we have two models Category and Product. we have association like category has many products. our Category model like this class Category extends Model {     /**      * Get the comments for the blog post.      */     public function products()     {         return $this->hasMany('App\Product', 'category_id');     } } our Product model like this class Product extends Model {     /**      * Get the comments for the blog post.      */     public function category()     {         return $this->belongsTo('App\Category');     } } so write this in your category model. public static function boot() {         parent::boot();         static::deleting(function($category) { // before delete() method call this              $category->products()->delete();              // do the rest of the cleanup.