Why did not update table columns values in Laravel 5.6? - php

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 ]);

Related

Multiple field for search bar does not work

I have a search bar with 3 types of input and I want to search into my database if a data exist with those criteria.
Here is my html code for my search bar :
<form method="GET" action="{{ route('admin.liste.article.accueil.article') }}" class="mt-4 background_form">
<div class="form-row justify-content-center">
<div class="form-group col-md-4">
<label for="inputCity">Rechercher par ...</label>
<input class="form-control" name="rechercher" type="text" aria-label="Search" value="{{ request()->query('recherche') }}">
</div>
<div class="form-group col-md-2">
<label for="inputState">Type article</label>
<select id="inputState" class="form-control" name="type_article">
<option></option>
#foreach ($type_articles as $type_article)
<option value="{{ $type_article->type_article_id }}">{{ $type_article->libelle }}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-3">
<label for="datePublication">Date de publicaton</label>
<input class="form-control" type="text" placeholder="Exemple : 2021-02-29" name="date" value="{{ request()->query('date') }}">
</div>
<div class=" col-md-2 align-self-center mt-3">
<button type="submit" class="form-control btn-sm btn-custom mb-2">Trouver</button>
</div>
</div>
</form>
Into my controller :
public function accueil_article(Request $request)
{
$rechercher_par_accueil = $request->query('rechercher');
$type_article_rechercher_accueil = $request->query('type_article');
$date_publication_recherche_accueil = $request->query('date');
if($rechercher_par_accueil || $type_article_rechercher_accueil || $date_publication_recherche_accueil){
$article_rechercher = Article::where('titre', 'like', "%{$rechercher_par_accueil}%")
->when(request()->query('type_article_recherche'), function($query) {
$query->where('type_article_id', 'like', "%{$type_article_rechercher_accueil}%");
})
->when(request()->query('date_publication_recherche'), function($query) {
$query->where('created_at', 'like', "%{$date_publication_recherche_accueil}%");
})
->simplePaginate(5);
} else {
$article_rechercher = Article::orderBy('created_at','desc')->paginate(5);
}
return view('admin/article/admin_liste_article', [
'articles' => $article_rechercher,
'type_articles' => Type_article::all(),
'themes' => Theme::all()
]);
}
And at the bottom of my view, I've :
{!! $articles->appends(['rechercher' => request()->query('rechercher'), 'type_article' => request()->query('type_article'), 'date' => request()->query('date')])->links() !!}
Cordially
Try:
$query->whereIn('type_article_id', [$type_article_rechercher_accueil]);

Store data again with the same id Laravel

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

Uploading file to database from input

