Store data again with the same id Laravel - php

I want to create an order with multiple articles, so first I made a blade where you can create an order, and then you press on forward to get on another blade where you can submit articles for that order created, so on the second blade I've made 3 buttons down, one to submit and leave, the other one is to submit and add another article, and the other one is submit and create a new order.
So my submit and add article button doesn't work properly because i don't get the order_id (foreign of id of the order). Maybe a mistake in my controller function
Here is my code
Create order blade
<div class="form-group">
<label>Bestelnummer</label>
<div class="input-group">
<input type="text" name="ordernumber" class="form-control"
placeholder="Vul bestelnummer in (653...)"
value="{{ old('ordernumber') }}">
</div>
</div>
<div class="form-group">
<label>HB Ordernumber</label>
<input type="text" name="hbnumber" class="form-control"
placeholder="Vul HB Ordernummer in" value="{{ old('hbnumber') }}">
</div>
<div class="form-group">
<label>Klantnaam</label>
<input type="text" name="customername" class="form-control"
placeholder="Vul klantnaam in" value="{{ old('customername') }}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success" name="submit_order" value="submit">Doorgaan
</button>
</div>
And this is the function in the controller of the create
$retourorder = new Returnorder();
$retourorder->firmaname = request('firmaname');
$retourorder->contactperson = request('contactperson');
$retourorder->email = request('email');
$retourorder->ordernumber = request('ordernumber');
$retourorder->hbnumber = request('hbnumber');
$retourorder->customername = request('customername');
$retourorder->save();
$admin = \request()->get('admin');
return view ('retour.create_articles', ['retourOrder' => $retourorder, 'admin' => $admin]);
Create_articles blade
<div class="form-group">
<label for="order_id" >Bestelnummer</label>
<select name="order_id" class="form-control" >
<option
value="{{ $retourOrder->id }}" {{ old('order_id') == $retourOrder->ordernumber ? 'selected' : '' }} > {{ $retourOrder->ordernumber }} </option>
</select>
</div>
<div class="form-group">
<label>Artikelnummer</label>
<input type="text" name="articlenumber" class="form-control"
placeholder="Vul artikelnummer in" value="{{ old('articlenumber') }}">
</div>
<div class="form-group">
<label>Retour aantal</label>
<input type="text" name="return_quantity" class="form-control"
placeholder="Vul retour aantal in"
value="{{old('return_quantity')}}">
</div>
<div class="form-group">
<label>Kwaliteit retour</label>
<select name="return_quality" class="form-control">
<option value="1" {{ old('return_quality') == 1 ? 'selected' : '' }} >Verkoopbaar</option>
<option value="2" {{ old('return_quality') == 2 ? 'selected' : '' }} >Niet verkoopbaar</option>
</select>
</div>
<div class="form-group">
<label>Teruggave reden volgens retourbewijs</label>
<select name="return_reason" class="form-control">
<option value="1" {{ old('return_reason') == 1 ? 'selected' : '' }} >Levertijd</option>
<option value="2" {{ old('return_reason') == 2 ? 'selected' : '' }} >Kwaliteit</option>
<option value="3" {{ old('return_reason') == 3 ? 'selected' : '' }} >Defect</option>
<option value="4" {{ old('return_reason') == 4 ? 'selected' : '' }} >Overig</option>
<option value="5" {{ old('return_reason') == 5 ? 'selected' : '' }} >Aanname geweigerd</option>
<option value="6" {{ old('return_reason') == 6 ? 'selected' : '' }} >Adres ontvanger onbekend
</option>
</select>
</div>
<div class="form-group">
<label for="inputGroupFile01">Upload foto (Maximaal 1 foto)</label>
<input type="file" name="image" class="form-control-file"
id="inputGroupFile01"
aria-describedby="inputGroupFileAddon01">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success" name="submit" value="submit">Verstuur
</button>
<button type="submit" class="btn btn-secondary"
name="submit" value="submit_again">Nieuw artikel
</button>
<button type="submit" class="btn btn-danger"
name="submit" value="submit_new">Verstuur & Nieuwe retourmelding
</button>
Controller function of create_articles
if ($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalExtension();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$request->file('image')->move('storage/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$retourartikel = new Returnarticles();
$retourartikel->order_id = request('order_id');
$retourartikel->articlenumber = request('articlenumber');
$retourartikel->return_quantity = request('return_quantity');
$retourartikel->return_quality = request('return_quality');
$retourartikel->return_reason = request('return_reason');
$retourartikel->images = $fileNameToStore;
$retourartikel->save();
$admin = \request()->get('admin');
if ($request->submit === 'submit') {
return redirect('/retour')->with('message', 'Je retourmelding is succesvol verzonden');
} elseif ($request->submit === 'submit_again') {
return view('retour.create_articles', ['retourOrder' => $retourartikel, 'admin' => $admin])->with('message', 'Je kunt nu een ander artikelnummer toevoegen')->withInput($request->only('order_id'));
} else {
return redirect('/retour/create')->with('message', 'Je retourmelding is succesvol verzonden, je kunt nu een nieuwe retourmelding maken');
}

Related

blade file : $meeting is undefined

Whenever I visit the add-meeting URL, I receive an error message stating that $meeting is undefined. However, when I attempt to edit a meeting, everything works as expected. I am using the same blade file for both creating and updating meetings. Can you explain why this error is occurring?
Here is MeetingController:
public function add_meeting()
{
$customers = Customer::all();
$projects = Project::all();
return view('admin.meeting.add-meeting', get_defined_vars());
}
public function store(Request $request)
{
$this->validate($request, [
'meeting_scedule' => 'required',
'meeting_user_id' => 'required',
'project_id' => 'required',
'agenda' => 'required',
]);
if ($request->id) {
$input['meeting_scedule'] = $request->meeting_scedule;
$input['meeting_user_id'] = $request->meeting_user_id;
$input['project_id'] = $request->project_id;
$input['agenda'] = $request->agenda;
$meeting = Meeting::where('id', $request->id)->update($input);
return back()->with('success', 'Updated Successfully!');
} else {
$new['meeting_scedule'] = $request->meeting_scedule;
$new['meeting_user_id'] = $request->meeting_user_id;
$new['project_id'] = $request->project_id;
$new['agenda'] = $request->agenda;
$meeting = new Meeting();
$meeting->persist($new);
return back()->with('success', 'Meeting Created Successfully!');
}
}
public function edit_meeting($id)
{
$customers = Customer::all();
$projects = Project::all();
$meeting = Meeting::find($id);
return view('admin.meeting.add-meeting', get_defined_vars());
}
Here is add-meeting.blade.php file:
<form action="{{url('admin/update-meeting')}}" method="POST" id="add-rel-form"
enctype="multipart/form-data">
#csrf
#if(isset($meeting))
<input class="hidden" type="hidden" name="id" value="{{$meeting->id ?? ''}}">
#endif
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label> Meeting Schedule</label>
<div class="form-group">
<input type="datetime-local" value="{{$meeting->meeting_scedule ?? ''}}" required
class="form-control" name="meeting_scedule" id="meeting_scedule">
</div>
</div>
<div class="form-group col-md-6 mb-0">
<label> User</label>
<select id="customer-select" class='form-control' required name="meeting_user_id">
<option value="" id="">--Select User--</option>
#foreach($customers as $customer)
<option value="{{$customer->id}}"{{ $customer->id == $meeting->meeting_user_id ? 'selected' : '' }}>{{$customer->first_name." ".$customer->last_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label> Project</label>
<select id="project-select" class='form-control' required name="project_id" disabled>
<option value="" id="">--Select Project--</option>
#foreach($projects as $project)
<option value="{{$project->id}}"{{$project->id == $meeting->project_id ? 'selected' : ''}}>{{$project->project_type}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-6 mb-0">
<label> Agenda</label>
<div class="form-group">
<textarea type="text" value="" required
class="form-control" name="agenda" id="agenda">{{$meeting->agenda ?? ''}}</textarea>
</div>
</div>
</div>
Since there is no $meeting variable when creating a new meeting, the error occurs.
You can use null coalescing operator (??) or ternary operator (?:) to check if $meeting is defined before attempting to access its properties :
#if(isset($meeting))
<input class="hidden" type="hidden" name="id" value="{{$meeting->id}}">
#endif
...
<select id="customer-select" class='form-control' required name="meeting_user_id">
<option value="" id="">--Select User--</option>
#foreach($customers as $customer)
<option value="{{$customer->id}}" {{ isset($meeting) && $customer->id == $meeting->meeting_user_id ? 'selected' : '' }}>{{$customer->first_name." ".$customer->last_name}}</option>
#endforeach
</select>
...
<select id="project-select" class='form-control' required name="project_id" disabled>
<option value="" id="">--Select Project--</option>
#foreach($projects as $project)
<option value="{{$project->id}}"{{ isset($meeting) && $project->id == $meeting->project_id ? 'selected' : ''}}>{{$project->project_type}}</option>
#endforeach
</select>
...
<textarea type="text" value="" required class="form-control" name="agenda" id="agenda">{{ isset($meeting) ? $meeting->agenda : '' }}</textarea>

How to handle validation of multiple hidden fields

I have created a form where in many cases some fields are not displayed until user select a certain option that will make the particular field displayed, this is controlled by HTML onchange Event Attribute with functions in script. To this point everything works fine.
Problem:
In the Request class I am implementing some rules for validation , for all displayed fields and for particular none displayed fields if only their options are selected then they should be validated. In my case I have 8 of this none displayed fields are a_text, b_text, c_text, d_text, e_text, f_text,g_text, h_text. If this fields were 2 it would be easy to write an If statement to choose which should be validated and which should not be validated like below:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
class StoreSectionERequest extends FormRequest
public function rules(Request $request)
{
$a = $request->a;
$b = $request->b;
if($a==5 && $b==3){
return [
//for none displayed fields only validate the below
'a_text' =>'required',
'b_text' =>'required',
//..and other displayed fields will be valuated.
];
}elseif(!$a==5 && $b==3){
return [
//for none displayed fields only validate the below
'b_text' =>'required',
//..and other displayed fields will be valuated.
];
}elseif($a==5 && !$b==3){
return [
//for none displayed fields only validate the below
'a_text' =>'required',
//..and other displayed fields will be valuated.
];
}
// in case all none displayed field are not selected only check other fields below
return [
// validaying all none displyed field here]; }}
In my case I have 8 fields which are none displayed, of which I have to handle all cases of fields if it's selected and not, this will lead to 100's or a lot of if...elseif..statements. What is the wright approach to handle this kind of task.
Form
<form method="POST" action="{{ route('......') }}">#csrf
<div class="row">
<div class="col-md-5" id="lackingSkills">
<label for="">Which skills and competencies graduates are lacking? </label>
<select name="a" multiple="multiple" id="skills" class="form-control select2 #error('a')
is-invalid
#enderror" onchange="myFunction()">
<option value="1" #if (old('a')=="1" ) {{ 'selected' }} #endif>Information and communication skills</option>
<option value="2" #if (old('a')=="2" ) {{ 'selected' }} #endif>Interpersonal skill</option>
<option value="3" #if (old('a')=="3" ) {{ 'selected' }} #endif>System management skills</option>
<option value="4" #if (old('a')=="4" ) {{ 'selected' }} #endif>Innovation skills</option>
<option value="5" #if (old('a')=="5" ) {{ 'selected' }} #endif>Others. Specify</option>
</select>
<small class="text-primary">If necessary, choose more than one option.</small>
#error('a') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-6" id="skillReason" {!! $errors->has('a_text') || old('a') == 5 ? '':' style="display: none"' !!} >
<div class="form-floating mb-3">
<textarea rows="2" class="form-control #error('a_text')#enderror" name="e2_10" id="skillReason" placeholder="Type here.."></textarea>
</div>
</div>
<div class="col-md-6">
<select name="b" id="b" class="form-control" onchange="myFunction2()">
<option value="" selected disabled>Select</option>
<option value="1" #if (old('b')=="1" ) {{ 'selected' }} #endif>Most of them match the requirements</option>
<option value="2" #if (old('b')=="2" ) {{ 'selected' }} #endif>Only some match the requirements</option>
<option value="3" #if (old('b')=="3" ) {{ 'selected' }} #endif>Other, specify</option>
</select>
#error('b') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-6 mt-2" {!! $errors->has('e3_5') || old('b') == 3 ? '':' style="display: none"' !!} >
<div class="form-floating mb-3">
<textarea rows="2" class="form-control" id="b_text" name="b_text" placeholder="Type here.."></textarea>
<label for="floatingInput" id="specify">Specify here..</label>
</div>
</div>
<div class="col-md-6">
<select name="c" id="graduatesPreference" class="form-control" onchange="myFunction3()">
<option value="" selected disabled>Select</option>
<option value="1" #if (old('c')=="1" ) {{ 'selected' }} #endif>Yes</option>
<option value="2" #if (old('c')=="2" ) {{ 'selected' }} #endif>No</option>
</select>
#error('c') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-6" id="preferenceReason" {!! $errors->has('c_text') || old('c') == 1 ? '':' style="display: none"' !!}>
<label for="">Provide the reason, why you have a preference for a specific university?</label>
<textarea name="c_text" id="" rows="3" class="form-control" placeholder="Provide the reason here ..."></textarea>
</div>
<div class="col-md-6">
<select name="d" id="qualification" class="form-control #error('d')is-invalid #enderror" onchange="myFunction4()">
<option value="" selected disabled>Select</option>
<option value="1" #if (old('d')=="1" ) {{ 'selected' }} #endif>Bachelor’s degree</option>
<option value="2" #if (old('d')=="2" ) {{ 'selected' }} #endif>Post-graduate</option>
<option value="3" #if (old('d')=="3" ) {{ 'selected' }} #endif>Other, specify</option>
</select>
#error('d') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-12" id="d_text" {!! $errors->has('d_text') || old('d') == 3 ? '':' style="display: none"' !!}>
<label for="">Explain why?</label>
<textarea name="d_text" id="d_text" rows="3" class="form-control" placeholder="Provide the reason here ..."></textarea>
</div>
////.............others fields like e,f,g,h, all have the same functionality like the above ones.
<div class="action-button d-flex justify-content-center bg-white pt-2 pb-2">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
<script>
/// various function for onchange
myFunction()
myFunction2()
myFunction3()
myFunction4()
</script>
My question: what is the wright approach to handle the above scenario without writing 100's of if..elseif statements. Thanks in advance.
In form validation u have rules like required with or without, u can see more here
Little bit lower on the docs u can see Conditionally Adding Rules
So basically u will be doing same validation but without too much mess in your code.

Losing page number on form post laravel

I have two dropdowns one for asc desc and one with the field name. When I change page I am able to keep the order and order_by parameter and the page number. When I change one of the dropdown a postback happens In that case I lose the page number. How can I fix this problem.
Here is the url I want
home?orderby=created_at&order=desc&page=2
Here is the url I have in the case of a dropdown change
home?orderby=created_at&order=desc
Here is my front end code
<form action="{{ route('home') }}" method="GET">
<div class="row">
<div class="col-2">
<label for="nom">Trier par :</label>
</div>
<div class="col-tier form-group">
<select class="form-control" name="orderby" id="orderby" onchange="this.form.submit()">
<option value="nom" {{ Request()->orderby == 'nom' ? ' selected' : '' }}>Nom</option>
<option value="created_at" {{ Request()->orderby == 'created_at' ? ' selected' : '' }}>Date de création</option>
<option value="updated_at" {{ Request()->orderby == 'updated_at' ? ' selected' : '' }}>Date de modification</option>
</select>
</div>
<div class="col-tier form-group ">
<select class="form-control" name="order" id="order" onchange="this.form.submit()">
<option value="desc" {{ Request()->order == 'desc' ? ' selected' : '' }}>Desc</option>
<option value="asc" {{ Request()->order == 'asc' ? ' selected' : '' }}>Asc</option>
</select>
</div>
</div>
</form>
<div id="pagination" class="d-flex justify-content-center">
{{ $grilles->appends(request()->except('page'))->links() }}
</div>
Here is the code of the controlleur
public function index(Request $request)
{
$user = Auth::user();
$sort = "ASC";
if($request->get('order')==="desc")
{
$sort="DESC";
}
if ($request->get('orderby')==="created_at") {
$grilles = Grille::orderBy('created_at', $sort)->paginate(15)->appends(request()->query());
} else if ($request->get('orderby')==="updated_at") {
$grilles =Grille::orderBy('updated_at', $sort)->paginate(15)->appends(request()->query());
}
else{
$grilles =Grille::orderBy('nom', $sort)->paginate(15)->appends(request()->query());
}
return view('home', compact('grilles'));
}
<div id="pagination" class="d-flex justify-content-center">
{{ $grilles->appends(request()->except('page'))->links() }}
</div>

Why did not update table columns values in Laravel 5.6?

in laravel 5.6 app I have table name as vehicles, then I need update some of table values in VehicleController update function as this,
public function update(Request $request, $id)
{
$vehicle = Vehicle::find($id);
$vehicle->provincename = $request->input('provincename');
$vehicle->districtname = $request->input('districtname');
$vehicle->townname = $request->input('townname');
$vehicle->brandname = $request->input('brandname');
$vehicle->modelname = $request->input('modelname');
$vehicle->modelyear = $request->input('year');
$vehicle->condition = $request->input('condition');
$vehicle->milage = $request->input('milage');
$vehicle->detail = $request->input('data');
$vehicle->price = $request->input('price');
$vehicle->telephone = $request->input('telephone');
$vehicle->categoryname = $request->input('categoryname');
$vehicle->transmission = $request->input('transmission');
$vehicle->fueltype = $request->input('fueltype');
$vehicle->enginecapacity = $request->input('enginecapacity');
$vehicle->user_id = Auth::user()->id;
$vehicle->save();
and edit form action is,
<form method="post" action="{{ route('vehicles.edit', [$vehicles->id]) }}" enctype="multipart/form-data">
and update route is,
Route::post('myads/{id}', [
'uses' => '\App\Http\Controllers\VehicleController#update',
])->name('vehicles.edit');
and controller edit function,
public function edit($id)
{
$vehicles = Vehicle::findOrFail($id);
}
and edit route is,
Route::get('myads/{id}/edit', [
'uses' => '\App\Http\Controllers\VehicleController#edit',
'as'=> 'vehicles.edit'
]);
but when I click update button it did not update values. no any error occurred here only redirect back to edit form. how can fix this problem?
vehicle model
class Vehicle extends Model
{
use Searchable;
protected $guarded = [];
public function searchableAs()
{
return 'categoryname';
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function uploads()
{
return $this->hasMany(Upload::class);
}
public function cars()
{
return $this->hasMany(Car::class);
}
public function vans()
{
return $this->hasMany(Car::class);
}
public function scopePersonal($query)
{
return $query->where('user_id', Auth::user()->id);
}
}
edit form is,
<form method="post" action="{{ route('vehicles.edit', [$vehicles->id]) }}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group{{ $errors->has('provincename') ? ' has-error' : '' }}">
<label for="exampleFormControlSelect1">Province</label>
<select name="provincename" id="provincename" class="form-control input dynamic" data-dependent="districtname" >
<option value="{{$vehicles->provincename}}">{!! $vehicles->provincename !!}</option>
#foreach($town_list as $town)
<option value="{{$town->provincename}}">{{$town->provincename}}</option>
#endforeach
</select>
#if ($errors->has('provincename'))
<span class="help-block">{{ $errors->first('provincename') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('districtname') ? ' has-error' : '' }}">
<label for="exampleFormControlSelect1">District</label>
<select name="districtname" id="districtname" class="form-control input dynamic" data-dependent="townname" >
<option value="{{$vehicles->districtname}}">{!! $vehicles->districtname !!}</option>
</select>
#if ($errors->has('districtname'))
<span class="help-block">{{ $errors->first('districtname') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('townname') ? ' has-error' : '' }}">
<label for="exampleFormControlSelect1">Town</label>
<select name="townname" id="townname" class="form-control input">
<option value="{{$vehicles->townname}}">{!! $vehicles->townname !!}</option>
</select>
#if ($errors->has('townname'))
<span class="help-block">{{ $errors->first('townname') }}</span>
#endif
</div>
<!--hidden select box-->
<div class="form-group" style="display: none;">
<label for="exampleFormControlSelect1">Vehicle Category</label>
<select name="categoryname" id="categoryname" class="form-control input dynamic" data-dependent="brandname" >
#foreach($model_list as $model)
<option value="{{$vehicles->categoryname}}">{{$vehicles->categoryname}}</option>
#endforeach
</select>
</div>
<div class="form-group{{ $errors->has('brandname') ? ' has-error' : '' }}">
<label for="exampleFormControlSelect1">Brand</label>
<select name="brandname" id="brandname" class="form-control input dynamic" data-dependent="modelname" >
<option value="{{$vehicles->brandname}}">{!! $vehicles->brandname !!}</option>
</select>
#if ($errors->has('brandname'))
<span class="help-block">{{ $errors->first('brandname') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('modelname') ? ' has-error' : '' }}">
<label for="exampleFormControlSelect1">Model</label>
<select name="modelname" id="modelname" class="form-control input">
<option value="{{$vehicles->modelname}}">{!! $vehicles->modelname !!}</option>
</select>
#if ($errors->has('modelname'))
<span class="help-block">{{ $errors->first('modelname') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('year') ? ' has-error' : '' }}">
<label for="formGroupExampleInput">Model Year</label>
<input type="text" class="form-control" id="year" placeholder="Year" name="year" value="{!! $vehicles->modelyear ?: '' !!}">
#if ($errors->has('year'))
<span class="help-block">{{ $errors->first('year') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('condition') ? ' has-error' : '' }}">
<label for="formGroupExampleInput">Condition</label>
<label class="radio-inline"><input type="radio" name="condition" value="used" #if($vehicles->condition == 'used') checked #endif>Used</label>
<label class="radio-inline"><input type="radio" name="condition" value="recondition" #if($vehicles->condition == 'recondition') checked #endif>Recondition</label>
<label class="radio-inline"><input type="radio" name="condition" value="new" #if($vehicles->condition == 'new') checked #endif> New</label>
#if ($errors->has('condition'))
<span class="help-block">{{ $errors->first('condition') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('milage') ? ' has-error' : '' }}">
<label for="formGroupExampleInput">Milage</label>
<input type="text" class="form-control" id="milage" placeholder="Milage" name="milage" value="{!! $vehicles->milage ?: '' !!}">
#if ($errors->has('milage'))
<span class="help-block">{{ $errors->first('milage') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('transmission') ? ' has-error' : '' }}">
<label for="exampleFormControlSelect1">Transmission</label>
<select class="form-control" id="transmission" name="transmission">
<option value="{!! $vehicles->transmission !!}">{!! $vehicles->transmission !!}</option>
<option value="Manual">Manual</option>
<option value="Auto">Auto</option>
<option value="Hybrid">Hybrid</option>
<option value="Electric">Electric</option>
<option value="Codak">codak</option>
</select>
#if ($errors->has('transmission'))
<span class="help-block">{{ $errors->first('transmission') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('fueltype') ? ' has-error' : '' }}">
<label for="exampleFormControlSelect1">Fuel Type</label>
<select class="form-control" id="fueltype" name="fueltype">
<option value="{!! $vehicles->fueltype !!}">{!! $vehicles->fueltype !!}</option>
<option value="Petrol">Petrol</option>
<option value="Diesel">Diesel</option>
<option value="Hybrid">Hybrid</option>
<option value="Electric">Electric</option>
</select>
#if ($errors->has('fueltype'))
<span class="help-block">{{ $errors->first('fueltype') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('enginecapacity') ? ' has-error' : '' }}">
<label for="formGroupExampleInput">Engine capacity</label>
<input type="text" class="form-control" id="enginecapacity" placeholder="Engine capacity" name="enginecapacity" value="{!! $vehicles->enginecapacity ?: '' !!}" >
#if ($errors->has('enginecapacity'))
<span class="help-block">{{ $errors->first('enginecapacity') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('data') ? ' has-error' : '' }}">
<label for="comment">More Details</label>
<textarea class="form-control" rows="5" id="data" name="data" rows="10" cols="10">{!! trim($vehicles->detail) !!}</textarea>
#if ($errors->has('data'))
<span class="help-block">{{ $errors->first('data') }}</span>
#endif
</div >
<div class="form-group{{ $errors->has('price') ? ' has-error' : '' }}">
<label for="formGroupExampleInput">Price</label>
<input type="text" class="form-control" id="price" placeholder="Price" name="price" value="{!! $vehicles->price ?: '' !!}">
#if ($errors->has('price'))
<span class="help-block">{{ $errors->first('price') }}</span>
#endif
</div>
<div class="form-group{{ $errors->has('telephone') ? ' has-error' : '' }}">
<label for="formGroupExampleInput">Telephone</label>
<input type="text" class="form-control" id="telephone" placeholder="Telephone" name="telephone" value="{!! $vehicles->telephone ?: '' !!}" >
#if ($errors->has('telephone'))
<span class="help-block">{{ $errors->first('telephone') }}</span>
#endif
</div>
<!-- <button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>
</div> -->
#if( $vehicles->uploads->count() > 0 )
#php
$upload = $vehicles->uploads->sortByDesc('id')->first();
#endphp
<!-- <img id="preview" src="/images/{{ $upload->resized_name }}"> -->
<!--edit/delete buttons-->
#foreach( $vehicles-> uploads as $upload)
<img id="preview"
src="{{asset((isset($upload) && $upload->resized_name!='')?'images/'.$upload->resized_name:'images/noimage.png')}}"
height="200px" width="200px"/>
<input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
<br/>
<!-- Add Image | -->
<!-- <a style="color: red" href="javascript:removeImage()">Delete</a>
<input type="hidden" style="display: none" value="0" name="remove" id="remove"> -->
Edit Image|
<a class="button is-outlined" href="/myads/{{$upload->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a></td>
<hr>
#endforeach
#endif
<button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>
</div>
There will be mutiple reasons
1. You are not passing the csrf token with form request.
2. There will be one or more input value missing and you have not show the error message in the validation.
3. Vehicle id not exist.
etc.
try this way
and remove or comment this lines below
//$vehicle->save();
$vehicle = array('provincename'=>$request->input('provincename'),
'districtname' => $request->input('districtname'),
'townname' => $request->input('townname'),
'brandname' => $request->input('brandname'),
'modelname' => $request->input('modelname'),
'modelyear' => $request->input('year'),
'condition' => $request->input('condition'),
'milage' => $request->input('milage'),
'detail' => $request->input('data'),
'price' => $request->input('price'),
'telephone' => $request->input('telephone'),
'categoryname' => $request->input('categoryname'),
'transmission' => $request->input('transmission'),
'fueltype' => $request->input('fueltype'),
'enginecapacity' => $request->input('enginecapacity'),
'user_id' => Auth::user()->id);
Vehicle::where('id', $id)->update($vehicle);
You can use array to update your record and also make sure to pass csrf token when submitting a data. Add user_id column to the array.
DB::table('vehicles')
->where('id', $vehicle)
->update(['provincename ' => $request->input('provincename'),
'districtname'=>$request->input('districtname'),
'townname' =>input('townname'), 'user_id'=>Auth::user()->id ]);

(1/1) ErrorException Undefined index: id in ClientController.php (line 81)

I am not sure why I am getting this error. Here is the method in the ClientController.
protected function updateOneStudent($parameters)
{
$studentId = $parameters['id'];
return $this- >performPutRequest("https://lumenapi.juandmegon.com/students/{$studentId}", $parameters);
}
Basically I am trying to update a selected student. Below is the update form.
#extends('layouts.master')
#section('content')
<form action="{{url('/student/update')}}" method="POST" role="form">
{{ csrf_field() }}
{{method_field('PUT')}}
<legend>Create a Student</legend>
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" name="name" value="{{$student->name }}"required="required">
</div>
<div class="form-group">
<label for="">Address</label>
<input type="text" class="form-control" name="address" value="{{$student->address }}" required="required">
</div>
<div class="form-group">
<label for="">Phone</label>
<input type="text" class="form-control" name="phone" value="{{$student->phone }}" required="required">
</div>
<div class="form-group">
<label for="">Career</label>
<select name="career" class="form-control" required="required">
<option>Select a Career</option>
<option value="math"{{$student->career == 'math' ? 'selected' : ''}}>Math</option>
<option value="physics"{{$student->career == 'physics' ? 'selected' : ''}}>Physics</option>
<option value="engineering"{{$student->career == '' ? 'engineering' : ''}}>Engineering</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Update Student</button>
</form>
#endsection
The request I was sending was wrong. The error was in the StudentController.
I had
public function getUpdateStudent()
{
$students = $this->obtainAllStudents;
return view('students.select-student', ['students'=> $students]);
}
It it should have been
public function getUpdateStudent()
{
$students = $this->obtainAllStudents();
return view('students.select-student', ['students'=> $students]);
}
I missed the brackets to call the getUpdateStudent. Sorry guys I did not show this code earlier.

Categories