cannot pass multiple variables to view laravel

I have a problem : I’m currently working on aproject of a web app for a library (it’s for school).

And I want to return my view with 3 variables form queries, but it doesn’t seem to work. It works for the 2 first but not for the 3rd.

here is my code :

$search_text = $_GET['query'];
            $results = DB::table('clients')->where('id','=',$search_text)->paginate(3); 
            $resultsloan = DB::table('emprunts')->where('clientid', '=', $search_text)->paginate(3);

            // compte le nombre de prets au client, si le nombre est supérieur à 4, le btn pour selectionner le client disparait et un msg d'avertissement apparait (dans formloan)
           $limitemprunts = Emprunt::WhereIn('clientid',[$search_text])->where('clientid',$search_text)->distinct()->get()->count();
            
            return view('crud.formloan', ['results'=>$results],['limitemprunts'=>$limitemprunts],['resultsloan'=>$resultsloan]);

But the $resultsloan seems to never get to my view …
can someone please help me ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’re passing it wrong to view. You’ve to use it something like this:

return view('foo',compact('key'=>'bazz','key'=>'bar','key'=>'foo bar'));

or you can do it something like this:

return view('foo',['key'=>'bazz', 'key'=>'bar', 'key'=>'foo bar']);

Method 2

To expand on @Hasssaans’s anwer, there are a few ways to pass variable to a view. This can depend on your coding style and also the amount you want to pass.

Another example I use:

return view('foo')->with(['key' => $value, 'users' => $users]);

Method 3

Change your return view as:

return view('crud.formloan', compact('results', 'limitemprunts', 'resultsloan'));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x