Input value is being null laravel 5.4 - laravel

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

Related

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

Laravel $request with variable returns null

I am writing an update methoda for my api. I neede to update requested fields.
So ı am trying to get only requested fields and update them. However, the code below returns me null even thoug I cast $fill to string.
foreach ($fillableFields as $fill){
$customerId->$fill = $request->get("$fill") ;
edit- 1
My code is like below ;
$fillableFields = array('lastname','firstname');
when I dd($fill) it returns me null .
You forgot to call the save() method to update your object :
foreach ($fillableFields as $fill){
$customerId->$fill = $request->get($fill);
$customerId->save();
}
Your method should be.
public function yourMethod(Request $request)
{
try {
foreach ($fillableFields as $fill){
$customerId->$fill = $request->get($fill) ;
}
}
} catch (\Exception $ex) {
}
}
Best way to get request parameters is provided at laravel's official documentation as below.
You can get request parameter based on your specific need.
By using below statment you can get only those perameter you have specified in only attribute as array eg : username, password.
$input = $request->only(['username', 'password']);
If you wantto get all fields excluding particular field than you can define as below with except parameter. eg: by using below it will allow to get all the fields except credit_card.
$input = $request->except('credit_card');
For more information on request parameter please refer official doc url : https://laravel.com/docs/5.4/requests

Routing with parameters - Laravel

I'm trying to craft a route for a controller that will submit some data to a database. My URL is as follows:
http://example.co.uk/posts/5/edit?type=job
I've tried
Route::post('/posts/{id}/edit?type={role}', 'PostsContoller#store');
but am unsure if this will fly?
Don't add parameters in your route:
Route::post('/posts/{id}/edit', 'PostsContoller#store');
In your controller, just check if parameter exist:
$type = Input::has('type') ? Input::get('type') : null;
Don't worry about HTTP verb, as Input access for all verbs (POST,GET,PUT,DELETE...).
Edit
As pointed out by #Antoine, you can simply specify the default value in the get method
$type = Input::get('type', null);
I don't think that this is the right way to do it.
First way
If you change your route to
Route::post('/posts/{id}/edit/{role?}', 'PostsContoller#store');
You will then call the URL: GET posts/42/edit/job.
your store function in PostsController will be:
public function store($id, $role = null)
{
// some code
}
Second way
You can use another route like:
Route::post('/posts/{id}/edit', 'PostsContoller#store');
You will then call the URL: GET posts/42/edit?type=job
And you can get the type in your store function in PostsController:
public function store($id)
{
// $role will be null if type is not in the URL
$role = Input::get('type', null);
// additional code
}
I would personally go for the second way.

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

Codeigniter URI Routing not working till parameter

In my router.php file I added this code.
$route['mission'] = "content/index/mission";
Here as you know content is controller, index is function and mission is parameter to that function.
But when i check it in my browser, it takes me to content/index .
In other words, it is not passing required parameter to index function.
Make sure your recieving the parameters through the function parameters and not using uri segments.
Controller:
// This is incorrect, and will not work
public function index()
{
$param = $this->uri->segment(3); // This wont work
}
// This is correct and will work.
public function index($param = null) // use null to prevent "undefined var error"
{
if($param != null)
{
// The param was passed and do your stuff here
}
}

Resources