mercredi 29 novembre 2017

In Laravel Eloquent create method I am not able to save the data in database

I have Three models:

  • Language
  • Article
  • Category

The Article table has two foreign keys category_id and Language_id. Language-Model has "One to Many" Relationship with Article-Model, Similarly Category-Model has "One to Many" Relationship with Article-Model.

My Category model:

class Category extends Model
            { 

            protected $fillable = [
                 'category_name', 
            ];

            public function articles(){

                return $this->hasMany('App\Article');
             }
           }

My Language model:

class Language extends Model
    {
         protected $fillable = [
            'language_name'
        ];

         public function articles(){

            return $this->hasMany('App\Article');
        }

    }

My Article model:

class Article extends Model
{
 protected $fillable = [

   'language_id','category_id','category_name','article_title',
   'article_desc','source_from','source_url','video_link',
   'audio_url','author_name','article_image_url','like_count'
    ];

    public function languages(){

        return $this->belongsTo('App\Language');
    }

    public function categories(){

        return $this->belongsTo('App\Category');
    }

}

How can I insert in the database using Laravel Eloquent?

 $Article = new Article (
                 [
                   'article_image_url' => $img_url ,
                   'audio_url' => $audio_url,
                   'category_name'=>$category_name ,
                   'article_title'=>$request->article_title,
                   'article_desc'=>$request->article_desc,
                   'source_from'=>$request->source_from,
                    'source_url'=>$request->source_url,
                    'video_link'=>$request->video_link,
                    'author_name'=>$request->author_name,
                    'like_count'=>'0',

                  ]
            );

            $language = new Language;
            $category = new Category;
            $language->articles()->save($Article);

language_id doesn't have a default value; it is foreign key.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ilr5kk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire