How to pass multiple parameters to route - laravel

Hello i have this function called show and it has 2 parameters id and options this is the code from blade.php :
<a href="{{route('orders.show',$order->id,'1')}}">
<button>Edit</button>
</a>
The number one is static but i have another button that has value 2 now this is the code from the controller:
public function show($id, $option)
{
$orders = Order::with('client', 'products')->where('id', $id)->firstOrFail();
$clientList = Client::all();
$productList = Product::all();
switch ($option)
{
case 1:
{
return view('orders.edit', compact('orders', 'clientList', 'productList'));
break;
}
case 2:
{
return view('orders.show', compact('orders', 'clientList', 'productList'));
break;
}
}
}
But when i execute teh code i get this error:
Too few arguments to function App\Http\Controllers\OrderController::show(), 1 passed in /home/user/Desktop/k_project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 2 expected
Basically the second argument is not passing i used another method like this:
Student detail
From this page: Page
While before i passing just one value $id and it worked fine but now i need a second parameter to pass.
This is my route definition that just one value passing $id works but with two values passing $id and $option will make error:
Route::resource('orders', OrderController::class);

Your route has 3 parameters id, parameter, name.
You can have two solutions for that.
1. First Solution
route.php
Route::get('/orders/show/{id}', 'OrderController#show')->name('orders.show');
OrderController.php
public function show(Request $request, $id)
{
$name = $request->input('name');
$parameter = $request->input('parameter');
....
}
2. Second Solution
route.php
Route::get('/orders/show/{id}/{name}/{parameter}', 'OrderController#show')->name('orders.show');
OrderController.php
public function show($id, $name, $parameter)
{
....
}

Related

Too few arguments to function App\Http\Controllers\SupervisorController::edit(), 0 passed and exactly 1 expected

Sorry this is very basic, but i dont know what is the problem.
I always get this error when trying to visit edit_supervisor.
Route:
Route::get('/edit_supervisor', 'SupervisorController#edit')->name('edit_supervisor');
SupervisorController:
public function edit($id)
{
return view('DataSupervisor.edit');
}
you can add $id as a parameter like this in Route
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
or you can change the function parameter $id to Request $request
public function edit(Request $request)
{
return view('DataSupervisor.edit');
}
in blade
edit
for showing previous data you need to get data using $id in controller
$supervisor = Model::findOrFail($id);
return view('DataSupervisor.edit', compact('supervisor'));
You need to pass the id :
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
On blade :
<a href="{{ route('edit_supervisor', $id) }}...

I created 2 functions to same view in same controller but last route's function only working

I created 2 functions to same view in same controller but last one only working
This is my function
class ProspectController extends Controller {
public function get_prospects() {
$prospects = DB::select('select * from prospect');
return view('prospect', ['prospects' => $prospects]);
}
public function get_courses() {
$courses = DB::select('select * from course');
return view('prospect', ['courses' => $courses]);
}
}
This is my route
Route::get('prospect', 'ProspectController#get_courses');
Route::get('prospect', 'ProspectController#get_prospects');
This is my view file
#foreach($courses as $course)
<input type="checkbox" id="{{$course->course_id}}"
name="course_intrested[]" value="{{$course- >course_name}}">
<label for="defaultCheck">{{$course- >course_name}}</label>
#endforeach
But i'm getting this error
Undefined variable:
courses (View:C:\xampp\htdocs\laravel\customer_inquiry_model\resources\
views\prospect.blade.php)
But course function working when I change route like this
Route::get('prospect', 'ProspectController#get_prospects');
Route::get('prospect', 'ProspectController#get_courses');
but first one is not working. This is my problem.....
You are using duplicate routes. Therefore only the last route is being used.
And in the first case, you pass prospects variable and try to use courses so it throws an error.
public function get_prospects() {
$prospects = DB::select('select * from prospect');
return view('prospect', ['prospects' => $prospects]); // <---- 'prospects' should be 'courses'
}
But even if you change variable name, your logic still remains wrong. You need to set two different routes (and most probably two different template files) Like following:
Route::get('courses', 'ProspectController#get_courses');
Route::get('prospect', 'ProspectController#get_prospects');
UPDATE
As you mentioned in the comment, if you would like to pass courses and prospects to the same view you can do the following:
public function get_prospects() {
$prospects = DB::select('select * from prospect');
$courses = DB::select('select * from course');
return view('prospect', ['prospects' => $prospects, 'courses' => $courses]);
}
And you need to remove the second route and leave it as following:
Route::get('prospect', 'ProspectController#get_prospects');

Input array loop on controller laravel 5

