How to use laravel redirect back with compact - laravel

I am trying to pass value from the price page via input and redirect back to the price page with a value that has been calculated. I am using two functions for this; one is for get price()-PagesController#pricing and the other one is for post feeCal()-PagesController#feeCal.
public function pricing(Request $request)
{
return response()->view('pages.price');
}
public function feeCal(Request $request)
{
$value = $request->input('price');
if ($value > 500 or $value > 20000) {
$fee = $value * 0.015;
}
// dd($fee);
return redirect()->back()->compact('fee');
}
<form action="{{ route('page.vfee')}}" method="POST">
#csrf
<div class="row">
<input type="number" name="price" class="form-control rounded mb-3" placeholder="Type the price here">
</div>
<div class="row">
<input type="submit" class="btn btn-danger w-100
rounded" value="GET TOTAL FEE">
</div>
</form>
<div class="card-footer">
<div class="text-muted">Total fee</div>
<div class="text-danger">
{{ $fee ?? '' }}
</div>
</div>
When you try to input a value it returns a black value but when you dump the fee variable in the controller function you get a result.

Try one of following
return redirect()->back()->with(compact('fee'));
or
return redirect()->back()->with('fee', $fee);

Related

Receiving parameters by URL loads all the content

I have a project that shows orders between dates with two dates
Route:
Route::get('/order/manage/all/{date1?}{date2?}', [\App\Http\Controllers\OrderController::class, 'all'])->name('order.manage.all');
View:
<form method="GET" action="{{route('order.manage.all')}}" id="buscador">
<div class="row">
<div class="form-group col">
<label for="date1" class="col-md-4 col-form-label">Fecha1</label>
<input type="date" class="mb-2"name="date1"/>
</div>
<div class="form-group col">
<label for="date2" class="col-md-4 col-form-label">Fecha2</label>
<input type="date" class="mb-2" name="date2"/>
</div>
<div class="form-group col">
<input class="btn btn-primary" type="submit" value="Filtrar"/>
</div>
</div>
</form>
#foreach($orders as $order)
<div class="card mb-5 line">
<h3 class="text-center">{{"Fecha de pedido: ".$order->date_order." | "."Referencia: ".$order->reference}} </h3>
<h3 class="text-center">{{$order->address->name." ".$order->address->surname1." ".$order->address->surname2}}</h3>
<h3 class="text-center">{{$order->address->address." (".$order->address->region.", ".$order->address->city.")"}}</h3>
<p class="text-center">{{\OrderItemHelper::countOrderItems($order->id)}} productos</p>
</div>
#endforeach
Controller:
public function all($date1 = null, $date2 = null) {
var_dump($date1);
var_dump($date2);
if (!empty($date1) && !empty($date2)) {
$orders = Order::whereBetween('date_order', [$date1, $date2])->get();
} else {
$orders = Order::orderBy('date_order', 'desc')->get();
}
return view('order.all', [
'orders' => $orders
]);
}
The problem I have is that when I receive these parameters by the URL (when doing the var_dump of $date1 it loads me all the content of the URL. And the $date2 returns null)
You are not actually using the route parameters here. You are using the request inputs from the query string. Route parameters are from the URI (which does not include the query string).
With what you are doing you would drop the parameters from the route definition and get the dates from the request.
Route::get('/order/manage/all', [\App\Http\Controllers\OrderController::class, 'all'])->name('order.manage.all');
public function all(Request $request)
{
$date1 = $request->input('date1');
$date2 = $request->input('date2');
...
}
Though, it looks like you may have a misconfigured server to go along with this as the query string might be being passed without the '?' and its ending up as a part of the path [based on your data dump].

why Laravel controller doesn't show the zero leading when search from number starts with 0

