简体   繁体   中英

Laravel 5.4 how to passing id value from one function to another function?

I want to passing person_id value from function search() to function view_application() to return view data to detail page with this person_id. here my controller code is

    <?php

namespace App\Http\Controllers;

use Faker\Provider\ar_JO\Person;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Application;
use App\Personal;

class CandidatesApplyController extends Controller
{

    public function index(){
        return view('admin.candidates-apply')
            ->with('application', Application::all());
    }

    public function search(Request $request)
    {
        if ($request->ajax())
        {
            $output="";
            $application = DB::table('application')->where('person_id', 'LIKE', '%' .$request->search.'%')
                ->orWhere('name_khmer', 'LIKE', '%' .$request->search.'%')
                ->orWhere('name_latin', 'LIKE', '%' .$request->search.'%')
                ->get();

            if ($application) {
                foreach ($application as $key => $applications) {
                    $output .= '<tr>'.
                        '<td>'.$applications->person_id.'</td>'.
                        '<td>'.$applications->name_khmer.'</td>'.
                        '<td>'.$applications->name_latin.'</td>'.
                        '<td>'.$applications->app_sex.'</td>'.
                        '<td>'.$applications->app_dob.'</td>'.
                        '<td>'.$applications->app_nat.'</td>'.
                        '<td>'.$applications->app_pob.'</td>'.
                        '<td>'.$applications->app_current.'</td>'.
                        '<td>'.$applications->e_mail.'</td>' .
                        '<td>'.$applications->telephone.'</td>'.
                        '<td>'.$applications->app_no.'</td>'.
                        '<td>'.$applications->app_title.'</td>'.
                        '<td>'.$applications->created_at.'</td>'.
                        '<td>'."
                        <div class='btn btn-md btn-info' style='margin-left: 15px; padding: 10px 15px 0px 15px;'>
                        <a href='#' style='color: #fff;'><p>ពិនិត្យ</p></a>
                        </div>".'</td>'.

                        '<td>'."
                        <div class='btn btn-md btn-info' style='margin-left: 15px; padding: 10px 15px 0px 15px;'>
                        <a href='./candidate-application' target='_blank' style='color: #fff;'><p>ពិនិត្យ</p></a>
                        </div>".'</td>'.
                        '</tr>';
                }
                return response()->json($output);

            }

        }

        return view('admin.candidates-apply', compact('application'));

    }


    public function view_application($id){
        $app = DB::table('application')->wherePersonId($id)->get();
        return view('admin.candidate-application', compact('app'));

    }

}

and here my route is

    Route::get('/candidates-apply', 'CandidatesApplyController@search')->middleware('auth');

/* Candidate Application Detail View Route*/
Route::get('/candidate-application', 'CandidatesApplyController@view_application', function (){
    return view('admin.candidate-application');
})->middleware('auth');

How to do this. Thanks and Regards,

If I understand correctly what you are trying to do.

1 solution :

modify your route to this:

Route::get('/candidate-application/{id}', 'CandidatesApplyController@view_application', 
})->middleware('auth');

and your action link should be for exmaple /candidate-application/4 if your candidate id is 4, and it will be automatically passed to the id parameter you have in your function.

Add this to your link in search function

 <a href='/candidate-application/'.$applications->person_id></a>

2 Solution:

You can submit a form with the id and retrieve it the same way you do with search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM