How to validate 2 times - laravel

I can sucess First page's Laravel validation. After that page I would like to put another data input and I need to validate the data. Here is my code. When I click submit buttom there I backed First page. Could you teach me what is wrong my code?
// This page's validation is OK.
public function confirm(Request $request)
{
$rules = [
'orderer' => 'required'
];
$this->validate($request, $rules);
$data = $request->all();
$request->session()->put('data',$data);
return view('front.confirm', compact("data"));
}
// problem is here
public function complete(Request $request)
{
$rules = [
'num' => "required|in:1,2,3,4,5,6,7,8,9,10",
];
$this->validate($request, $rules);
$data = $request->session()->pull('data');
$token = array_shift($data);
$data = array_merge($data, $request->all());
$Contact = Contact::create($data);
}
UPDATES 2
web.php
Route::post('mail/complete','MailController#complete');
complete.blade.php
<form class="form-horizontal" role="form" method="post" action="http://test.ivory.ne.jp/order/public/mail/complete">
<div class="form-group">
<select class="form-control" name="p1q" value="{{ old('p1q') }}">
<option value="-" selected>select </option>
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
</select>
  </div>
<button class="btn btn-lg btn-primary btn-block" type="submit">送信</button>
</form>

Thanks for updating the code. So it seems that your are trying to validate a field num which is not present on your $request object, since the name attribute of your HTML <select> is set to p1q.
So in order to make your validation function work, you need to either set
$rules = [
'p1q' => "required|in:1,2,3,4,5,6,7,8,9,10", //mind the changed field name change from "num" to "p1q"
];
or adapt your HTML markup to <select name="num" ... >.

Related

laravel8 checkbox deleting last inserted options on view

good morning, I come accross the following problem, i have some checkboxes, they store correctly and appear on the phpmy admin, but the prevous submited forms on the view page only show the last inserted, if someone know how to fix it tell me please
i have this code:
view blade file
<td>
#foreach($item->motivo_deslocacao as $motivo)
{{$motivo}},
#endforeach
</td>
<td>
#foreach($item->servico_afetacao as $servico)
{{$servico}},
#endforeach
</td>
create.blade file
<input type="checkbox" class="form-check-input" name="motivo_deslocacao[]" value="motiv">motiv
<input type="checkbox" class="form-check-input" name="motivo_deslocacao[]" value="motivdois">motivdois
<input type="checkbox" class="form-check-input" name="servico_afetacao[]" value="serve">serve
<input type="checkbox" class="form-check-input" name="servico_afetacao[]" value="servedois">servedois
model
protected $casts = [
'motivo_deslocacao' => 'array',
'servico_afetacao' => 'array',
];
controller
public function create(Request $request)
{
$motivo_deslocacao = array($request->motivo_deslocacao);
$servico_afetacao = array($request->servico_afetacao);
return view('admin.formulario.create', [
'viaturas' => Viaturas::all(),
'motivo_deslocacao' => $motivo_deslocacao,
'servico_afetacao' => $servico_afetacao,
]);
}
public function store(Request $request)
{
$data->motivo_deslocacao = $request->input('motivo_deslocacao');
$data->servico_afetacao = $request->input('servico_afetacao');
$data->save();
}

Return Redirect Laravel

