codeigniter url parameter interchange - codeigniter

my current url is http://localhost/ci/test_test/test/12/abc
12 is my id and abc is the value which are to be passed in the function test
now i want that my url would look like this http://localhost/ci/test_test/test/id/12/val/abc
function may know that after the id keyword its the id and after the value keyword its the value
public function test($id="",$code=""){
$data['id']=$id;
$data['code']=$code;
$this->load->view('welcome_message',$data);
}

In your routes.php , set your url like this :
$route['test_test/test/id/(:num)/val/(:any)'] = "path/to/your_controller/$1/$2";
and your controller :
public function your_controller($id="",$val){
//...
}

Related

How to pass a boolean param in laravel through url?

I need to pass a param like "link/page?url=true" or "link/page?url=false"
How can I set it in my route file?
Route::get('/link/page', 'TestController#index')->defaults('_config', [
'view' => 'test::home.page.index'
])->name('test.home.page.index');
If the param is going to be a query parameter, then there is no need to define it in the Route file. In the controller you can fetch the param as:
$request->query('url');
But if you really want the parameter to part of the URL itself then you can define it like this:
Route::get('/link/page/{url}', 'TestController#index')
and to fetch it in controller you have to do this:
TestController {
public function index($url){
//put your logic.
}
}

Get Id Field From Eloquent Laravel

I want get id value from eloquent laravel and passing to variable to insert process. this is my eloquent to get data from siswa table
$siswa = Siswa::where('nis', $row['nis'])->first();
but when accessing id value like $siswa->id I'm getting error
enter image description here
Use this code
if($siswa){
$id=$siswa->id;
}
$siswa variable can be null. Look at its contents with the dd($siswa) command.
To escape the error you can use it like this:
if($siswa)
{
// $siswa not null.
}
You have two ways to fix this.
1/ You can use ->firstOrFail() to display a 404 page in case of your data is not found :
$siswa = Siswa::where('nis', $row['nis'])->firstOrFail();
// if $siswa is NULL, then it will display a 404 page, else it will continue
2/ You can add a simple condition to continue with your data :
$siswa = Siswa::where('nis', $row['nis'])->first();
if ($siswa) {
// then continue
}
First you can $id pass on your public function like
public function show($id)
{
$siswa = Siswa::where('id', $id)->first();
}
You can pass your id on your route

Input value is being null laravel 5.4

I am using following parameter value to be posted into controller but it shows null
Route::get('JobApplication/{JobId}','JobApplicationController#view');
Route::post('application/','JobApplicationController#Post');
In view method of JobApplicationController value is null when I try to get it as:
$JobId = Input::get('JobId') ;
$JobId= $request->JobId; or view($JobId){echo $JobId}
Try this:
public function Post($JobId) {
return $JobID;
}
You have set JobID as route segment, so you can access it directly as a parameter to the method.
<?php
class JobApplicationController
{
public function view($jobId)
{
dd($jobId);
}
}
The code $JobId = Input::get('JobId'); that you are trying is used in situations where JobId is sent as a query parameter, such as in url https://example.com/JobApplication?JobId=12321

Can i use Model or Helper function in routes.php [codeigniter]

I wants to set database values to the URL.
Actually i have ID of product so that i wants to show its Name by fetching from database.
Currently i am passing integer ID through URL so that i wants a name of that ID related Product.
My current URL is like : http://example.com/controller/method/1
I wants URL like : http://example.com/controller/method/product-name
i guess u are using id as parameter in controller method.
class example extends CI_Controller {
public function method ( $product_name )
{ echo $product_name; }
}
this will give u product_name as last field in controller as like controller/method/product_name..
You can get the last bit in the url as an argument to the method of your controller class.
class myClass extends CI_Controller {
public function myMethod ( $product )
{ echo $product; }
}
This will result in url : http://example.com/myClass/myMethod/anything_goes_here
And in your controller method myMethod the argument $product will be the last part of the above url anything_goes_here
For this example, the echo in the myMethod will produce result anything_goes_here
This doesn't need rerouting.
If you are getting the products based on names then this solution will do, but I don't think this will be good in real life situation. For example what if you have more then one product with the same name ?
If you still want to use product name, then I think the following approach is better. Where you will use get request uri.
class myClass extends CI_Controller {
public function myMethod ( $product )
{ echo $this->input->get ( 'id' ); echo $product; }
}
And use the url like this : http://example.com/myClass/myMethod/anything_goes_here?id=123
In the above url, the ?id=123 is the get uri. This way you can have both the product name and the id of the product in the url.
Hope it helps.

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

Resources