public function index()
{
$serial = Input::get('serial');
$device = DB::table('device')
->where('slno', $serial)->first(); // changed from devices as you're fetching a single model
// as a side note, if you're using Eloquent then you could do something like this instead:
// $device = Device::whereSlno($serial)->first(); // this is accessed via a magic method, but it does the same thing under the hood
if ( ! $device ) {
return view('custom error view here');
}
return view('pages.stock-in-fieldreturn', compact('device'));
}
if search is true show success message or else failed
Hi i have a form in that user input a number when submit it has to fetch all the information from the database. It is working fine but when user enters a serial number which is not present in database it showing nothing instead i want to show a message "details not found" This is my controller code
public function index()
{
$devices = DB::table('device')
->where('slno', Input::get('serial') )->first();
return view('pages.stock-in-fieldreturn', compact('devices'));
}
@mstnorris Now the view is not loading directly it is showing "custom error view here"
Obvisouly! You need to replace that part with either a custom error view, or decide how you're going to handle the null $device within your 'pages.stock-in-fieldreturn' view.
No what i mean is, if i go to that URl it is directly showing that message. But first it has to take the input from the user then it has to search for that record. if it is not present in the database then it has to show that error message.
So you want to display what the user searched for?
if ( ! $device ) {
return view('pages.error')->with('serial', $serial);
}
# pages/error.blade.php
// put your other view code here
<p>Sorry we couldn't find a device with the serial number: {{ $serial }}</p>
sorry you did not get my point. I want to show only message . here you will have a clear explanation this is my view
@extends('layouts.template')
@section('content')
<!-- page content -->
<div class="right_col" role="main">
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<h3 style="text-align: center">Stockin - Add Field-return</h3>
<div class="i-stockin-new">
<form action="field-return-details" method="POST" id="details-form">
<div class="form-group">
<input type="number" name="serial" class="form-control" id="stockfrserial" placeholder="Enter Serial number">
</div>
<input type="hidden" name="_token" value="{{csrf_token()}}">
<button type="submit" class="btn btn-primary">Get Details</button>
</form>
@if (isset($devices))
<form action="update-fieldreturn-details" method="POST">
<div class="form-group">
<input type="number" name="serial" class="form-control" id="stocknewserial" value="{{ old('slno', $devices->slno) }}">
</div>
<div class="form-group">
<input type="number" name="op" class="form-control" id="stocknewop" value="{{ old('opno', $devices->opno) }}">
</div>
<input type="hidden" name="_token" value="{{csrf_token()}}">
<button type="submit" class="btn btn-primary">Save and sent for testing</button>
</form>
@endif
</div>
</div>
</div>
</div>
</div>
<!-- /page content -->
@endsection
here user can enter a serial number . if the serial number is present in database then the details will be shown in the same view. if the serial number is not present in database i want to show a message like "details does not exist".
but when i used your code the above shown view is not showing . instead it is directly showing the message.
You're talking about an asynchronous request (AJAX). Check out this lesson on Laravel, VueJS, and AJAX. It will help, and I believe it is free, although I may be wrong.
I think you're going about this the wrong way. On one hand you're passing ALL of the devices to the view (there could be less than 10, or that could be in the 1000s - who knows).
You're better off having a form with a textfield within one view, that accepts a serial number. That form, submits a request to the server with the serial number and tries to find a device that matches.
Do you understand?
Please or to participate in this conversation.