I want to return to the page with Link : localhost:8000/order/show/O1/T1.
O1 is $order->id_order and T1 is $trip->id_trip.
Here's my code.
Route::get('/order/show/{id_order}/{id_trip}', 'OrderController#show');
Route::get('/order/update_driver/{id_order}/{id_trip}', 'OrderController#update_driver');
Order Controller
public function show($id_order, $id_trip){
$trip = Trip::find($id_trip);
$order = Order::where(['id_order' => $id_order, 'id_trip' => $id_trip])->first();
$detail_order = Detail_Order::where(['id_order' => $id_order, 'id_trip' => $id_trip])->first();
$detail = Order::join('detail_order', 'order.id_order', '=', 'detail_order.id_order')
->where('order.id_order', $id_order)
->select('order.id_trip as order_trip',
'order.id_order as order_id',
'order.id_users as order_users',
'order.date_order as order_date',
'detail_order.id_seat as detail_seat',
'detail_order.users as detail_users')
->get();
$driver = Driver::all();
return view('travel.order.show', ['trip' => $trip, 'order' => $order, 'detail' => $detail, 'detail_order' => $detail_order, 'driver' => $driver]);
}
public function update_driver($id_order, $id_trip){
$driver = Input::get('id_users_driver');
Detail_Order::where('id_order', $id_order)
->update(['id_users_driver' => $driver]);
session()->flash('flash_success', 'Data has been updated');
return redirect('/order/show/{id_order}/{id_trip}');
}
View
<form method="get" action="/order/update_driver/{{ $order->id_order}}/{{ $order->id_trip}}">
<label>Driver</label>
<select class="form-control" name="id_users_driver" id="id_users_driver">
<option value=""> Driver </option>
#foreach($driver as $d)
<option value="{{$d->id_users}}"{{$detail_order->id_users_driver == $d->id_users ? 'selected' : ''}}>{{$d->name}}</option>
#endforeach
</select>
#if($errors->has('id_users_driver'))
<div class="text-danger">
{{ $errors->first('id_users_driver')}}
</div>
#endif
<input type="submit" class="btn btn-primary" value="Save">
</form>
It returned error Trying to get property 'id_order' of non-object.
Do you know how to make it return to localhost:8000/order/show/O1/T1? Thank you

Update data in laravel 6

