recover the slug of a category linked to another category Laravel - laravel

I would like to recover the slug of 2 categories from my routes but can’t write the Controller.
My Route
Route::get('technicians/o/{occupation}/c/{city}', 'User\TechnicianController#viewoccupationcity');
My Controller
public function viewoccupationcity($slug)
{
$technicians = TechnicianResource::collection(occupation::where('slug',$slug)->firstOrFail()->technicians()
->with('city','occupation')
->latest()->get());
return $technicians;
}

Route::get('technicians/o/{occupation}/c/{city}', 'User\TechnicianController#viewoccupationcity');
Your controller will accept the parameters from your route as variables by order
public function viewoccupationcity($ocupation, $city)
{
...
}
Example:
URL: technicians/o/foo/c/bar
public function viewoccupationcity($ocupation, $city)
{
// $ocupation will be 'foo'
// $city will be 'bar
}

Ok, you would need to retrieve 2 variables as that is what you are passing
public function viewoccupationcity($occupation, $city)
If you want the whole slug to do another search then you would use the $request object. So like so
public function viewoccupationcity(Request $request, $occupation, $city){ // You also need to include the Request decleration
$slug = $request->path();
$technicians = TechnicianResource::collection(occupation::where('slug',$slug)->firstOrFail()->technicians()
->with('city','occupation')
->latest()->get());
return $technicians;
}
EDIT: We are having to do a lot of guesswork as your question isn't very clear. I think what you are trying to achieve is probably this
public function viewoccupationcity($occupation, $city){
$technicians = TechnicianResource::collection(occupation::where('city',$city)->where('occupation',$occupation)->firstOrFail()->technicians()
->with('city','occupation')
->latest()->get());
return $technicians;
}
If you need something more then you need to give more details

Related

not working #extends() use query in laravel

i try to display data with id, but problem is when just:
public function category()
{
return view('font.category.category');
}
#extends() blade is working. I try to query use this:
function public function category($id)
{
$pCategoryById = Menu::where('id', $id)->get();
return view('font.category.category', 'pCategoryById'=>$pCategoryById]);
}
#extends() blade is not working how to solve it? url is {{url('/category/'.$result->id)}} web is: Route::get("/category/{id}",'fontController#category');.
in url
{{url('/category',$result->id)}}
in controller
$pCategoryById=Menu::find($id);
return view('font.category.category',compact('pCategoryById'));
In the url,
{{url('/category',$result->id)}}
Or you can use as like,
{{url('/')}}/{{$result->id}}
In the Controller,
$pCategoryById=Menu::where('id',$id)->get();
return view('font.category.category',compact($pCategoryById));
If #extends() is not working, then you have to check the right path which is extended.

Redirect from controller to named route with data in laravel

I'm gonna try to explain my problem:
I have a named route called 'form.index' where I show a html form.
In FormController I retrieve all form data.
After do some stuff with these data, I want to redirect to another named route 'form.matches' with some items collection.
URLS
form.index -> websiteexample/form
form.matches -> websiteexample/matches
FormController
public function match(FormularioRequest $request)
{
// Some stuffs
$list = /*Collection*/;
return redirect()->route('form.matches')->with(compact('list'));
}
public function matches()
{
// How to retrieve $list var here?
return view('form.views.matches')->with(compact('list'));
}
The problem:
When the redirects of match function occurs, I get an error "Undefined variable: list' in matches funcion.
public function match(Request $request)
{
// Operations
$list = //Data Collection;
return redirect()->route('form.matches')->with('list',$list);
}
In view
#if(Session::has('list'))
<div>
{!!Session::get('list')!!}
</div>
#endif
You can use Redirect::route() to redirect to a named route and pass an array of parameters as the second argument
Redirect::route('route.name',array('param1' => $param1,'param2' => $param2));
Hope this helps you.

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');

passing different type variables to the view from controller in laravel

i want to pass two variables called as repcounter and suppliers to the view to do two different task.
here is my function in controller,
public function admin()
{
$suppliers = SupplierData::all();
$repcounter= SalesRep::count();
return view('dashboard', compact('suppliers'));
}
this is how i send suppliers data. it worked fine. but i don't have an idea to send repcounter and suppliers at once.. each time i try i gave error undefined variable.
so how to send this two varibles to the dashboard.blade.php view?
You should try this:
public function admin()
{
$suppliers = SupplierData::all();
$repcounter= SalesRep::count();
return view('dashboard', compact('suppliers','repcounter'));
}
Replace:
return view('dashboard', compact('suppliers'));
With the following code:
return view('dashboard', compact('suppliers,'repcounter'));
You can pass multiple variables to the view by nesting the data you want in an array. See below code.
public function admin()
{
//create an empty array
$response = array();
//nest your data insde the array instead of creating variables
$response['suppliers'] = SupplierData::all();
$response['repcounter'] = SalesRep::count();
return view('dashboard', compact('response'));
}
Inside your View, you can access them as below
$response['suppliers']
$response['repcounter']

Laravel resource: How define the name of the key parameter?

When you define a resource with Route::resource('recipe', 'RecipeController');, among others, the following route is defined: /photo/{photo}/edit, and once you define all your resources you have something like this:
/recipes/{recipes}/edit
/allergens/{allergens}/edit
/ingredients/{ingredients}/edit
Because all my records use id as primary key (MongoDB), I'd like to have {id} instead, like so:
/recipes/{id}/edit
/allergens/{id}/edit
/ingredients/{id}/edit
I dug in the Router class but I don't see how to specify this.
More over when I create a form with Form::model($record) I get actions like /recipes/{recipes} because recipes is a property of $record.
How can I define the name of the key parameter to id instead of recipes, allergens, ingredients?
In order to change the param name for Route::resource, you need custom ResourceRegistrar implementation.
Here's how you can achieve that in a shortest possible way:
// AppServiceProvider (or anywhere you like)
public function register()
{
$this->app->bind('Illuminate\Routing\ResourceRegistrar', function ($app) {
// *php7* anonymous class for brevity,
// feel free to create ordinary `ResourceRegistrar` class instead
return new class($app['router']) extends \Illuminate\Routing\ResourceRegistrar
{
public function register($name, $controller, array $options = [])
{
if (str_contains($name, '/')) {
return parent::register($name, $controller, $options);
}
// ---------------------------------
// this is the part that we override
$base = array_get($options, 'param', $this->getResourceWildcard(last(explode('.', $name))));
// ---------------------------------
$defaults = $this->resourceDefaults;
foreach ($this->getResourceMethods($defaults, $options) as $m) {
$this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options);
}
}
};
});
}
Now your routes will look like:
Route::resource('users', 'UsersController', ['param' => 'some_param'])
/users/{some_param}
// default as fallback
Route::resource('users', 'UsersController')
/users/{users}
Mind that this way can't work for nested resources and thus they will be a mix of default and custom behaviour, like this:
Route::resource('users.posts', 'SomeController', ['param' => 'id'])
/users/{users}/posts/{id}
I know this is 4 year old question but for anyone who is googling; you can pass a third argument to override key naming:
Route::resource('ingredients', 'IngredientController', ['parameters' => ['ingredients' => 'id']]);
Or
Route::resource('ingredients', 'IngredientController')->parameters(['ingredients' => 'id']);
You can pass your ids to the routes you don't necessary need to change the parameters {recipes} to {id} since the parameters are just a placeholder.
So
public function edit($recipes){
// code goes hr
}
is the same as this
public function edit($id){
// code goes hr
}
for this route /recipes/{recipes}/edit

Resources