I'm using laravel and I can create a record with barcode = 069, or barcode = 006, however when I
Click the search button, it shows 69 in the form, below view, controller...etc, please I need help:
HTML where i enter value to search:
<div class="container my-container">
<div class="lead text-center font">
<h3><strong><u> Search Form</u></h3></strong>
</div>
<form action="{{ route('store.show')}}" method="GET">
{{ csrf_field() }}
<div class="input-group">
<input type="text" class="form-control" required name="name" placeholder="Scan Barcod">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary" name="submit" value="serach">
<span class="glyphicon glyphicon-search"></span>
</button>
</form>
</div>
Controller: for the same view
public function show()
{
$saved_barcode= request('name');
$searchbyfield = store::find($saved_barcode);
if ($searchbyfield) {
return view('store.show', ['store'=> $searchbyfield]);
}
return redirect('store/create')->with('missing', 'Item not Found');
}
HTML where show the search result
<form action= "{{ route('store.editbarcode',$store -> barcode) }}" method="GET">
{{ csrf_field() }}
<div class="input-group">
<span class="input-group-addon">
<strong>Asset ID </strong></strong>
<span style="color: red">*</span>
</span>
<input disabled type="text" class="form-control" required id="barcode" name="barcode" value="{{ $store -> barcode }}">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary" name="submit" >
<span class="glyphicon glyphicon-edit"></span>
</button>
</form>
</div>
This is most probably because you are saving barcode value in integer type column.
To allow your database engine store leading zeros, you should change the column type to varchar or string as in Laravel. It will solve your problem.
Assuming the Store has barcode as a string, you could pass the search input as a string instead of int, cuase when you pass it to ->find() it gets casted as int
What you can do is in your controller:
public function show()
{
$saved_barcode= request('name');
$searchbyfield = store::where('barcode', $saved_barcode)->first();
if ($searchbyfield) {
return view('store.show', ['store'=> $searchbyfield]);
}
return redirect('store/create')->with('missing', 'Item not Found');
}
EDIT
After reading what you said, perhaps you would wanna set this value in your Store model:
protected $primaryKey = 'barcode';
public $incrementing = false;
protected $keyType = 'string';

laravel 6 : Call to a member function store() on null

I am trying to attach a file to a blog type post. For this i have an file field and 2 buttons, one saves the fields to the database and the other uploads the file. standalone the file upload works as intended. However in the form i get Call to a member function store() on null. I have changed the menthod from put to post, but that doesnt seem have any effect.
Below my post forms and the function in the controllers.
The Form:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">New/Edit Blog</div>
<div class="card-body">
#if($data)
<form … action = "{{Route ('post.update', $data->_id)}}" method="post", enctype = 'multipart/data'>
#csrf
#method('POST')
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title" value = "{{$data->title}}" >
</div>
<div class="form-group">
<label for="shorttext">Shorttext:</label>
<input type="text" class="form-control" name="shorttext" value = "{{$data->shorttext}}" >
</div>
<div>
<input id="x" type="hidden" name="content" value ="{{$data->content}}">
<trix-editor input="x" ></trix-editor>
</div>
<div class="form-group">
<label for="created_by">Created By:</label>
<input type="text" class="form-control" name="created_by" value = "{{$data->created_by}}" readonly>
</div>
<input type="file" name="attachment" enctype="multipart/form-data"/>
<p align="center">
<input type="submit" btn btn-primary class="btn btn-primary" id="save" name="action" value="save">
<input type="submit" btn btn-primary class="btn btn-primary" id="upload" name="action" value="upload">
The functions in the controller:
Store:( this one is the function that determines which button is pressed)
public function store(Request $request, $_id = false, $attachment = false){
//check which submit was clicked on
if($request->action == 'upload'){
//
$this->upload($request);
return redirect()->route('home');
} elseif($request->action == 'save') {
//echo 'save pressed';
//run function save all form fields
$this->update($request, $_id);
return redirect()->route('home');
} else {echo "error";}
}
Upload function:
function upload(Request $request){
$path = $request->file('attachment');
// $original = $request->file('attachment')->getClientOriginalName();
$path->store('/public');
Update function:
public function update (Request $request, $_id){
//$this->upload($request);
/*
$path = $request->file('attachment');
$path->storeas('/public','123'); */
$data = post::findOrFail($_id);
$data->title = $request->title;
$data->content = $request->content;
$data->shorttext = $request->shorttext;
$data->created_by = $request->created_by;
$data->text3 = $request->text3;
$data->attachment = $request->attachment;
$data->save();
if($data){
return redirect()->route('home');
}else{
return back();
}
}
The route is:
Route::post('/post/update/{_id}', 'PostController#store')->name('post.update');
As mentioned, the save function in the complete form works. In a standalone form ( and a standalone controller) the upload works (and i can, if i wish, manipulate the filename), but how do i bring the 2 together? At first i thought it was because the update form had a PUT method, but changing everything to post doesnt seem to have any effect and i still get the Null error.
For completeness the standalone solution:
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Te7aHoudini\LaravelTrix\Pipes\AttachmentInput;
class UploadController extends Controller
{
//
function upload(Request $req){
$path = $req->file('attachment');
$original = $req->file('attachment')->getClientOriginalName();
$path->storeas('/public',$original);
echo $original;
}
}
The standalone form:
<html>
<head>
<title>Upload</title>
</head>
</html>
<form action="upload" method="POST" enctype="multipart/form-data">
<input type="file" name="attachment" />
#csrf
<button type="submit">file upload</button>
</form>
In your form element Change enctype = 'multipart/data' to enctype="multipart/form-data"
<form … action = "{{ Route ('post.update', $data->_id) }}" method="post", enctype="multipart/form-data">
Hope this solves your problem.

I want to make validation for same user assigned using laravel

I am a beginner and I am trying to make validation for the user assigned when I assign the same user again when I submit so the error message should be shown, this user is already assigned please help me how to do this? thanks.
Controller
public function adduseraction(REQUEST $request) {
// Users_permissions::where('user_id',$request->userid)->get()-
// >pluck('user_Access_id');
$useradd=$request->get('userid');
$checkid=$request->get('multiusersid');
$array = array();
foreach($checkid as $id){
$array[] = array('user_id'=>$useradd,'user_Access_id'=>$id);
}
$user=Users_permissions::insert($array);
return redirect('admin')->with('success', 'Users has been assigned');
}
html view
<form action="{{route('adduseraction')}}" method="post">
{{ csrf_field() }}
<div class="col-sm-4">
<label>Select User</label>
<select name="userid" class="form-control" id="typeofworkday">
<option>Select User</option>
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name}}</option>
#endforeach
</select>
</div>
<br> <br> <br>
<div class="card-body">
<label>Select Muliple User or One</label>
<!-- Minimal style -->
<div class="row">
#foreach($users as $user)
<div class="col-sm-2">
<div class="form-check">
<input type="checkbox" id="check" name="multiusersid[]" value="
{{$user->id}}" class="form-check-input" >
<h5 style="position:relative;left:10px;">{{$user->name}}</h5>
</div>
<!-- checkbox -->
</div>
#endforeach
</div>
<!-- /.card-body -->
</div>
<br><br><br><br>
<div class="card-footer">
<button type="submit" name="btnsubmit" class="btn btn-primary col-
md-2 center">Submit</button>
</div>
</form>
Ok you can do something like below
First check does user already exists in the table and if exists return redirect with the error message or else insert the data
public function adduseraction(Request $request) {
$isUserAvailable = Users_permissions::where('user_id',$request->userid)->get()- >pluck('user_Access_id');
//If user already exists
if($isUserAvailable) {
return redirect('admin')->with('error', 'Users has been assigned');
}else{
$useradd=$request->get('userid');
$checkid=$request->get('multiusersid');
$array = array();
foreach($checkid as $id){
$array[] = array('user_id'=>$useradd,'user_Access_id'=>$id);
}
$user=Users_permissions::insert($array);
return redirect('admin')->with('success', 'Users has been assigned');
}
}

