SleepWalker
SleepWalker

Reputation: 637

view not found in Passing ID on create function on laravel

I would like to pass the user_id on create page.

My controller

public function create($user)
{

    $loanAmount=LoanAmount::all()->pluck('amount','id');
    $userId=User::where('id',$user)->pluck('id')->first();
    // $user->id;
    // dd($user);
    return view('loan/grant-loan-amounts/create/'.$userId)
                ->with($userId)
                ->with($loanAmount);
}

Here's my route

Route::resource('loan/grant-loan-amounts', 'Loan\\GrantLoanAmountsController',[
    'except' => ['create']
]);
Route::get('loan/grant-loan-amounts/create/{userId}', 'Loan\\GrantLoanAmountsController@create')->name('grant-loan-amounts.create');

I made the create page blank. with only "asd" to display.

what I want to achieve is that, from user list which is on user folder, I'll pass the user id on grant loan amount create a page. But I can't figure out why the route can't be found.

any suggestion?

Upvotes: 0

Views: 63

Answers (3)

Ripon Uddin
Ripon Uddin

Reputation: 714

pluck required one more parameter for fetch data.

public function create($user)
{

$loanAmount=LoanAmount::all()->pluck('amount','id');
$userId=User::findOrFail($user);
return view('YourBladeFileFolderLocationOrFileLocationHere')->compact('userId','loanAmount');
}

now you can fetch or write at blade file like For Id :
{{ $userId->id }}

For User Name :
{{ $userId->name }}

Upvotes: 0

smartrahat
smartrahat

Reputation: 5609

Don't pass the id in the first parameter of view() function. Only mention the view file in the first parameter of the view function. Pass all variables with compact() function as the second parameter of view(). Example:

public function create($user)
{
    $loanAmount=LoanAmount::all()->pluck('amount','id');
    $userId=User::query()->findOrFail($user)->id;

    return view('user.create',compact('userId','loanAmount')) //in this example 'user' is the folder and 'create' is the file inside user folder
}

Upvotes: 1

Sythe Veenje
Sythe Veenje

Reputation: 46

Because you have this route:

Route::get('loan/grant-loan-amounts/create/{userId}', 'Loan\\GrantLoanAmountsController@create')->name('grant-loan-amounts.create');

You named the route so you should call it by it's name:

return redirect()->route('grant-loan-amounts.create', $userId);

Upvotes: 0

Related Questions