when am adding, it records saves to database, but when return to view is says ($store_id is undefined)
Blade:
<form action="{{ route("machine-slots.create", $machineSlotId) }}" method="POST">
#csrf
<input type="hidden" name="store_id" value="{{ $store_id }}">
<div class="modal-body ct-modal-form">
<div class="form-group">
<label for="slot_id" class=" form-control-label">Slot ID</label>
<input type="number" id="slot" name="slot_id"
placeholder="Enter Slot ID" class="form-control">
</div>
<div class="form-group ct-form-dropdown">
<label for="assign-item" class="form-control-label">Assign Item</label>
<select class="form-control" name="product_id">
<option value="" disabled selected>Select Items</option>
#forelse ($products as $i => $product)
<option value={{$product->id}}>{{$product->name}}</option>
#empty
#endforelse
</select>
</div>
<div class="form-group">
<label for="quantity" class=" form-control-label">Quantity</label>
<input type="number" id="quantity" name="current_count"
placeholder="Enter Quantity" class="form-control"
onkeyup="this.value = this.value.toUpperCase();">
</div>
<div class="form-group">
<label for="max-quantity" class=" form-control-label">Max Quantity</label>
<input type="number" id="max-quantity" name="max_count"
placeholder="Enter Max Quantity" class="form-control"
onkeyup="this.value = this.value.toUpperCase();">
</div>
</div>
<div class="modal-footer">
<input type="hidden" value="{{ $machineSlotId }}" name="machine_address_id">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
Controller:
public function store(Request $request, $machineSlotId)
{
//get all machineslot data
$machineSlots = $this->getMachineSlotData1($machineSlotId);
$products = $this->getProductsData();
$store_id = Machine::where([Machine::COLUMN_MACHINE_ADDRESS_ID => $machineSlotId])->first(['store_id'])->store_id;
if($request->input('current_count') > $request->input('max_count'))
{
//display text
Session::flash('warning', "Quantity cannot be higher than max Quantity. Please Try again");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
else
{
//create new record
MachineSlot::create($request->all());
//display text
Session::flash('success', "Table has been updated.");
//display return
//return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
return redirect()->route('machine-slots.index')->with(compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
}
Web:
`
Route::post('/machine-slots/{id}', [App\Http\Controllers\UI\MachineSlotController::class, 'store'])->name('machine-slots.create');
Error
Heres the Update:
Blade
no Changes
Controller: Add on Index
public function index()
{
//check user Data
if(Auth::user() != null)
{
//get user ID
$userID = User::findOrFail(auth()->user()->id)->first(['id'])->id;
//check store is existing, based of passed ID
$isStoreExisting = Store::where('user_id', $userID)->first(['id'])->id;
//check machine is Existing, based on passed ID
$isMachineExisting = Machine::where('store_id', $isStoreExisting)->first(['machine_address_id'])->machine_address_id;
//get machine data
$machineSlot = MachineSlot::where('machine_address_id', $isMachineExisting)->first();
$machineSlots = MachineSlot::where('machine_address_id', $isMachineExisting)->paginate(5);
//fix paginate error
return view('machine-slots',compact('machineSlot', 'machineSlots'));
}
}
public function store(Request $request, $machineSlotId)
{
//get all machineslot data
$machineSlots = $this->getMachineSlotData1($machineSlotId);
$products = $this->getProductsData();
$store_id = Machine::where([Machine::COLUMN_MACHINE_ADDRESS_ID => $machineSlotId])->first(['store_id'])->store_id;
if($request->input('current_count') > $request->input('max_count'))
{
//display text
Session::flash('warning', "Quantity cannot be higher than max Quantity. Please Try again");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
else
{
//create new record
MachineSlot::create($request->all());
//display text
Session::flash('success', "Table has been updated.");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
}
Error: Data duplicates every time i refresh the page
Related
i'm doing a simple project but having some difficulties with updating my database with values that I get from a form. The project is having a list of movies and when you click on one it would take you to a page with more details. Theres a button which you can update the details of that movie. I'm trying save whatever details are made to the relevant index of the table using the id. I can't seem to get it to work though. I did something similar when creating a new entry but having some difficulty updating the values of a specific movie/page. Thanks for any help!
Route:
Route::get('catalog', 'App\Http\Controllers\CatalogController#getIndex');
Route::get('catalog/show/{id}', 'App\Http\Controllers\CatalogController#getShow');
Route::get('catalog/create', 'App\Http\Controllers\CatalogController#getCreate');
Route::get('catalog/edit/{id}', 'App\Http\Controllers\CatalogController#getEdit');
Route::post('catalog/create', 'App\Http\Controllers\CatalogController#postCreate');
Route::put('catalog/edit/{id}','App\Http\Controllers\CatalogController#putEdit');
Controller:
public function getEdit($id)
{
return view('catalog.edit', ['arrayPeliculas' => Movie::all()]);
}
public function getCreate()
{
return view('catalog.create');
}
public function postCreate(Request $request)
{
$Movie = new Movie;
$Movie->title = $request->title;
$Movie->year = $request->year;
$Movie->director = $request->director;
$Movie->poster = $request->poster;
$Movie->synopsis = $request->synopsis;
$Movie->save();
return redirect()->action('App\Http\Controllers\CatalogController#getIndex');
}
public function putEdit(Request $request, $id)
{
$Movie = new Movie;
$Movie[$id]->title = $request->title;
$Movie[$id]->year = $request->year;
$Movie[$id]->director = $request->director;
$Movie[$id]->poster = $request->poster;
$Movie[$id]->synopsis = $request->synopsis;
$Movie->save();
return redirect()->action('App\Http\Controllers\CatalogController#getIndex');
}
Edit page:
<form method="PUT">
{{ method_field('PUT') }}
{{-- TODO: Protección contra CSRF --}}
{{ csrf_field() }}
<div class="form-group">
<label for="modificar">Modificar Pelicula</label>
<input type="text" name="title" id="title" class="form-control" value="{{ $arrayPeliculas[$id]->title }}">
</div>
<div class="form-group">
{{-- TODO: Completa el input para el año --}}
<label for="Año">Año</label>
<input type="text" name="year" id="year" class="form-control" value="{{ $arrayPeliculas[$id]->year }}">
</div>
<div class="form-group">
{{-- TODO: Completa el input para el director --}}
<label for="Director">Director</label>
<input type="text" name="director" id="directo" class="form-control" value="{{ $arrayPeliculas[$id]->director }}">
</div>
<div class="form-group">
{{-- TODO: Completa el input para el poster --}}
<label for="Poster">Poster</label>
<input type="text" name="poster" id="poster" class="form-control" value="{{ $arrayPeliculas[$id]->poster }}">
</div>
<div class="form-group">
<label for="synopsis">Resumen</label>
<textarea name="synopsis" id="synopsis" class="form-control" rows="3" >{{ $arrayPeliculas[$id]->synopsis }}"</textarea>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-primary" style="padding:8px 100px;margin-top:25px;">
Añadir película
</button>
</div>
</form>
I tried a few other things like->update but I can't seem to get it to work properly.
To update an existing model, first find() it.
public function putEdit(Request $request, $id)
{
$Movie = Movie::find($id);
$Movie->title = $request->title;
$Movie->year = $request->year;
$Movie->director = $request->director;
$Movie->poster = $request->poster;
$Movie->synopsis = $request->synopsis;
$Movie->save();
return redirect()->action('App\Http\Controllers\CatalogController#getIndex');
}
Using the update function should also work then:
public function putEdit(Request $request, $id)
{
$Movie = Movie::find($id);
$Movie->update([
'title' => $request->title,
'year' => $request->year,
'director' => $request->director,
'poster' => $request->poster,
'synopsis' => $request->synopsis,
]);
return redirect()->action('App\Http\Controllers\CatalogController#getIndex');
}
You could really simplify it with some Laravel magic like Route Model Binding...
Route::put('catalog/edit/{movie}','App\Http\Controllers\CatalogController#putEdit');
...
public function putEdit(Request $request, Movie $movie)
{
$movie->update($request->all());
return redirect()->action('App\Http\Controllers\CatalogController#getIndex');
}
I currently have the add attachment button for each product on the product list page. After clicking the button, will proceed to add attachment form. How do I get the current product ID from the product table in order to store the attachment data into the attachment table?
Route
Route::post('/store/{product}', 'AttachmentController#store')->name('attachment.store');
Product Model
public function attachment()
{
return $this->hasMany(Attachment::class, 'product_id', 'id');
}
Attachment Model
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
Controller
public function create()
{
return view('document.create', ['prod' => Product::select('id', 'name')->get()]);
}
public function store(Request $request, Product $product) {
$data['product_id'] = $product->id;
$data = $request->validate([
'file' => 'required',
'file.*' => 'mimes:csv,xlsx,pdf,docx',
]);
$attachmentData = [];
if($request->hasFile('file'))
{
foreach($request->file('file') as $file) {
$path = public_path('storage/attachments/'.$request->product_id);
$fileName = time().'-'.$file->getClientOriginalName();
$file->move($path, $fileName);
$attachmentData[] = $fileName;
}
$data['file'] = json_encode($attachmentData);
}
$attachment = Attachment::create($data);
return redirect()->route('product.index')->with('success','Attachment added successfully');
}
Blade View
<form method="POST" action="{{route('attachment.store')}}" enctype="multipart/form-data">
#csrf
<h3><b>Add Attachment</b></h3>
<input type="submit" class="btn btn-primary mr-2" value="Save">
<div class="row">
<h4 class="card-title">General</h4>
<input type="text" name="product_id" value="{{ $product->id ?? '' }}" hidden>
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" required>
</div>
<div class="form-group">
<label>Attachment </label>
<div class="input-group-append">
<label for="attachment" class="btn btn-info">Upload</label>
<input id="attachment" type="file" name="file[]" multiple required>
</div>
</div>
</div>
</form>
You have to use form action like below
<form method="POST" action="{{route('attachment.store',['product'=>$product->id])}}" enctype="multipart/form-data">
First, I try to check if the two fields 'id' and 'flowid' in form are in the same row or not. If they are in the same row, bring all the data in the row into the view. If not the same, then return and display a message that your inputs don't match. I'm having issues with this. Please advise.
Migration
Schema::create('letters', function (Blueprint $table) {
$table->id()->unique();
$table->string('flowid');
$table->string('id_number');
$table->string('letter_type');
$table->string('name_en');
$table->string('name_ar');
$table->string('nationality_en');
$table->string('nationality_ar');
$table->string('jobt_en');
$table->string('jobt_ar');
$table->string('to_en');
$table->string('to_ar');
$table->date('date_issue');
$table->date('hdate');
$table->date('salary_next');
$table->string('email');
$table->string('iban')->nullable();
$table->string('endser')->nullable();
$table->string('endser1')->nullable();
$table->string('salary_b_l')->nullable();
$table->string('housing_l')->nullable();
$table->string('trans_l')->nullable();
$table->string('mob_all_l')->nullable();
$table->string('other_l')->nullable();
$table->string('total_salary_l')->nullable();
$table->timestamps();
});
Model
class Letter extends Model
{
use HasFactory;
protected $guarded=[];
}
Controller
public function verify(Request $request)
{
$id = $request->id;
$flowid = $request->flowid;
if ($id != "") {
$letter = Letter::where('flowid', 'LIKE', '%' . $id . '%')->get();
if (!empty($flowid)) {
$letter->where('flowid', '=', $flowid);
}
$letter = Letter::findOrFail($letter());
return view('layouts.verify', compact('letter'));
}
}
View
<form action="{{ route('letter.verfiy') }}" method="GET">
{{-- #csrf --}}
<div class="form-group">
<label class="control-label col-sm-2" for="id">
Certificate Code:
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="id"
placeholder="Type certificate Code">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="flowid">
Flow Employee ID:
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="flowid"
placeholder="Type Flow ID">
</div>
</div>
<button type="submit" class="btn btn-primary">check</button>
</form>
Route
Route::any('/verfiy', [LetterController::class, 'verify',])->name('letter.verfiy');
Try that
$id = $request->id;
$flowid = $request->flowid;
if (!empty($id) {
$letter = Letter::find($id);
if (!empty($flowid)) {
if($letter->flowid === $flowid){
return view('layouts.verify', compact('letter'));
}
return 'Error'; // return error or redirect
}
}
check if the two fields 'id' and 'flowid' in form are in the same row or not
I believe a basic where clause should do the job for you here because you are checking id and flowid in one row.
$letter = Letter::where('id', $id)->where('flowid', $flowId)->first();
if (! $letter) {
return "Letter not found";
}
return $letter;
When i try to insert data into my table this error occurs
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\xampp\htdocs\Portal\vendor\laravel\framew...
view
<form method="post" action="{{ route('notice.store') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="Select Group to Post Notice">Select Group to Post Notice </label>
<select class="bg-white text-danger form-control " name='GroupID[]' multiple>
#foreach ($users as $user)
<option value="{{ $user->GroupID }}">{{ $user->GroupID }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="Enter Notice">Enter Notice</label>
<input class="bg-white text-danger p-2 form-control form-control-sm" type="text" name="Notice" placeholder="Enter Notice">
</div>
<input class="btn btn-danger btn-lg px-5" type="submit" name="submit">
</form>
controller
public function store(Request $request)
{
$member = $request->input('GroupID');
foreach($member as $value) {
$storeInfo = new notice();
$storeInfo->GroupID = $request->input('GroupID');
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('/notice');
}
I would imagine the reason you're getting this error is because of:
$storeInfo->GroupID = $request->input('GroupID');
$request->input('GroupID') will return an array (name='GroupID[]') and not an individual id.
Since you're already looping through the group ids you can instead use the value for the GroupId:
public function store(Request $request)
{
foreach ($request->input('GroupID') as $groupId) {
$storeInfo = new notice();
$storeInfo->GroupID = $groupId; //<--here
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('notice');
}
try changing controller logic
public function store(Request $request)
{
//
$member=$request->input('GroupID');
foreach($member as $value){
$storeInfo = new notice();
$storeInfo->GroupID = $value;
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('/notice');
}
I want to display data in textfield . Not all data. When search by name and it have data then went to display data in textfield. And search form and want to display form is same form.
<form action="postAuth" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="text" class="form-control" name="productname" placeholder="Search Product"> <span class="input-group-btn">
<button type="submit" class="btn btn-default" name="search">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<div class="form-group">
<label for="ProductName" >Product Name :</label>
<input type="text" class="form-control" name="ProductName">
</div>
</form>
route
Route::post("postAuth", ['as' => 'search' , 'uses'=> 'ProductController#postAuth']);
That's my controller
public function postAuth(Request $request)
{
//check submit
$update = $request->get('update',false);
if($update){
return $this->update($request);
}
$productname = $request->input('productname');
$product = DB::table('products')
->where('product_name','LIKE','%'.$productname.'%')
->get();
if($product->count() > 0)
return redirect()->to('/update')->withDetails($product)->withQuery($productname);
else
$request->session()->flash('alert-danger','No Data Found!');
return redirect()->to('/update');
}
can anyone help me please
Here what you exactly want : Just for example
<div class="form-group">
{{Form::label('name', trans('admin.venue.fields.name'),['class' => 'col-md-4 control-label'])}}
<div class="col-md-6">
{{Form::text('name',old('name', isset($venue) ? $venue->name : ''),['class' => 'form-control'])}}
</div>
</div>
Model function example :
public function save(Request $request) {
try {
$this->validate($request, Venue::rules(), Venue::messages());
$venue = Venue::saveOrUpdate($request);
if($venue !== false) {
if($request->get('continue', false)) {
return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
} else {
return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
}
} else {
return back()->with('error', "Unable to save venue")->withInput();
}
} catch (\Exception $ex) {
return back()->with('error', "Unable to save venue")->withInput();
}
}
Hope it is useful.
use session() in the value field of ProductName input tag and with some logic, for example
view file:
#if(session()->has('data'))
#if(count(session('data')))
#foreach(session('data') as $data)
<input type="text" class="form-control" name="ProductName" value="{{ $data->prodcut_name }}">
#endforeach
#else
<input type="text" class="form-control" name="ProductName" value="No Data Available">
#endif
#else
<input type="text" class="form-control" name="ProductName">
#endif
and in Controller:
public function postAuth(Request $request) {
$productname = $request->input(productname);
$product_search = Products::where('product_name', $productname)->get();
if($product_search) {
return redirect()->back()->with('data', $product_search);
}
}