I have one form that adds data to two different tables (Articles & Deals). An Article has many deals. A deal has one Article. There are multiple deals with different dealnames that the user inputs on the create and edit form. I can update the 'deals' part of my database fine in my local/vagrant dev environment but I get a 'Creating default object from empty value' error when I try on my live site.
It says the problem is in the update function of my Articles Controller.
I'm using Laravel 5.
The Articles Table has: id(primary), title, image, description, address.
The Deals table has: id(primary), dealname, article_id (index), dayID.
The only difference I can see between my dev and live environment is that the index (article_id) on the 'Deals' table doesn't have a key icon next to it in PHPMyAdmin. But the foreign key r/ship is set correctly.
You can see all the code here: https://github.com/lakemck/gethappy
Articles Controller- Update
public function update(ArticleRequest $request, $id)
{
$article = Article::findOrFail($id);
if( $request->hasFile('image') ){
// photo saving stuff.
}
$article->update($request->all());
for($i = 0; $i < sizeof($request->input('dealname')); $i++) {
//Error is supposedly on the next line.
$article->deals->where('dayID',($i + 1))->first()->dealname = $request->input('dealname')[$i];
$article->deals->where('dayID',($i + 1))->first()->save();
}
return redirect('/');
}
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']) !!}
#foreach ($article->deals as $deal)
#if($deal->dayID == '1' )
{!! Form::label('dealname','Monday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '1']) !!}
#endif
#if($deal->dayID == '2' )
{!! Form::label('dealname','Tuesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '2']) !!}
#endif
#if($deal->dayID == '3' )
{!! Form::label('dealname','Wednesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '3']) !!}
#endif
#endforeach
{!! Form::label('address','ADDRESS') !!}
{!! Form::text('address', null, ['class' => 'form-control']) !!}
{!! Form::close() !!}
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 article()
{
return $this->belongsTo('App\Article')->withTimestamps();
}
protected $fillable = array('dealname', 'article_id', 'dayID');
}
In the end I had to change the code in my ArticlesController update function to this:
for($i = 0; $i < sizeof($request->input('dealname')); $i++) {
$deal = $article->deals()->where('dayID', $i + 1)->first();
$deal->dealname = $request->input('dealname')[$i];
$deal->save();
}
return redirect('/');
}
Note the brackets on the deals().
Related
How to check is form was submitted?
view:
{!! Form::open(['method' => 'GET', 'action' => 'JobController#index']) !!}
<div class="form-group">
{!! Form::label('keywords','Keywords' ) !!}
{!! Form::text('keywords', null, array('class' => 'form-control')) !!}
</div>
{!! Form::submit('submit', array('class' => 'btn btn-primary form-control ')) !!}
{!! Form::close() !!}
Controller:
public function index(Request $request) {
// if(!empty($request->keywords)) {
//
// $search = Post::where("keywords","LIKE","%{$request->keywords}%")
// ->paginate(10);
// }
if(!empty($request->submit)) {
$search = Post::where("keywords","LIKE","%{$request->keywords}%")
->paginate(10);
}
}
$request->keywords is working fine. $request->submit shows nothing.
Is this constuction do not work in Laravel?
Thanks!
You can pass the name and value in the array as:
{!! Form::submit('submit', array('class' => 'btn btn-primary form-control', 'name'=>'submit', 'value'=>'save')) !!}
And use it in controller as:
if($request->submit == 'save') {
}
UPDATED. I have one form that adds data to two different tables (Articles & Deals). An Article has many deals. A deal has one Article. There are multiple deals with different dealnames that the user inputs on the create and edit form. I can create an Article with many Deals fine, and I can populate the edit form with the data from the Deals table, but when I update my 'deals' table using the Articles Controller it just updates every 'dealname' with the last dealname that is inputted. I only need to update the 'dealname' column as all other columns will stay the same. If I remove the dealname/deals part of the form I can update fine.
How can I update the deals table correctly? I know I have to change something in the update function of my Articles Controller.
I'm using Laravel 5.
The Articles Table has: id, title, image, description, address. The Deals table has: id, dealname, article_id, dayID.
Articles Controller- Update
public function update(ArticleRequest $request, $id)
{
$article = Article::find($id);
if( $request->hasFile('image') ){
// photo saving stuff.
}
$article->fill($request->input())->save();
//Get IDs of deals to be updated.
$dealID = Deal::all()->lists('dealname', 'id');
$dealID = $dealID->toArray();
$dealID = array_keys($dealID);
$deals = $request->input('dealname');
foreach($deals as $deal) {
Deal::whereIn('id', $dealID)->update(['dealname' => $deal]);
}
return redirect('/');
}
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']) !!}
#foreach ($article->deals as $deal)
#if($deal->dayID == '1' )
{!! Form::label('dealname','Monday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '1']) !!}
#endif
#if($deal->dayID == '2' )
{!! Form::label('dealname','Tuesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '2']) !!}
#endif
#if($deal->dayID == '3' )
{!! Form::label('dealname','Wednesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '3']) !!}
#endif
#endforeach
{!! Form::label('address','ADDRESS') !!}
{!! Form::text('address', null, ['class' => 'form-control']) !!}
{!! Form::close() !!}
Articles Controller -Store
public function store(ArticleRequest $request)
{
$image_name = $request->file('image')->getClientOriginalName();
$request->file('image')->move(base_path().'/public/images', $image_name);
$article = ($request->except(['image']));
$article['image'] = $image_name;
$article = Article::create($article);
// GET INPUT
$deals = $request->input('dealname');
// GET ID OF ARTICLE
$articleID = $article->id;
// N is the day id that increments
$n = 1;
foreach($deals as $deal) {
Deal::create(['dealname' => $deal, 'article_id' => $articleID, 'dayID' => $n++]);
}
return redirect()->route('articles_path');
}
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 article()
{
return $this->belongsTo('App\Article')->withTimestamps();
}
protected $fillable = array('dealname', 'article_id', 'dayID');
}
I'm really not sure to fully understand your question, but would something like that could be useful in your case:
public function update(ArticleRequest $request, $id) {
$article = Article::findOrFail($id);
if( $request->hasFile('image') ){
// photo saving stuff.
}
$article->update($request->all());
$article->deals->where('dayID',1)->first()->dealname = $request->input('dealname')[0];
$article->deals->where('dayID',1)->first()->save();
$article->deals->where('dayID',2)->first()->dealname = $request->input('dealname')[1];
$article->deals->where('dayID',2)->first()->save();
$article->deals->where('dayID',3)->first()->dealname = $request->input('dealname')[2];
$article->deals->where('dayID',3)->first()->save();
}
Are they only those 3 dayIds you are using in your form?
EDIT:
You could also try with a for loop. This is untested code, so you might want to optimize it :)
public function update(ArticleRequest $request, $id) {
$article = Article::findOrFail($id);
if( $request->hasFile('image') ){
// photo saving stuff.
}
$article->update($request->all());
for($i = 0; $i < sizeof($request->input('dealname')); $i++) {
$article->deals->where('dayID',($i + 1))->first()->dealname = $request->input('dealname')[$i];
$article->deals->where('dayID',($i + 1))->first()->save();
}
}
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).
In the admin area of my app, I want to have a form by which an admin can find a project by its id and show the project details there. What is the best approach to implement this?
This is what I have tried:
//route
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
//form
{!! Form::open(['action' => 'AdminController#showProject', 'method' => 'get']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
//controller method
public function showProject(Request $request)
{
$project=Project::find($request->get('project_id'));
return view('admin.projects.showProject', compact('project'));
}
It almost worked but there is little problem. After retrieving the requested project, the ULR is like this:
admin/projects/%7Bproject_id%7D?project_id=5
I want it be like this one:
admin/projects/5
How can I solve this problem?
Create the following routes:
Route::get('admin/projects', 'AdminController#getProject');
Route::post('admin/projects', 'AdminController#postProject');
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
In your getProject function you return a view that show the form where the user can enter an ID. (The one you already have):
{!! Form::open(['action' => 'AdminController#postProject', 'method' => 'post']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
In your postProject function you just send a redirect to admin/project/{project_id} URL:
public function postProject(Request $request)
{
return redirect('admin/projects/' . $request->project_id);
}
In your showProject function you just retrieve the record and return a view with the information:
public function showProject($ProjectID)
{
$project=Project::find($ProjectID);
return view('admin.projects.showProject'),
->with('Project', compact('project'));
}
Try changing your controller method to
public function showProject($project_id)
{
$project=Project::find($project_id);
return view('admin.projects.showProject', compact('project'));
}
You don't need to use request on get route.
I stumbled upon a problem while creating a CRUD application in Laravel 5. In the edit section of a product I have 2 inputs that are populated from 'products' table and I have a dropdown list that needs to be populated with data from the categories table. The problem is that I don't know how to aproch the dropdown list. I generated the Form::model for the two inputs but I'm stuck at the dropdown list.
Controller
public function edit($id)
{
$products_edit = products::find($id);
return View::make('products.edit')->with('products_edit', $products_edit);
}
View
{!! Form::model($products_edit, array('route' => array('products.update', $products_edit->id), 'method' => 'PUT')) !!}
<div class="form-group">
{!! Form::label('name', 'Nume') !!}
{!! Form::input('text', 'name', $products_edit->product_name, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('price', 'Cost') !!}
{!! Form::input('number', 'price', $products_edit->product_price, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('category', 'Categorie') !!} <br />
{!! Form::select('category', <!-- insert var here -->, array('class' => 'form-control') !!}
</div>
You can do this at your controller:
public function edit($id)
{
$products_edit = Product::findOrFail($id);
$categories = Category::lists('name', 'id');
return view('product.edit', compact('products_edit', 'categories'));
}
In the view you still make the form model bind:
{!! Form::model($products_edit, array('route' => array('products.update', $products_edit->id), 'method' => 'PUT')) !!}
And to create the category dropdown try this:
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
your can do thi controller:
public function edit($id)
{
$products_edit = Product::find($id);
$categories = Category::pluck('name', 'id');
return view('product.edit', compact('products_edit', 'categories'));
}