简体   繁体   中英

How to populate 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 the Laravel 5 - Query Builder documentation:

$roles = DB::table('roles')->lists('title');

This method will return an array of role titles.

Since you are simply returning the $deals variable, your output will be the array returned by the above.

In order to output the individual deals, you should use a "foreach" loop within the view to output the specific variables of each deal (as QuickDanger mentions in his comment).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM