Laravel Edit form error - php

this is my form:
{!! Form::model($countries, ['route' => ['countries.update', $countries->id], 'method' => "PUT"]) !!}
{{ Form::label('code', 'Country Code:') }}
{{ Form::text('code', null, ['class' => 'form-control']) }}
{{ Form::label('name', 'Country Name:') }}
{{ Form::text('name', null, ['class' => 'form-control']) }}
{{ Form::submit('Save', ['class' => 'mt-20 btn btn-success btn-sm']) }}
{!! Form::close() !!}
and this is my update function:
$countries = Country::find($id);
$this->validate($request, array(
'code' => 'required|min:2|max:4',
'name' => 'required|max:255'
));
$country = Country::where('id',$id)->first();
$country->code = Input::get('code');
$country->name = Input::get('name');
$country->save();
Session::flash('success', 'The Country info was successfully updated.');
return redirect()->route('locations.index', $country->id);
what is the issue in my form that I'm getting Undefined variable: countries error from my blade?

Consolidating this answer from our conversation in the comments.
The error Undefined variable: countries in the blade view (form) arises as you have forgotten to pass the said variable to the view.
In the edit function (as this the function calling the view), add the following
$countries = Country::find($id); // though I'd suggest naming it $country
...
return view('<view_name>', compact('countries'));

Related

Neither it show an error nor save the Data into my database PHP Laravel?

I am trying to insert data into the database neither it shows an error nor saves the data into my database below are the codes. here is the create blade php page .
#extends('layouts.app')
#section('content')
<h1>Create a Camp</h1>
<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'camps')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', Request::old('name'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('district', 'District') }}
{{ Form::text('district', Request::old('district'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('address', 'Address') }}
{{ Form::text('address', Request::old('address'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('block', 'Block') }}
{{ Form::text('block', Request::old('block'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('population', 'population') }}
{{ Form::text('population', Request::old('population'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Create the Camp!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
#endsection
here is the controller the index controller works fine when I manually enter the data it fetches from the database
here is the store controller and also it not validate the forms data , i am new i dont know how to do it
public function store(Request $request)
{
$rules = array(
'name' => 'required',
'district'=> 'required',
'address' => 'required',
'block' => 'required|numeric',
'Population' => 'required|numeric',
);
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
return Redirect::to('camp/create')
->withErrors($validator)
->withRequest(Request::except('password'));
} else {
// store
$camp = new Camp;
$camp->name = Request::get('name');
$camp->district = Request::get('district');
$camp->address = Request::get('address');
$camp->block = Request::get('block');
$camp->population = Request::get('population');
$camp->save();
// redirect
Session::flash('message', 'Successfully created Camp!');
return view('camp\camps',['camps'=>$camps]);
}
}
You forgot to add parenthesis after Camp it should be like this:
$camp = new Camp();
$camp->name = $request->name;
$camp->district = $request->district;
$camp->save();

Laravel edit children in parent view

I try to get children's in their parents view with ability of editing them,
Codes
controller
public function edit($id)
{
$option = Option::findOrFail($id);
return view('admin.options.edit', compact('option'));
}
blade
// getting parent info in edit blade
{{ Form::model($option, array('route' => array('options.update', $option->id), 'method' => 'PUT')) }}
{{ Form::label('title', 'Name') }}
{{ Form::text('title', null, array('class' => 'form-control')) }}
{{ Form::submit('Update', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
// Geting this option subs (children's) in my second tab
#foreach($option->suboptions as $sub)
{{ Form::model($sub, array('route' => array('suboptions.update', $sub->id), 'method' => 'PUT')) }}
{{ Form::label('title', 'Name') }}
{{ Form::text('title', null, array('class' => 'form-control')) }}
{{ Form::label('price', 'Price') }}
{{ Form::text('price', null, array('class' => 'form-control')) }}
{{ Form::submit('Update', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
#endforeach
Issue
As my sub's form is inside the loop I get update button for each row
(children) how can I only have 1 button?
UPDATE
check this video to see what I mean.
UPDATE 2
More explain!
my page is include 2 different forms (one of them edits my option
eg. size that's fine we no have problem with that)
my second form which is my issue will return subs of my option, eg.
option = size, subs = 12 inch, 15 inch etc.) in order to edit this subs currently i have 1 button for each of them (as you see in video i shared), what i want is to have only 1 button for all of my second form.
You can just open and close the form outside of the loop:
// Geting this option subs (children's) in my second tab
{{ Form::open(array('route' => ['suboptions.updateMany'], 'method' => 'PUT')) }}
#foreach($option->suboptions as $sub)
{{ Form::label('title', 'Name') }}
{{ Form::text('title[]', null, ['class' => 'form-control']) }}
{{ Form::label('price', 'Price') }}
{{ Form::text('price[]', null, ['class' => 'form-control']) }}
#endforeach
{{ Form::submit('Update', ['class' => 'btn btn-success']) }}
{{ Form::close() }}
You would then implement the updateMany controller method to loop over the submitted data (validate it) and submit it.
SOLVED
I've made controller below and I can update my data as I wanted:
public function subsupdate(Request $req)
{
for ($i=0; $i<count($req->id); $i++) {
DB::table('suboptions')
->where('id',$req->id[$i])
->update([
'title' => $req->title[$i],
'price' => $req->price[$i],
]);
}
return redirect()->back()->with('success', 'Suboptions updated successfully.');
}

"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')) !!}.

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 Form Model Binding One to One Relationships Not Populated

Terms table:
term_id
name
slug
Term_taxonomy table:
term_taxonomy_id
term_id
description
My Term model:
public function TermTaxonomy(){
return $this->hasOne('TermTaxonomy');
}
My TermTaxonomy model:
public function Term(){
return $this->belongsTo('Term');
}
my Controller
public function edit($id)
{
$categories = Term::with(['TermTaxonomy' => function($q){
$q->select('term_id', 'description');
}])->get(['term_id', 'name', 'slug']);
$category = Term::with(['TermTaxonomy' => function($q){
$q->select('term_id', 'description');
}])->find($id, ['term_id', 'name', 'slug']);
return View::make('qhymchildz.backend.posts.categories', compact('category', 'categories'));
}
My View
#if (isset($category))
{{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}
#else
{{ Form::open(['route' => 'admin_posts_categories_store'])}}
#endif
{{ Form::label('name', 'Name') }}
<span data-tooltip aria-haspopup="true" class="has-tip radius" title="Category name for your posts.">
{{ Form::text('name', '', ['placeholder' => 'Category name here']) }}
#if ($errors->has('name')) <small class="error"> {{ $errors->first('name') }} </small> #endif
</span>
{{ Form::label('slug', 'Slug') }}
<span data-tooltip aria-haspopup="true" class="has-tip radius" title="Slug or URL for your category.">
{{ Form::text('slug', '', ['placeholder' => 'Slug here']) }}
#if ($errors->has('slug')) <small class="error"> {{ $errors->first('slug') }} </small> #endif
</span>
{{ Form::label('description', 'Description') }}
{{ Form::textarea('description', '', ['placeholder' => 'Category description here', 'size' => '50x5']) }}
#if (!isset($category))
{{ Form::submit('Add New Category', ['class' => 'radius button']) }}
#else
{{ Form::submit('Update', ['class' => 'radius button']) }}
#endif
{{ Form::close() }}
and all input text is not populated, already try many way from googling but nothing work, then how to populate data in my input text and text area ? i use it for edit function.
Thanks, i am new in laravel. any help will be appreaciated.
First, the second parameter has to be null so it will actually use the value from your model:
{{ Form::text('name', null, ['placeholder' => 'Category name here']) }}
To use a property from a related model you can use this:
{{ Form::textarea('TermTaxonomy[description]', null, ['placeholder' => 'Category description here', 'size' => '50x5']) }}
Note that there's no need to do an #if(isset($category)) at the beginning. The Form::model method will handle non-existent models on it's own. This is sufficient:
{{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}
In my case was not rendering the model value on the Textarea.
So instead of null i give the value like this:
{{ Form::textarea('TermTaxonomy[description]', $category->TermTaxonomy['description'], ['placeholder' => 'Category description here', 'size' => '50x5']) }}
Hope this also helps someone.
I also had the same problem, but I did it like this:
{!! Form::text('firstname', $user->profile->firstname, ['class' => 'form-control']) !!}
In my case:
User belongsTo('App\Profile'),
Profile hasOne('App\profile')
Just get an idea. Hope it helps!

Categories