[Laravel][Routing] How to pass param to controller respective with route parameter - laravel-5

I met a problem, although i searched many places but could not find the answer. So, i ask here for a support.
I want to pass param into controller function that respective with param in route.
Please see below example:
I want these routes to Check controller , function getByTime($time)
Route::get('/hourly', 'Check#getByTime');
Route::get('/daily', 'Check#getByTime');
Route::get('/weekly', 'Check#getByTime');
class Check {
function getByTime($time) {}
}
Example conditions:
hourly: $time=1
daily: $time=24
weekly: $time = 24*7
I mean like this:
Route::get('/hourly', 'Check#getByTime(1)');
Route::get('/daily', 'Check#getByTime(24)');
Route::get('/weekly', 'Check#getByTime(168)');
Please help me to resolve it.
Sorry for my bad explanation, and thanks for your help.

Not sure if I have understood your exact problem, but this will do a trick
Route::get('{slug}', function ($slug){
if ($slug == "hourly") {
App::call('App\Http\Controllers\Check#getByTime', [1]);
} else if ($slug == "daily") {
App::call('App\Http\Controllers\Check#getByTime', [24]);
} else if ($slug == "weekly") {
App::call('App\Http\Controllers\Check#getByTime', [168]);
}
});
But you have add this at top of all routes.

This is so simple,If you check laravel document you will find solution easily.here is the link.
By the way you can do it simply like below
Route::get('/hourly/{hour}', 'Check#getByTime');
Route::get('/daily/{day}', 'Check#getByTime');
Route::get('/weekly/{week}', 'Check#getByTime');
class Check {
function getByTime($time) {}
}
and pass parameter in url like
http://yourhost/hourly/1
http://yourhost/daily/24
http://yourhost/weekly/168
As per your requirement you could try this:
Route::get('/hourly', 'Check#getByTime')->defaults('hour', 1);

Related

Request get method route handling

I am using laravel 5 . I want to set route like
http://localhost:8000/website/product-search-data?term=k
I also tried with
Route::get('/website/product-search-data/*', 'websiteController#searchProductList');
public function searchProductList($term)
{
dd($term);
}
But its providing me Sorry, the page you are looking for could not be found. How can I handle and get parameer value ?
Maybe can try this instead:
Route::get('/website/product-search-data/{term}', 'websiteController#searchProductList');
public function searchProductList($term) {
dd($term);
}

How to get a value in controller from get method and return it back to another view?

The problem looks basic but it is really painful!
I'm using get method and getting value in controller and I want the same value to return in another view.
How can I do that???
Please help!!!
This is my function from controller:
public function guest(){
if (Input::get('Cash On Delivery')){
$get = Input::get('Cash On Delivery');
return Redirect::to('guest/guestview/'.$get);
}
Well, with regards to your answer, using $_REQUEST directly is not Laravel's way of doing things :(
I believe this is better
public function guest(Request $request)
{
if ($request->payment_method == ('Cash On Delivery'))
{
return view('guest/guestview', ['guest'=>$request->payment_method]);
}
}
Ok Guys, I figured it out,
Just do this below.
public function guest(Request $request){
if ($request->payment_method == ('Cash On Delivery')){
$get = $_REQUEST['payment_method'];
return view('guest/guestview', compact('get'));
}

Laravel 5 How to follow DRY in my simple controller logic?

I have some simple logic in my Controller. But I often use it in other method. Here is an example.
In my controller.
public function method1()
{
if(isset(Auth::user()->showroom->name)){
$showroomName = Auth::user()->showroom->name;
}else{
$showroomName = "Belum Ada Izin";
}
return view('method1view', compact('showroomName'));
}
public function method2()
{
if(isset(Auth::user()->showroom->name)){
$showroomName = Auth::user()->showroom->name;
}else{
$showroomName = "Belum Ada Izin";
}
return view('method2view', compact('showroomName'));
}
... so on
How I can follow DRY principle in my case?
Any help will be appreciated.
Thanks in advance.
For cleaner and shorter syntax, use data_get helper:
data_get(Auth::user(), 'showroom.name', 'your default value');
This in most cases comes handy, but not always is the best way.
However, in your example it seems, that maybe in fact you'd like to simply share this variable across your views - use share or a View Composer described in the docs.
In your Showroom model you could include the following function:
public function getNameAttribute($value)
{
return isset($value) ? $value : 'Belum Ada Izin';
}
That's assuming you want that behavior everywhere showroom->name is used. If not, make the function getNameWithDefaultAttribute, check $this->name in the body, and reference it in the controller like Auth::user()->showroom->name_with_default

Missing argument 1 in laravel controller

In routes.php I have following route
Route:: get('/crm/hotel/occupant/{id}', array('uses'=>'OccupantController#occupant','as'=>'crm.hotel.occupant'));
for the above route ... if i put the controller like this it's work...but if i remove the $room_id in model calling like
$hotel = new Occupant();
.. i got an error missing argument 1 ....
public function occupant($room_id)
{
$hotel = new Occupant($room_id);
// manage page
return $hotel->occupant($room_id);
}
how to solve it ...
You can make {id} optional.
It is achieved by this:
Route:: get('/crm/hotel/occupant/{id?}', array('uses'=>'OccupantController#occupant', 'as'=>'crm.hotel.occupant'));
as explained by #vinweb you have to add a question mark ? to your id parameter,
Route:: get('/crm/hotel/occupant/{id?}', array('uses'=>'OccupantController#occupant','as'=>'crm.hotel.occupant'));
but you also have to set your variable to a default value (example taken from the official doc,):
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
So, in your case it would be probably be something like this :
public function occupant($room_id = null)
{
$hotel = new Occupant($room_id);
// manage page
return $hotel->occupant($room_id);
}

pagination in codeigniter and passing values to a function do not work

Controller
public function category($id = '') {
$offset=..;
$var = $this->pm->get_items_by_category($id,$offset,$start_row);
}
But in my view I have :
<li>Mobile</li>
I think I know what you are trying to say but it's not very clear.
Your link needs to pass the limit and offset as well as the id. So do something like this
<li>Mobile</li>
Then in your controller set up your function like this.
public function category($limit, $offset, $id){
}
If you give more info I can give better answer.

Resources