I am learning Laravel and got stuck with one issue. I have created form with validation and all goes to database. Except my image_url with type="file"
<form method="POST" action="{{route('photos.store')}}" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="">
#component('components.back', ['url' => route('photos.index')])
#endcomponent
<hr>
</div>
<div class="form-all">
<ul class="form-section page-section">
<li class="form-line" data-type="control_image" id="id_1">
<div id="cid_1" class="form-input-wide">
<div style="text-align:center;">
<img alt="" class="form-image" style="border:0px;" src="" height="42px" width="42px" data-component="image">
</div>
</div>
</li>
<li class="form-line" data-type="control_text" id="id_12">
<div id="cid_12" class="form-input-wide">
<div id="text_12" class="form-html" data-component="text">
<p><span style="color:#363636;font-size:xx-large;">Įkelti nuotrauką</span></p>
</div>
</div>
</li>
<li class="form-group{{ $errors->has('title')?' has-error' : ''}}" id="id_7">
<label class="form-label form-label-top" id="label_7" for="title">Pavadinimas</label>
<div id="cid_7" class="form-input-wide">
<input id="title" type="text" name="title" value="{{ old('title') }}" class="form-control" placeholder="Pavadinimas">
#if ($errors->has('title'))
<span class="help-block">
<strong>
{{ $errors->first('title')}}
</strong>
</span>
#endif </div>
</li>
<li class="form-group{{ $errors->has('description')?
' has-error' : ''}}">
<label class="form-label form-label-top" id="label_5" for="description">Aprašymas</label>
<div id="cid_5" class="form-input-wide">
<input id="description" type="text" name="description" value="{{ old('description') }}" class="form-control" placeholder = "Aprašymas">
#if ($errors->has('description'))
<span class="help-block">
<strong>
{{ $errors->first('description')}}
</strong>
</span>
#endif
</div>
</li>
<li class="form-group {{ $errors->has('image_url')?
' has-error' : ''}}">
<label class="form-label form-label-top" id="label_11" for="image_url">Nuotrauka</label>
<div id="cid_11" class="form-input-wide">
<input id="image_url" type="file" name="image_url" value="{{ old('image_url') }}" class="form-control">
#if ($errors->has('image_url'))
<span class="help-block">
<strong>
{{ $errors->first('image_url')}}
</strong>
</span>
#endif
</div>
</li>
<li class="form-group {{ $errors->has('category_id') ? 'has-error': ''}}">
<label class="form-label form-label-top" id="label_11">Kategorija</label>
#if(isset($photo))
<select name="category_id" class="form-control">
<option value="">Pasirinkti kategoriją</option>
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->title}}</option>
#endforeach
</select>
#else
<select name="category_id" class="form-control">
<option value="">Pasirinkti kategoriją</option>
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->title}}</option>
#endforeach
</select>
#endif
#if($errors->has('category_id'))
<span class="help-block">
<strong>{{ $errors->first('category_id')}}</strong>
</span>
#endif
</li>
<li class="form-line" data-type="control_button" id="id_2">
<div id="cid_2" class="form-input-wide">
<div style="text-align:center;" class="form-buttons-wrapper">
<button id="input_2" type="submit" class="form-submit-button" data-component="button">
Patvirtinti
</button>
</div>
</div>
</li>
</ul>
</div>
and my Controller looks like this:
public function create()
{
$categories = Category::all();
return view('photo.create', compact('photo','categories'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|min:3',
'image_url'=> 'required',
'category_id' => 'required',
]);
$photo = new Photo;
$photo->title = $request->input('title');
$photo->image_url = $request->input('image_url');
$photo->description = $request->input('description');
$photo->category_id = $request->input('category_id');
$photo->save();
$request->session()->flash('create', $request->title. "Sekmingai sukurta");
$path = $request->image_url->store('images');
return redirect()->route('photos.index');
}
and seeder:
public function __construct( Photo $photo, Category $category) {
$this->photo = $photo;
$this->category = $category;
}
public function run()
{
$categories_ids = $this->category->pluck('id');
DB::table('photos')->insert(
[
'title' => '',
'image_url' => '',
'description' => '',
'category_id' => $categories_ids->random()
]);
}
I understand that I should put some if into Controller telling "If its not text, but file- accept file". But I am not able to find answer online.
Thank you for your advises in advance.
You will get image name from $fileName = $request->image_url->store('images');
and note $fileName wil return folder name also images/2Q3J1gU3j8NkMgt5HtGurSDtu5BIbeATNZb13Ih3.jpeg
$photo = new Photo;
if ($request->hasFile('image_url')) {
$fileName = $request->image_url->store('images');
$photo->image_url = $fileName;
}
$photo->title = $request->input('title');
$photo->description = $request->input('description');
$photo->category_id = $request->input('category_id');
$photo->save();
$request->session()->flash('create', $request->title. "Sekmingai sukurta");
return redirect()->route('photos.index');

laravel 5.3 - multiple rows insert to database table

