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

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

Related

How to pass multiple parameters to route

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)
{
....
}

How to return an int variable from controller to view in Laravel?

I have created a global variable in my CartController for the quantity of an item, here is the declaration:
class CartController extends Controller
{
// defining global quantity variable
private $quantity = 1;
Then in my index() function on the same controller, where I return the view, I pass through the quantity variable like so:
public function index()
{
$this->quantity;
return view('cart.cart', ['quantity' => $this]);
}
But when I call it in the cart.blade.php file like this:
<div class="display-quantity">
{{-- Shows Quantity --}}
<span class="item-quantity" id="item-quantity">{{ $quantity }}</span>
</div>
It gives me the following error:
TypeError
htmlspecialchars(): Argument #1 ($string) must be of type string, App\Http\Controllers\CartController given (View: /Users/rosscurrie/mobile-mastery-latest/resources/views/cart/cart.blade.php)
I think I need to convert it to a string but I need to work with it as an integer so how do I get around this? Thanks!
It look like you're passing in $this as the quantity.
Try changing your controller's index() code to something like:
public function index()
{
$myQuantity = $this->quantity;
return view('cart.cart', ['quantity' => $myQuantity]);
}
It looks like the confusion here is with the -> operator which in php is used to call on object's method or access an object's property the same way . works in Javascript. This statement $this->quantity; accesses the quantity property of $this, so to use it - you need to assign it to a variable (or use it directly). This would have also worked:
public function index()
{
return view('cart.cart', ['quantity' => $this->quantity]);
}
You can always do things like dd($this->quantity); to ensure you are working with the correct information.

recover the slug of a category linked to another category 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

Trying to get property 'post_titile' of non-object

This is My Controller.
public function show(Posts $posts)
{
$page = Posts::find($posts->id);
//dd($page);
return view('web_views.index',['page' => $page]);
}
This is my view page
<h4>{{$page->post_titile}}</h4>
It's better to use Route-Model Binding I think.
Your route (if you are using resource route, it's already done):
Route::get('posts/{post}', 'PostController#show); // domain.tld/posts/1
Your method should look like this:
public function show(Post $post)
{
return view('web_views.index',compact('post'));
}
In your view:
<h4>{{ $post->post_titile }}</h4>
May be $posts->id you are passing, has no result in database so you need to check it by using if-statement
public function show(Posts $posts)
{
$page = Posts::find($posts->id);
if($page == null){
$page = ['post_title' => 'Not Available'];
}
return view('web_views.index',['page' => $page]);
}
I believe this error is because of not finding data for an ID into database. So if as per above script will pass the fetched data to view otherwise it will push an item into $page array.

Laravel 5.5 - Show specific category from API response

I am returning an API response inside a Categories controller in Laravel 5.5 like this...
public function get(Request $request) {
$categories = Category::all();
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}
Now I am trying to also have the option to return a specific category, how can I do this as I am already using the get request in this controller?
Do I need to create a new route or can I modify this one to return a specific category only if an ID is supplied, if not then it returns all?
Better case is to create a new route, but you can also change the current one to retrieve all models if the parameter is not supplied. You first gotta choose which approach you will be using. For splitting it into multiple calls you can see Resource controllers and for using one method you can follow Optional Route Parameters
It will be much cleaner if you will create another route. For example
/categories --> That you have
/categories/{id} -> this you need to create
And then add method at same controller
public function show($id) {
$categories = Category::find($id);
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}
But if you still want to do it at one route you can try something like this:
/categories -> will list all categories
/categories?id=2 -> will give you category of ID 2
Try this:
public function get(Request $request) {
$id = $request->get('id');
$categories = $id ? Category::find($id) : Category::all();
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}

Resources