Reputation: 815
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');
}
Upvotes: 2
Views: 1019
Reputation: 217
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).
Upvotes: 1