mercredi 7 décembre 2016

Storing to a reference table with a foreign key (LARAVEL 5.2)

I just can't figure it out, and I'm getting mad.

I have a projects table that have a multiple upload file images. My goal is storing the array of images filenames on a reference table that will basically have 2 columns: ID_PROJECT (foreign key) and IMAGE_NAME

I set my Gallery model

class Gallery extends Model
{
    public function project()
    {
    return $this->hasMany('App\Project');
    } 
}

I set my Project model

class Project extends Model
{
    public function gallery()
    {
    return $this->belongsTo('App\Gallery');
    } 
}

on my project controller...

$post = new Project;
$post->title = $data->title;
$post->save();

$post->gallery()->associate($filename); !! $filename is an array !!

I know it's wrong. I'm just super confused how to deal with relationships in Laravel



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire