Route with multiple Id's (Laravel) - php

I want to pass multiple ID's to my controller.First for Categories and second for Food items as show in function show
My Controller is:
public function index()
{
$Foods=Food::all();
$Category=Categories::all();
return view('index.welcome', compact('Foods','Category'));
}
public function show($Food_id,$Category_id)
{
$food = Food::with('restaurant','categories')->findOrFail($Food_id);
$category = Categories::with('food')->findOrFail($Category_id);
return view('index.show', compact('food','category'));
}
My Routes are:
Route::get('index','DetailsController#index');
Route::get('index/{Food_id?}', 'DetailsController#show');
But it returns me error "Missing argument 2 for App\Http\Controllers\Detailscontroller::show()".Where is the problem in this?

You are calling a method which has 2 arguments, but in the route file you're only specifying one parameter.
Maybe your route should look something like this:
Route::get('index/{Food_id?}/{Category_id}', 'DetailsController#show');
Then you would be passing both variables.
Or modify your method declaration and remove the $Category_id parameter, as you are not passing it to the method, like this:
public function show($Food_id)

Related

Try to double filter sql query data laravel

guys i trying to filter my data from mysql with where clause but after put secound value laravel give me a blank result? If i try to filtered with first value example like this : http://localhost/transport/1 everything is good but if i try to set from destionation give me a blank result. example with fail : http://localhost/transport/1/Германия
Here is my Controller
class TransportController extends Controller
{
public function filtermethod($method){
$data['ads'] = db::table('ads')->where('method', $method)->get();
return view('transport', $data );
}
public function regionfrom($from){
$data['ads'] = db::table('ads')->where('from', $from)->get();
return view('transport', $data );
}
Here is my routes :
Route::get('transport/{method}', 'TransportController#filtermethod');
Route::get('transport/{method}/{from}', 'TransportController#regionfrom');
Your second route should be giving your controller 2 variables.
public function regionfrom($method, $from)
Is what your route your having problems with is calling, do the logic you like in there.
If you would like to filter twice, try this:
$data = DB::table('ads')-where('method', $method)->where('region', $region)->get();

Routes parameters are interchangeable

I have in my View:
Edit</td>
But when I put 1-50 in my $property->user_id parameter, it leads to a property.
I have Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit'); in my web.php file.
Route File:
Route::get('/properties/', 'PropertyController#index');
Route::get('/properties/{property}', 'PropertyController#show');
Route::get('/agents/{agent}', 'AgentController#index');
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
Route::post('/agents/{agent}', 'AgentController#update')->name('agent.property.update');
This is my Controller code:
public function edit($id)
{
$property = Property::find($id);
return view('agents.edit', compact('property'));
}
I don't understand this behavior in Laravel, it's not what I intend and I just want to make the route work correctly.
Laravel gives both agent_id and property_id to the controller as parameter. You are using agent_id only and assuming it as property id.
public function edit($agent_id, $property_id)
{
$property = Property::find($property_id);
return view('agents.edit', compact('property'));
}
Based on your route file, I think you should put this route:
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
above this one:
Route::get('/agents/{agent}', 'AgentController#index');

Laravel Model wrong return

I have this function in one of my controllers:
public function getApplicationFiles(Contract $contract) {
dd($contract->get());
}
The parameter is defind in the url. So my route is
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles');
My problem is that the function getApplicationFiles displays all entries from the Contract type and not only the Object with the given id?
What am I doing wrong?
I would go with the following, make your route a named route like this:
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles')->name('contract.files');
Then in your view use it like this:
Files
This should give you the expected result.
u don't need to pass all row u need to pass only one row try like this(if you are paassing only contract id in request)
public function getApplicationFiles(Contract $contract) {
//dd($contract);
return response()->json($contract); // if you pass json
return view('show',compact($contract); /// if you view
}

how to get the current id of show function in laravel

in my controller in my show function in laravel i want the get the id that shows in browser show when i browse it it shows like this
http://localhost:8000/admin/invoices/1
i want to get that "1" and use it in show controller like below
public function show(Invoice $invoice)
{
$clients = Invoice::with('user','products')->get();
$invoice_id = 1;
$invoices = Invoice::with('products')->where('id', '=', $invoice_id)->firstOrFail();
return view('admin.invoices.show', compact('invoice','invoices'),compact('clients'));
}
and put it instead of $invoice_id so when every my client visit this page only sees the related invoice products . thanks you for help
If you're actually getting an instance of Invoice passed to your show method then it likely means you have Route-Model Binding set up for your project. Laravel is looking at the defined route and working out that the ID part (1) should map to an instance of Invoice and is doing the work to grab the record from the database for you.
The Invoice object passed through should refer to an item in your database with the ID of 1, so to get the ID that was mapped in the route you can simply just do:
public function show(Invoice $invoice)
{
echo $invoice->id; // This should be 1
Laravel supports route model binding out of the box these days, but in earlier versions you had to set it up in app/Providers/RouteServiceProvider.php. If you don't want it, try replacing your show method signature with this:
public function show($id)
{
echo $id; // Should be 1
By removing the type-hint you're simply expecting the value that was given in the route parameter and Laravel won't try to resolve it out of the database for you.
Simple way you may try this.
//Define query string in route
Route::get('admin/invoice/{id}','ControllerName#show')
//Get `id` in show function
public function show(Invoice $invoice,$id)
{
$invoice_id = $id;
}
Try using $invoiceId
public function show(Invoice $invoice, $invoiceId)
{
$clients = Invoice::with('user','products')->get();
$invoices = Invoice::with('products')->findOrFail($invoiceId);
return view('admin.invoices.show', compact('invoice','invoices'),compact('clients'));
}
do this if you want to get the url segment in controller.
$invoice_id = request()->segment(3);
if you want this in view
{{ Request::segment(3) }}
Goodluck!
Usually happens when giving a route name different from the controller name
Example:
Route::resource('xyzs', 'AbcController');
Expected:
Route::resource('abcs', 'AbcController');

Fat Free framework dynamic routing does not work

I am trying to get variable from URL to pass to the Controller
The URL looks like this http://example.org/MyCategory or http://example.org/MyCategory-1
My DB contains table for all categories that includes category_slug column
The route is GET /#category_slug = MainController->CategorySlug
My main controller has method that supposed to handle this, which should invoke method getBySlug() from the Categories model
Main Controller
function CategorySlug($category_slug){
$categories = new Categories($this->db);
$cat = $categories->getBySlug($category_slug);
$this->f3->set('categories',$cat);
echo \Template::instance()->render('index.html');
}
Categories Model
public function getBySlug($category_slug)
{
$this->load(array('category_slug=?', $category_slug));
return $this->query;
}
As you see from the code above, I am passing variable $category_slug, but have an error
#AlexB, what error are you getting?
However, you need to pick up the variable from the URL before you pass it to your methods;
$category_slug = $this->f3->get('PARAMS.category_slug')

Categories