Laravel 5 session()->keep method not working

I have a view with an input date field and a table beneath that. The table is populated based on the date entered. When the date is entered I use a POST method on the form which handles the DB request and returns the same view with the data. I'd like to also return the original date that was entered. I tried session()->keep and flashOnly methods. None return the input date to the view.
My controller:
public function groupTestAthletes(Request $request)
{
$inputDate = null;
$tests = null;
if ($request['tgroupdate']){
$inputDate = Carbon::createFromFormat('d/m/Y', $request['tgroupdate']);
$tests = Test::where('test_date', '=', $inputDate->format('Y-m-d'))
->orderBy('athlete_id', 'desc')
->get();
}
$request->session()->keep(['tgroupdate']);
//$request->flashOnly(['tgroupdate']);
return view('npr.test_athletes', ['tests' => $tests]);
My view:
<form class="form-inline" role="form" method="POST" action="{{ route('admin.search_tgroup') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="tgroupdate" class="control-label">Test Date</label>
<div class="input-group date" id="testgroupdatepicker">
<input name="tgroupdate" type="text" style="background-color:#ffffff" readonly=""
value="{{ Session::get('tgroupdate') }}" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Search Athletes
</button>
<input type="hidden" name="_token" value="{{ Session::token()}}">
</div>
</form>
You dont need to save the date in session. You can save the date in a variable,send it to the view and in the view you can check if variable exist using isset php function.
In Controller
public function groupTestAthletes(Request $request)
{
$inputDate = null;
$tests = null;
if ($request['tgroupdate']){
$inputDate = Carbon::createFromFormat('d/m/Y', $request['tgroupdate']);
$tests = Test::where('test_date', '=', $inputDate->format('Y- m-d'))
->orderBy('athlete_id', 'desc')
->get();
}
return view('npr.test_athletes', ['tests' => $tests,'selected_date' => $request['tgroupdate']]);
And in the view,
<form class="form-inline" role="form" method="POST" action="{{ route('admin.search_tgroup') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="tgroupdate" class="control-label">Test Date</label>
<div class="input-group date" id="testgroupdatepicker">
<input name="tgroupdate" type="text" style="background-color:#ffffff" readonly=""
value="#if(isset($selected_date)) $selected_date #endif" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Search Athletes
</button>
<input type="hidden" name="_token" value="{{ Session::token()}}">
</div>
</form>
Edit: This minor change in the view gave the optimal solution.
value="#if(isset($selected_date)){{$selected_date}}#endif"

Resources