jeudi 3 mars 2016

Edit form one to many relationship laravel

I have one form that adds data to two different tables (Articles & Deals). An Article has many deals. A deal has one Article. I can't work out how to populate the edit form with the data from the Deals table. I know I'm doing it wrong but at the moment it just populates the edit form with a long array. I think I need to change the Articles Controller and form value variable. At the moment I just have $deals on every deal day.

I'm using Laravel 5.

The Articles Table has: id, title, image, description, address. The Deals table has: id, dealname, article_id, dayID.

FORM:

{!! Form::model($article, ['route' => ['articleUpdate_path', $article->id], 'files' => true, 'method' => 'PATCH']) !!}

    {!! Form::label('title','TITLE') !!}
    {!! Form::text('title', null, ['class' => 'form-control']) !!}
    {!! $errors->first('title','<p class="error">:message</p>')!!}

    {!! Form::label('image','PHOTO') !!}
    {!! Form::file('image', null, ['class' => 'form-control']) !!}

    {!! Form::label('description','DESCRIPTION') !!}
    {!! Form::textarea('description', null, ['class' => 'form-control']) !!}

    {!! Form::label('dealname','Monday') !!}
    {!! Form::text('dealname[]', $deals, null, ['class' => 'form-control']) !!}

    {!! Form::label('dealname','Tuesday') !!}
    {!! Form::text('dealname[]', $deals, null, ['class' => 'form-control']) !!}

    {!! Form::label('dealname','Wednesday') !!}
    {!! Form::text('dealname[]', $deals,null, ['class' => 'form-control']) !!}

    {!! Form::label('address','ADDRESS') !!}
    {!! Form::text('address', null, ['class' => 'form-control']) !!}

    {!! Form::close() !!}

ARTICLES CONTROLLER

public function edit($id)
{
    $article = Article::find($id);
    $deals = Deal::lists('dealname');
    return view('articles.edit', compact('article', 'deals'));
}

ARTICLE MODEL

class Article extends Model
{
    public function deals()
    {
        return $this->hasMany('App\Deal');
    }  
protected $fillable = array('title', 'photo', 'description', 'address'); 
}

DEAL MODEL

class Deal extends Model
{
    public function articles()
    {
        return $this->belongsTo('App\Article')->withTimestamps();
    }
    protected $fillable = array('dealname', 'article_id', 'dayID'); 
}



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

Aucun commentaire:

Enregistrer un commentaire