Can someone help me convert this query so that my result set is in different format?
$sessions = new Session();
$results = $sessions->where('session_status', $status)->where('application_period_id', (int) ApplicationPeriod::all()->last()->id)->get()->pluck('speaker_id');
$speakers = Speaker::whereIn('id', $results)
->with('session.audiancesession.audiances')
->with('session.subjectsession.subjects')
->with(['session' =>
function ($query) use($status) {
$query->where('session_status', '=', $status);
}])->orderBy('last_name')->get();
This is requested via Ajax(axios)... Now this is how result is formatted:
Obj->data(array of objects)->[0]->name
->address
->session(array of objects)
->[0]->time
->fee
My issue is that my session parameter is array and there can only ever be (1) so I don't need to to be an array and I would like to have object (json) instead.
Thank you!
You might have more success if you change your client-side code to work with an array of sessions each session having its speaker, that means your original query would be like
$sessions = Sessions::with([
'speaker', 'audiancesession.audiances', 'subjectsession.subjects'
])->where('application_period_id', (int) ApplicationPeriod::orderBy('id','DESC')->first())->get();
Note the order by -> first in the ApplicationPeriod makes it so you don't have to get all application periods from the database to memory.
Then your client side should handle an array of sessions.
You can transform the above slightly using to get a similar result to what you need:
$speakers = $sessions->map(function ($session) {
$speaker = collect($session->speaker->toArray());
$speaker->put('session', collect($session->toArray())->except('speaker'));
return $speaker;
})->orderBy('last_name','DESC');
Though I wouldn't guarantee the result here as I've not tested it on your (complex looking) data.
I have a model instance for which I set the attributes from a post request using $my_instance->fill($request_json) and after saving using $my_instance->save() the instance as a record in the database, I want to receive the saved object back with all its attributes using return response()->json($my_instance). Now this works fine as long as I provide all the attributes I set in the protected $fillable = [] on the model class inside the post request body. But when I want to send only part of the attributes in the post request, what happens is that inside tinker I see the skipped attributes are set to null. This is fine. But the problem is when I return using return response()->json($my_instance) I don't see the skipped attributes and I want them to be returned even with them being null in my databse. Is there a way to instruct laravel to do so?
A possible implementation to your question COULD be to:
Allow all possible fields that are getting nulled out to be fillable in your model.
Then, use a middleware with collections to do:
$form_stuff = $request->all();
$form_stuff = collect($form_stuff);
$things_you_want = $form_stuff->only('wanted_field_1','wanted_field_2');
$things_that_should_be_null = $form_stuff->only('nulled_field_1','nulled_field_2');
$keys = array_keys($thins_that_should_be_null);
$values = array_fill(0, count($keys), null);
$new_array_of_nulled_things = array_combine($keys, $values);
var_dump($things_you_want);
var_dump($new_array_of_nulled_things);
die();
WARNING: Since there was no code posted, this is just concept mostly and has not been tested so you will have to play with it to get it work the way you want.
I want to pass two parameters in index function using codeigniter but its giving me error i don't know why its giving me error.
here is my controller
function index($one = null,$two = null)
{
$data['title'] = "Product Page ";
$this->load->view('home/header/header',$data);
$this->load->view('home/css/css');
$this->load->view('home/navbar/navbar2');
$this->load->view('products/product');
$this->load->view('home/footer/footer');
$this->load->view('home/js/js');
}
here is my routes setting
$route['product/(:any)'] = 'product/index/$1';
When i set my index function in one parameter its working fine but its not working with two parameters.
You can try with this, it will be working
$route['product/(.*)/(.*)']='product/index/$1/$2';
I have a codeigniter model function which queries a database and sends the result back to the controller, encoded as json.
the entire function is shown below:
function get_skufamily_cube($q){
$sql=("select min([Pieces]) as ProductCode from
(SELECT
[ProductCode]
,[Description]
,[Length]
,[Pieces]
,[Thickness]
,[Width]
,([width]*1000) w2
,([thickness]*1000) t2
,REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','') AS l2
,concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')) AS pc
,REPLACE([ProductCode],concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'') as grade
,CONCAT(([width]*1000),([thickness]*1000),REPLACE([ProductCode],concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'')) as options
FROM [hammerhead].[dbo].[ProductList]) as p
where Options like '%$q%' ");
$query=$this->db->query($sql);
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row['ProductCode']));
echo $row['ProductCode']; // to see database result and troubleshoot
}
$this->output->set_content_type('application/json')->set_output(json_encode($row_set));
}
}
$q is being passed successfully to the query and the output of the query in echo $row['ProductCode']; is consistent with the result I require. in this case it is 108. the database query returns a single result in a single field.
For some reason, this is no tbeing passed back tot he controller correctly.
Controller is:
$this->load->model('Sales_model');
if (isset($_POST['data'])){
$q = strtolower($_POST['data']);
$data = $this->Sales_model->get_skufamily_cube($q);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
}
In my developer tools, I can see the server response
108NULL. 108 being my echo and NULL being the json response. if I remove the echo this is simply NULL.
Laslty, I need to populate the input in my table row with the value. my view jquery syntax for this is:
$.post('get_skufamily_cubes', {data:selectedObj.value},function(result)
{
$(this).find('input[id^="cubesperbundle"]').val(result);
});
Currently nothing is populated. the html input name and id is:cubesperbundle but has the row number appended to it hence the 'input[id^="cubesperbundle"]'
Any assistance would be appreciated.
Thanks and Regards,
The mistake i see is that you are not returning anything to controller.
Model
function get_skufamily_cube($q)
{
$Query="select
min(Pieces) as ProductCode
from (SELECT
ProductCode,
Description,
Length,
Pieces,
Thickness,
Width,
(width*1000) w2,
(thickness*1000) t2,
REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','') AS l2,
concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')) AS pc,
REPLACE(ProductCode,concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'') as grade,
CONCAT((width*1000),(thickness*1000),REPLACE(ProductCode,concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'')) as options
FROM hammerhead.dbo.ProductList) as p
where Options like '%$q%'";
return $this->db->query($Query)->row();
}
Controller
function getJson(){
$this->load->model('Sales_model');
if (isset($_POST['data'])){
$q = strtolower($_POST['data']);
$viewData = $this->Sales_model->get_skufamily_cube($q);
$data_json = json_encode($viewData);
echo $data_json;
}
}
EDITS:
Change the return instruction in model function.
$result = $this->db->query($Query)->row();
return $result->ProductCode;
In my controller I have called a model that makes a mysql query to fetch information from table. It is working okay but now under the same function I want to call another model and make a query based on one of the results from the previous mysql query. (The field name which I want to get the result of is "batch") . I have tried to get the value (batch) right in my controller, pass it into the model and then tried to make the second query but it seems like the second model is not getting the value from the controller and hence its not working. Would you please kindly help me with this?
Thanks in Advance :)
Here is my Controller
function Get($id){
$this->load->model('mod_studentprofile');
$data['query']= $this->mod_studentprofile->student_get($id);
// To get the batch name
$batch= $query ['batch']; // This I get from the above query result.
$this->load->model('batchname');
$data['query1']= $this->batchname->batchname($batch);
$data['tab'] = "Student Profile";
$data['main_content']='studentprofile';
$this->load->view('includes/template',$data);
}
Here is my model number 1
function student_get($id)
{
$query=$this->db->get_where('student',array('studentid'=>$id));
return $query->row_array();
}
Here is my model number 2
function batchname($batch)
{
$query1=$this->db->get_where('batch',array('batchid'=>$batch));
return $query1->row_array();
}
Well, you're not actually assigning anything to $batch, how about this:
$batch= $data['query'];
This now passes the variable along. As a side note, you can pass it as a paramater and be done with it in a single line, dependency injection style:
$data['query1']= $this->batchname->batchname($this->mod_studentprofile->student_get($id));
Are you getting a value back from the first query?
I would log the value you are getting back from the first query in your controller.
log_message('debug', 'Batch value is '.$batch);
And also put in some debug to see what the query that is being ran.
function batchname($batch)
{
$query1=$this->db->get_where('batch',array('batchid'=>$batch));
$str = $this->db->last_query();
log_message('debug', 'Batchname Query: '.$str);
return $query1->row_array();
}