I have inputs array and i need to make a foreach but laravel $request->all() only return last one:
url:
http://localhost:8000/api/ofertas?filter_pais=1&filter_pais=2&filter_pais=3
controller:
public function filtroOfertas(Request $request){
return $request->all();
}
result:
{"filter_pais":"3"}
result should return 1, 2 and 3 and i need to make a foreach in filter_pais.
Any solution? Thanks
Use [] at the key of query string.
http://localhost:8000/api/ofertas?filter_pais[]=1&filter_pais[]=2&filter_pais[]=3
It will be parsed as array.
Repeated parameters make no sense and should be avoided without exception.
But looking at other solutions, there are several:
routes.php
Route::get('/api/ofertas/{r}', 'Controller#index');
Controller:
public function index($r)
{
$query = explode('&', $r);
$params = array();
foreach($query as $param)
{
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
// $params contains all parameters
}
Given that the URL has no question marks:
http://localhost:8000/api/ofertas/filter_pais=1&filter_pais=2&filter_pais=3

Passing route parameter to controller Laravel 5

I'm trying to pass a route parameter to controller, but I get this error : Argument 2 passed to App\Http\Controllers\JurnalController::store() must be an instance of App\Http\Requests\JurnalRequest, none given
Below are the codes ..
Route :
Route::get('/edisi/{id}', 'JurnalController#store');
Controller :
public function store($id, JurnalRequest $request) {
$input = $request->all();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
$jurnal = Edisi::findOrFail($id)->jurnal()->create($input);
return redirect('jurnal');
}
So my question is how to pass the route parameter properly ? Thank you
new routes :
Route::get('/', function () {
return view('pages/home');
});
Route::group(['middleware' => ['web']], function () {
Route::get('edisi', 'EdisiController#index');
Route::get('edisi/create', 'EdisiController#create');
Route::get('edisi/{edisi}', 'EdisiController#show');
Route::post('edisi', 'EdisiController#store');
Route::get('edisi/{edisi]', 'EdisiController#edit');
Route::patch('edisi/{edisi}', 'EdisiController#update');
Route::delete('edisi/{edisi}', 'EdisiController#destroy');
});
Route::get('/edisi/{id}', 'JurnalController#storejurnal');
Route::group(['middleware' => ['web']], function () {
Route::get('jurnal', 'JurnalController#index');
Route::get('jurnal/create', 'JurnalController#create');
Route::get('jurnal/{jurnal}', 'JurnalController#show');
Route::post('jurnal', 'JurnalController#storejurnal');
Route::get('jurnal/{jurnal}/edit', 'JurnalController#edit');
Route::patch('jurnal/{jurnal}', 'JurnalController#update');
Route::delete('jurnal/{jurnal}', 'JurnalController#destroy');
});
new storejurnal method :
public function storejurnal(JurnalRequest $request, $id) {
$input = $request->all();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
//Insert data jurnal
$jurnal = Edisi::findOrFail($id)->jurnal()->create($input);
return redirect('jurnal');
}
When you are using resource controller, the store method does not accept any other argument except the Request instance. Try changing the method name or remove the second argument. store() method be default accepts post requests not get requests. Either put your route on top of the resource controller or change the method name.
Route::get('/edisi/{id}', 'JurnalController#store');
Route::resource('jurnals', 'JurnalController');
I hope this helps.
The correct format is:
public function store(JurnalRequest $request, $id) {
// your code
}
If you receive an argument such as Missing argument 2 as suggested in your comments, it means that either you aren't generating the routes correctly, or the url doesn't include the id segment.

Handling non objects

I might have a controller function like so
public function index()
{
$poll = DB::table('poll')->whereNull('deleted_at')->orderBy('id', 'desc')->first();
$question = DB::table('poll_question')->whereNull('deleted_at')->where('poll_id', $poll->id)->first();
$answers = DB::table('poll_answer')->whereNull('deleted_at')->where('question_id', $question->id)->orderBy('id')->get();
return view('index', compact('poll', 'question', 'answers'));
}
This is fine if the three collections I am obtaining contain data. If they dont and I try to visit the index page, I get
ErrorException in PollResponseController.php line 20: Trying to get property of non-object
So what is the best way to handle non-object's? To bypass this, I could do
public function index()
{
$poll = DB::table('poll')->whereNull('deleted_at')->orderBy('id', 'desc')->first();
if($poll) {
$question = DB::table('poll_question')->whereNull('deleted_at')->where('poll_id', $poll->id)->first();
if($question) {
$answers = DB::table('poll_answer')->whereNull('deleted_at')->where('question_id', $question->id)->orderBy('id')->get();
return view('index', compact('poll', 'question', 'answers'));
}
}
return view('error');
}
But is that not a bit exessive? I was just wondering if there was a better approach to handling this?
Thanks
You can use simple if($question) clause or if(is_null($question)).
$question = DB::table('poll_question')->whereNull('deleted_at')->where('poll_id', $poll->id)->first();
if($question){
// The object is not empty, so I'll use it
}else{
// The object is empty
}
In a blade template it will look like #if($question) and #if(is_null($question)) respectively.
#if($question)
{{ $question->property }}
#endif
In most cases you should just bypass all variables in a template and then check each of them with #if clauses.

Resources