I am trying to add multiple subitems to my eloquent model as arrays but am unable to do so.
View:
<div class="row">
<div class="col-md-6">
<form class="" action="{{route('production.update', $rm->id)}}" method="post">
<input name="_method" type="hidden" value="PATCH">
{{csrf_field()}}
<!-- <input type="hidden" name="product_id" id="product_id" class="form-control" value="{{$rm->id}}"> -->
<div class="form-group{{ ($errors->has('batch_id')) ? $errors->first('title') : '' }}">
<lable>Batch ID</lable>
<input type="text" name="batch_id" id="batch_id" class="form-control" value="{{$rm->product_id}}" readonly="">
{!! $errors->first('wastage','<p class="help-block">:message</p>') !!}
</div>
<div class="form-group{{ ($errors->has('item_id')) ? $errors->first('title') : '' }}">
<lable>Item Name</lable>
<input type="hidden" name="item_id" id="item_id" class="form-control" value="{{$rm->item_id}}" readonly="">
<input type="text" class="form-control" value="{{$rm->item->item_name}}" readonly="">
{!! $errors->first('wastage','<p class="help-block">:message</p>') !!}
</div>
<div class="form-group{{ ($errors->has('rquantity')) ? $errors->first('title') : '' }}">
<lable>Quantity (Kg)</lable>
<input type="text" name="rquantity" id="rquantity" class="form-control" value="{{$rm->quantity}}" readonly="">
{!! $errors->first('wastage','<p class="help-block">:message</p>') !!}
</div>
<div class="form-group{{ ($errors->has('')) ? $errors->first('title') : '' }}">
<lable>Packing</lable>
#foreach($subitem as $sub)
<div class="row">
<div class="col-md-4">
<input type="checkbox" name="subitem[{{$sub->id}}]['subitem_id']" value="{{$sub->id}}"> {{$sub->subitem_name}}<br><br>
</div>
<div class="col-md-4">
<input type="text" name="subitem[{{$sub->id}}]['qty']" id="qty" class="form-control" placeholder="Entire Qty">
{!! $errors->first('qty','<p class="help-block">:message</p>') !!}
</div>
</div>
#endforeach
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Add">
</div>
</form>
</div>
</div>
Controller:
public function update(Request $request, $id)
{
$result = Production::findOrFail($id);
foreach($request->subitem as $key=>$row)
{
//print_r($request->subitem);exit;
$items = new Itempacking;
$items->batch_id = $result->product_id;
$items->item_id = $result->item_id;
$items->rquantity = $result->quantity;
$items->product_id = $result->id;
$items->subitem_id = $row['subitem_id'];
$items->qty = $row['qty'];
$items->status = 1;
$items->save();
$items='';
}
}
When I uncomment this line print_r($request->subitem); exit; I can see like this:
Array (
[3] => Array (
['subitem_id'] => 3
['qty'] => 2
)
[4] => Array (
['subitem_id'] => 4
['qty'] => 3
)
)
But when I comment and try to send data to table it is not working. It gives this message
Undefined index: subitem_id
Don't insert '' for key of array in HTML code.You can print your request to details.
Replace from
subitem[{{$sub->id}}]['subitem_id']
subitem[{{$sub->id}}]['qty']
to
subitem[{{$sub->id}}][subitem_id]
subitem[{{$sub->id}}][qty]

how to update values in Laravel 5.2

I am going to update My permission table value.
this is controlller function
public function edit($project,$id)
{
$projectId=Project::findOrFail($project, ['id'])->id;
$permissions = Permission::permissioneditt($id,$projectId)->get();
view('collaborators.permissionedit')->withPermissions($permissions);
return view('collaborators.permissionedit', compact('permissions', 'projectId','collaborator->user()->first()->id'));
}
and this is update function
public function update(Request $request, $projectId, $collaboratorId)
{
$this->validate($request, [
'status' => 'required',
]);
DB::table('permissions')
->where('project_id', $projectId)
->where('collaborator_id', $collaboratorId)
->update(['status' => $request->input('status')]);
return redirect()->back()->with('info','Your Permission has been updated successfully');
}
this is update form
<form class="form-vertical" role="form" method="post" action="{{url('projects/' .$projectId .'/collaborators/' . $collaboratorId.'}}">
<div class="form-group{{ $errors->has('status') ? ' has-error' : '' }}">
<label for="status" class="control-label">Choose Permission</label>
<select name="status" id="status">
<option value="{!! $permission->status !!}">{!! $permission->status !!}</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
{{ getStatus($permission->status) }}
</select>
#if ($errors->has('status'))
<span class="help-block">{{ $errors->first('status') }}</span>
#endif
{{-- {{$permission->project_id}} --}}
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Update</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{!! method_field('PUT') !!}
</form>
and this is routes
Route::put('projects/{projects}/collaborators/{id}',['uses'=>'ProjectCollaboratorsController#update',]);
but when I click update button it is generated following error
FatalErrorException in 607c064f09cf795b94deb14e18a3a34650c521d0.php line 23: syntax error, unexpected 'status' (T_STRING)
how can I fix this problem?

Categories