How to populate edit form (one to many relationship laravel) - php

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).

Related

"View not defined" error in Laravel

I'm learning Laravel, and I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car, which is identified with the modelesc variable. This form sends the data to a "orders" table. But I keep getting this error "Action App\Http\Controllers\orders not defined." on catalog.blade.php
This is the code that I have made.
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders($modelesc) {
$cars = DB::select('select * from cars where Model = modelesc');
return view('orders', compact('cars'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
{!! Form::hidden(modelesc, $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
{!! Form::open(array('action' => 'index', 'method' => 'POST')) !!}
{!! Form::text(Model, $modelesc) !!}
{!! Form::hidden(users_id, Auth::user()->id) !!}
{!! Form::hidden(Fabrication_date, date(Y-m-d)) !!}
{!! Form::select('colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{!! Form::hidden(Order_status_id, '1' !!}
{!! Form::close() !!}
This is the structure of the table 'orders'. The _id come from other tables, and I want to fill some values of the forms with this id's.
id,
users_id,
Model,
Fabrication_date,
Colour_id,
Order_status_id
Laravel does not know where to find the action orders, because you did not specify a controller (see the thrown exception: it is looking for some class named orders in the namespace App\Http\Controllers\). To do so, you just need to replace
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
with
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
If you want to use a route instead (e.g. you have defined a route named orders which links to a Controller action) you need to replace action with route in the Form::open method:
{!! Form::open(array('route' => 'orders', 'method' => 'GET')) !!}.

Laravel inserting "null" into database from form input.

This is a strange problem i've run into. I have a form that takes in First, Last, Title, Email, and Phone which takes that data and inputs into a ContactPerson table I have set up in a database.
When I fill out the form, and hit submit, only the Title, Email, and Phone number get inserted correctly into the database. The first and last names get inserted as NULL.
Here's some of my code that I have using PHPstorm and Laravel 2.5.3
My Create a New Contact view:
<h1 style="text-align:center;">Add A New Contact Person</h1>
<hr/>
{!! Form::open(['url' => '/create']) !!}
<span style="text-align:center;align-content:right;display:block; margin-right:auto;margin-left:auto;">
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('Title', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('email', 'Email: ') !!}
{!! Form::text('Email', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('phone', 'Phone: ') !!}
{!! Form::text('Phone', null, ['class' => 'form']) !!}
</div>
{!! Form::submit('Submit', ['class' => 'btn btn-primary form']) !!}
</span>
{!! Form::close() !!}
Here is my controller called ResourceController I have created:
class ResourceController extends Controller
{
public function resource()
{
$contacts = ContactPerson::all();
return view('pages.resource', compact('contacts'));
}
public function create()
{
return view('pages.create');
}
public function store(Requests\CreateNewContactRequest $request)
{
ContactPerson::create($request->all());
return redirect('resource');
}
}
Here's the validation I have set up in the CreateNewContactRequest class for the form:
public function rules()
{
return [
'First' => 'required|min:2',
'Last' => 'required|min:1',
'Title' => 'required|min:2',
'Email' => 'required|Email|unique:users',
'Phone' => 'required|numeric|min:9'
];
}
Here's what it looks like when I fill out/submit the form.
After hitting submit it redirects to the database dump:
the view of the database after insertion:
Looking at your dump details, your field names need to match your form names or visa versa, so your form names need to change to say:
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First_Name', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last_Name', null, ['class' => 'form']) !!}
</div>
EG for field name First_Name your input needs to be named the same name="First_Name"
Also making sure your fillable details are also correct
protected $fillable = ['First_Name','Last_Name','Title','Email','Phone'];

Error when updating database after deploying to server. Laravel 5

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().

Lravel 5.1 How to find database records through a from?

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.

Laravel 5 Populate a form from two different tables

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'));
}

Categories