I try to create crud in laravel 6. Create, Read and Delete process is running well. But when Update process, the data in table not change. Could anyone help me to find the problem ? The following my code.
Route
Route::get('/blog', 'BlogController#index');
Route::get('/blog/add','BlogController#add');
Route::post('/blog/store','BlogController#store');
Route::get('/blog/edit/{id}','BlogController#edit');
Route::post('/blog/update','BlogController#update');
Controller
public function index()
{
$blog = DB::table('blog')->get();
return view('blog',['blog' => $blog]);
}
public function edit($id)
{
$blog = DB::table('blog')->where('blog_id', $id)->get();
return view('edit', ['blog'=>$blog]);
}
public function update(Request $request)
{
DB::table('blog')->where('blog_id',$request->blog_id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
View
#foreach ($blog as $n)
<form method="post" action="/blog/update" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
You must provide id in your route
Route::post('/blog/update/{id}','BlogController#update');
In update method add parameter id and then find product against id
public function update(Request $request, $id)
{
DB::table('blog')->where('blog_id',$id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
#foreach ($blog as $n)
<form method="post" action="{{ route('your route name'), ['id' => $$n->id] }}" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
try separating the update into two statements like so
$blog = DB::table('blog')->where('blog_id',$id)->first();
$blog->update([
'blog_title' => $request->title,
'author' => $request->author]);
Also you might want to use models in the future so you can do it like
$blog = Blog::where('blog_id',$id)->first();
Doesn't really shorten your code but it improves the readibility.
Do your update like this:
public function update(Request $request)
{
$post = DB::table('blog')->where('blog_id',$request->blog_id)->first();
$post->blog_title = $request->title;
$post->author = $request->author;
$post->update();
return redirect('/blog');
}

Laravel - Drop down list where statement

I currently have a single drop down list that when i select the value it will display the data. However i am now adding in a new drop down list but when i press submit nothing is appearing.
I select distance and press submit data is presented. However, I have now included a price drop down list however nothing is appearing when i press submit. Can someone please help, see code:
SearchController.php
public function index(Request $request)
{
$distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
$prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');
$postsInRange = $request->has('distance')
? Post::where('distance', $request->distance)->get()
: [];
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $postsInRange
]);
Search.php
<div class="form-group">
<select name="distance" id="distance" class="form-control input-lg dynamic"
data-dependent="state">
<option value="">Distance</option>
#foreach($distances as $distance)
<option value="{{ $distance }}">{{ $distance }}</option>
#endforeach
</select>
<br>
<select name="price" id="price" class="form-control input-lg dynamic" data-
dependent="state">
<option value="">Price</option>
#foreach($prices as $price)
<option value="{{ $price}}">{{ $price}}</option>
#endforeach
</select>
Try this:
public function index(Request $request)
{
$distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
$prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');
$postsInRange = $request->has('distance') ? Post::where('distance', $request->distance)->get()
: [];
$postsPrice = $request->has('price') ? Post::where('price', $request->price)->get()
: [];
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $postsInRange,
'postsPrice' => $postsPrice
]);

My model always give empty even I have data in database Laravel 5

UPDATE
I just found the problem here, it is typo at $instID = $request->insititution;
It should be $instID = $request->institution;
Thanks for all your helps..
UPDATE
I trying to insert to CRConfigDetail Model / table, but first I need to get my CRConfigID from CRConfig Model / Table with specific rules. So I can get the CRConfigID and put it to CRConfigDetail column.
But every time I trying to retrieve, it always give empty data even I already have data at my database. In other Controller I can retrieve data with similar rules.
Am I do something wrong with logic? Because I don't see any errors.
Here is my HTML / form:
<form action="doInsertSCC" method="POST" enctype="multipart/form-data" id="scheduleDetailForm">
{{csrf_field()}}
<div class="form-group">
<label for="institution">Institution</label>
<select name="institution" class="form-control" id="institution">
</select>
</div>
<div class="form-group">
<label for="acadCareer">Academic Career</label>
<select name="acadCareer" class="form-control" id="acadCareer">
</select>
</div>
<div class="form-group">
<label for="period">Period</label>
<select name="period" class="form-control" id="period">
</select>
</div>
<div class="form-group">
<label for="department">Department</label>
<select name="department" class="form-control" id="department">
</select>
</div>
<div class="form-group">
<label for="fos">Field of Study</label>
<select name="fos" class="form-control" id="fos">
</select>
</div>
<div class="form-group">
<label for="scc">Lecturer's ID - Name</label>
<select name="scc" class="form-control" id="scc">
</select>
</div>
<button type="submit" class="btn btn-default">Assign SCC</button>
<div id="search" class="btn btn-default">Search</div>
</form>
Here is my Route to access my Controller
Route::post('/doInsertSCC', "ScheduleController#insertSCC");
And here is my ScheduleController
public function insertSCC(Request $request){
$this->validate($request, [
'scc' => 'required'
]);
$instID = $request->insititution;
$acadID = $request->acadCareer;
$termID = $request->period;
$depID = $request->department;
$rule = ['institutionID' => $instID, 'acadCareerID' => $acadID, 'termID' => $termID, 'departmentID' => $depID];
$crConfig = CRConfig::where($rule)->first();
if( !empty($crConfig) ){
foreach ($crConfig as $cr) {
$crConfigID = $cr->CRConfigID;
}
$schedule = new CRConfigDetail;
$schedule->status = 'Pending';
$schedule->numRevision = 0;
$schedule->FOSID = $request->fos;
$schedule->SCC = $request->scc;
$schedule->CRConfigID = $crConfigID;
$schedule->save();
return redirect("AssignSCC")->with('status', 'Add SCC success!');
}else{
return redirect("AssignSCC")->with('status', 'Add schedule first!');
}
}
I already check my rules data are match with my CRConfig table's data (using console.log()
Everytime I submit this form, I will do the "else" and redirect with "Add schedule first!" message.
Actually, it does accept an array, but the array should be formatted as such..
$rule = [
['institutionID', '=', $instID],
['acadCareerID', '=', $acadID],
['termID', '=', $termID],
['departmentID', '=', $depID]
];
Source: https://laravel.com/docs/5.4/queries#where-clauses
use the below one
$crConfig = CRConfig::where('institutionID' , $instID)
->where('acadCareerID' , $acadID)
->where('termID', $termID)
->where('departmentID', $depID)
->first();
instead of
$rule = ['institutionID' => $instID, 'acadCareerID' => $acadID, 'termID' => $termID, 'departmentID' => $depID];
$crConfig = CRConfig::where($rule)->first();
You can check the query built at the backend by getting the query log, for this you have to enable the query log before the query gets built and get the query log after the query gets built both query log methods belongs to DB facade
\DB::enableQueryLog();
\DB::getQueryLog();

Resources