Saranya Test
Saranya Test

Reputation: 87

laravel resource controller - show method not working $id returning value as 'show'

I am new to laravel and i am working in resource controller CRUD operation.i have done with show function and my front end will get the id from the user and i need to show the details using id. For this i have used resource controller show function . Problem is , how to return the id ? i tried with below formats

http://localhost/sbadmin/public/invoice/invoice/show?12 http://localhost/sbadmin/public/invoice/invoice/show/12

both are returning 404 only.

Can anyone suggest how to write this uri ?

Here is my web.php

<?php

Route::prefix('invoice')->group(function() {
Route::get('/createInvoice', 'Invoice\InvoiceController@getAllInvoiceDetails');
Route::post('/addInvoice', 'Invoice\InvoiceController@addInvoice');
Route::post('/checkPhoneNumberAndFetchData', 'Invoice\InvoiceController@checkPhoneNumberAndReturnData');
Route::resource('invoice', 'Invoice\InvoiceManagementController')->only([
'index', 'show'
]);
  Route::get('/searchInvoice','Invoice\InvoiceController@searchInvoice');

Route::get('/viewAllInvoice','Invoice\InvoiceController@viewAllInvoice');
Route::get('/viewInvoicesAsJson','Invoice\InvoiceController@viewInvoicesAsJson');

});

Controller:

  namespace Modules\Invoice\Http\Controllers\Invoice;

  use Illuminate\Routing\Controller;
  use Illuminate\Http\Request;
  use Modules\Invoice\Entities\Invoice;

  class InvoiceManagementController extends Controller
  {
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{

    return view('invoice::viewinvoices');
}



/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //return $id;
    $invoices = Invoice::findOrFail($id)->with(['user','events','payments'])->get();
    return view('invoice::invoice');
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}

 /**
 * Responds to requests to GET /users/show/1
 */
public function getShow($id)
{
    //
}
}

blade.php:

<div class="card o-hidden border-0 shadow-lg my-5">
  <div class="card-body p-0">
    <!-- Nested Row within Card Body -->
    <div class="row">
      <div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
      <div class="col-lg-7">
        <div class="p-5">
          <div class="text-center">
            <h1 class="h4 text-gray-900 mb-4">Search Invoice</h1>
          </div>
          <form id="form" onsubmit="return OnSubmitForm();" >
            @csrf
            <div class="form-group row">
              <div class="col-sm-6 mb-3 mb-sm-0">
                <input type="text" class="form-control form-control-user" name="invoice_id" id="id" placeholder="Enter Invoice number" >
              </div>

            </div>

            <button type="submit" class="btn btn-primary btn-user btn-block">
              View Invoice
            </button>
            <hr>
            <a href="index.html" class="btn btn-google btn-user btn-block" hidden="">
              <i class="fab fa-google fa-fw"></i> Register with Google
            </a>
            <a href="index.html" class="btn btn-facebook btn-user btn-block" hidden="">
              <i class="fab fa-facebook-f fa-fw"></i> Register with Facebook
            </a>
          </form>
          <div class="text-center" hidden="">
            <a class="small" href="forgot-password.html">Forgot Password?</a>
          </div>
          <div class="text-center" hidden="">
            <a class="small" href="login.html">Already have an account? Login!</a>
          </div>
        </div>
      </div>
    </div>
  </div>
   </div>

  </div> 
 <!-- Bootstrap core JavaScript-->
  <script src="{{ asset('sbadmin/vendor/jquery/jquery.min.js') }}"></script>
  <script src="{{ asset('sbadmin/vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>

  <!-- Core plugin JavaScript-->
  <script src="{{ asset('sbadmin/vendor/jquery-easing/jquery.easing.min.js') }}"></script>

  <!-- Custom scripts for all pages-->
  <script src="{{ asset('sbadmin/js/sb-admin-2.min.js') }}"></script>

  <script type="text/javascript">
  function OnSubmitForm()
  {
    $('#form').prop('action',"invoice/show/"+$('#id').val());

   return true;
 }
 </script>

Upvotes: 2

Views: 5064

Answers (3)

Rajkumar R
Rajkumar R

Reputation: 1097

For Laravel resource controller actions no need to add the CRUD actions

GET/invoice, mapped to the index() method,
GET /invoice/create, mapped to the create() method,
POST /invoice, mapped to the store() method,
GET /invoice/{id}, mapped to the show() method,
GET /invoice/{id}/edit, mapped to the edit() method,
PUT/PATCH /invoice/{id}, mapped to the update() method,
DELETE /invoice/{id}, mapped to the destroy() method.

And try to access your show route action like this:

http://localhost/sbadmin/public/invoice/invoice/12

Upvotes: 1

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

You don't need to append show in url.

Route::resource('invoice', 'Invoice\InvoiceManagementController')->only([
'index', 'show'
]);

http://localhost/sbadmin/public/invoice/invoice/show/12 -> remove show from here.

Now you url look like.

http://localhost/sbadmin/public/invoice/invoice/12 -> show method

Here, I elaborate more.

Get | http://localhost/sbadmin/public/invoice/invoice | index method

Post | http://localhost/sbadmin/public/invoice/invoice | store method

get | http://localhost/sbadmin/public/invoice/invoice/12 | show method.

Upvotes: 0

Reza Sheykhi
Reza Sheykhi

Reputation: 797

first of all change your prefix in routes to:

Route::group(['prefix' => 'invoice', 'as' => 'invoice.'], function(){

then in your script :

function OnSubmitForm()
  {
    $('#form').prop('action',"invoice/invoice/"+$('#id').val());

   return true;
 }

i hope this works.

Upvotes: 0

Related Questions