how to pass arguments to filter in laravel4.2 - laravel-4

in my project , i want to pass 'user_id' to filter to check.
this is isAdmin function:
public function isAdmin($id){
$user=User::find($id);
if($user->role==10){
return true;
}
return false;
}
and this is my route file:
Route::group(array('before'=>'admin'),function(){
Route::get('dashboard','UserController#Dashboard');
});
this is my filte that iwant give user_id ($id), and idont know how.
Route::filter('admin',function($id)
{
if(Auth::guest() or ! Auth::user()->isAdmin($id))
return Redirect::guest('user/logout');
});

For this case, you can directly check from the filter like this:
Route::filter('admin',function()
{
if(Auth::guest() or Auth::user()->role != 10)
return Redirect::guest('user/logout');
});
You do not need to pass params.
Also have a look to this link if you really want to pass params.
Link of Laravel Doc: here

Related

Route not defined when passing parameter to a route

I have a named route with a parameter which looks like this...
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
Now in one of my controller,
I have a function that calls this route like this
public function _myFunction($some_data) {
return redirect()->route('profile', [$some_data->user_id]);
}
and in my ProfileController's profile() function, I have this.
public function profile() {
return view('modules.profile.profile');
}
I've followed the documentation and some guides I found in SO, but I got the same error,
"Route [profile] not defined."
can somebody enlighten me on where I went wrong?
Here's what my routes/web.php looks like...
Route::middleware(['auth:web'])->group(function ($router) {
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
});
When calling the route, you should pass the name of the attribute along with the value (as key vaue pairs). In this case, your route is expecting user_id so your route generation should look like this:
return redirect()->route('profile', ['user_id' => $some_data->user_id]);
Read more on Generating URLs To Named Routes in Laravel.
I solved the issue, and its really my bad for not providing a more specific case information and made you guys confused.
I was using socialite and called _myFunction() inside the third party's callback..
After all, the problem was the socialite's google callback, instead of placing the return redirect()->route('profile', [$user->id]) inside _myFunction(), what I did was transfer it to the callback function.
So it looked like this now...
private $to_pass_user_id;
public function handleGoogleCallback()
{
try {
$user = Socialite::driver('google')->user();
$this->_myFunction($user);
return redirect()->route('profile', [$this->to_pass_user_id]);
} catch (Exception $e) {
dd($e->getMessage());
}
}
public function _myFunction($some_data) {
... my logic here
$this->to_pass_user_id = $some_value_from_the_logic
}

Laravel 5.4 controller function not able to use a get request parameter from ionic 3

I am try to pass a from ionic application to a laravel 5.4 application, and this parameter is an array, i have been able to pass the parameter successfully but i am being able to use the parameter to select records from the database.
Here is my ionic 3 provider function:
getMySmartQueues(data){
let params = new HttpParams();
params = params.append("sq_ids", JSON.stringify(data));
return this.http.get(this.url + 'my/smart/queues', {params: params});
}
And here is my laravel controller function:
public function getMySmartQueues(Request $request){
$ids = $request['sq_ids'];
$my_sq = SmartQueue::whereIn('id', $ids)->get();
return $my_sq;
}
And here is how i subcribe to the provider function is my page:
ionViewDidLoad() {
this.storage.get('sq_ids').then(
res => {
console.log(res);
if(res != null){
this.sq_ids= res;
console.log(this.sq_ids);
this.mService.getMySmartQueues(this.sq_ids).subscribe(
data => {
console.log(data);
}
);
}
}
);
}
But i get Server internal error. But if i have to hard code a default value for the controller function, let say like [5,6], it will return the records of this ids, but it can not returns the records of the ids sent from the ionic 3 application, will be glad if any one can help me out.
Also if i change the request to a put request i can get the records of the ids sent from the ionic application. But a get request is what i want.
if you want to use controller function with GET you need allow arguments in the Route to allow your id array.
for an example.
Route::get('your/url/{ids}', 'Controller#function')->name('mane_of_the_route');
and the controller function
public function getMySmartQueues(array $ids){
$my_sq = SmartQueue::whereIn('id', $ids)->get();
return $my_sq;
}
I have figure it out, and this is what i had to do, i think it might help someone one day, i just had to json_decode the request like so:
public function getMySmartQueues(Request $request){
$ids = $request['sq_ids'];
$my_sq = SmartQueue::whereIn('id', json_decode($ids))
->with(
'station.company'
)->get();
return $my_sq;
}

conditional nested routes in laravel

I want to check segment of particular URL on route and based on value of segment decide it to handle to another route.Somewhat like below:
Route::get('{module}/{seg}', function(){
if (is_numeric((Request::segment(3)) {
return Route::get('{module}/{seg}',Request::segment(2) . 'Controller#index');
}else{
return Route::get('{module}/{seg}',Request::segment(2).'Controller#index' . Request::segment(3));
}
});
I don't think above code works but can anyone suggest a working code for implementing above logic in laravel?
I'd suggest adding it as an optional parameter, and handle differences in the controller. Given your code, it might look like this, for instance:
// route
Route::get('{module}/{seg}/{param?}', 'Controller#index');
// controller
public function index($module, $seg, $param = null)
{
// for dynamic index methods
if (is_numeric($param)) {
$method = 'index' . $param;
return $this->{$method}();
}
// for non-numeric third-segment params, continue here as usual
}

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

getting the value of the single field output using the codeigniter active record

the following function is supposed to read the name of the given asset code from the database. but it triggers the error: "Trying to get property of non-object"
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get()->result()->row('name');
}
All I want is to have the name of the asset returned back to the controller! Your help is highly appreciated!
Use row() like,
return $this->db->get()->row()->name;
Use row() for a single row, and result() for multiple rows.
do like this, asset_types is your table name
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get('asset_types');
}
And in your controller acess it like
$result=$this->modelname->sban_name('$asset')->row();
$name=$result->name;
I think it's important to check if the record that satisfies the conditions even exists in the database. Code for the model:
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
$row = $this->db->get()->row();
if (isset($row)) {
return $row->name;
} else {
return false;
}
}
Simply call the function from the controller like so:
$response = $this->model_name->sban_name($asset)
Try this code of block , I already checked and works fine:
function sban_name($asset)
{
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code', $asset);
return $this->db->get()->row()->name;
}

Resources