I want to make a table for the reporting of some data.
So I have a query in my model called:
query($statusId, $regionId);
so the value depends on statusID and regionID.
___________|____1___|____2____|____3___|
|_____1_____|query(1,1)|query(1,2)|query(1,3)|
|_____2_____|query(2,1)|query(2,2)|query(2,3)|
|_____3_____|query(3,1)|query(3,2)|query(3,3)|
But how can I do this?
Here I write the query into the $data array in the controller. But i dont know how to define the $statusId and $regionId
$num_meas_region= $this->MMeasure->get_measure_number_and_region($statusId, $regionId);
$data["num_meas_region"] = $num_meas_region;
Related
So I am quite new to Laravel, and I have a situation, where I am trying to gather data from a pivot table (contains 2 foreign keys only) in order to retrieve data from other tables.
Before everything, I'd like to note, that word "campaign" is the same as "box". Simply it differs in database and front.
I have multiple boxes, that contains specific gifts.
I have set the URL of the box to be something as such: http://127.0.0.1:8000/box/1
http://127.0.0.1:8000/box/2
etc..
I have done so, by simply using a button with the {id}:
View the box
My plan is, to print out only that specific boxes gifts (right now, all boxes print out all gifts).
I have tried to use the ->where option within my function, although, it seems that I can't try equaling to the campaigns ID.
Incorrect code:
function box(){
$data = array(
'list'=>DB::table('campaigns_gifts')
->join('gift_items', 'gift_items.id', '=', 'campaigns_gifts.gift_foreignK')
->select('gift_items.*')
->where($campaign_foreignK = '{id}')
->get()
);
return view('DBqueries.boxView', $data);
}
My question is, how can I specifically return data, that only belongs to that specific box, since I am not able to use mysql where option.
For reference, these are the database tables:
Basically, I would need to match my URL's id with campaign_foreignK
Thank you in advance.
First of all, yout need to start to use Laravel Eloquent Models.
But doing by your way (the hardest):
You need to create a route in web or api, something like that:
Route::get('/box/{id}', [BoxController::class, 'view']);
Then you need to put this function on your controller:
function view($id){
/**
* You can do it by 2 ways:
* 1 - Do a where in the result of DB query (the bad way)
*/
$list = DB::table('campaigns_gifts')
->join('gift_items', 'gift_items.id', '=', 'campaigns_gifts.gift_foreignK')
->select('gift_items.*')
->where($campaign_foreignK = '{id}')
->get();
$list = (array)collect($list)->where('abc', 123);
/**
* Or the second way (the best is to use the Eloquent, but using DB the following is the best)
* 1 - Get the relations:
* Is git_items id the key for gift_foreignK ? i'm supposing that is it! so....
*/
$giftsIds = array_values((array)DB::select("select * from campaigns_gifts where campaign_foreignK = $id"));
$giftsIdsString = implode($giftsIds, ',');
$list = (array)DB::select("select * from gift_items where id in ($giftsIdsString)");
return view('DBqueries.boxView', ['list' => $list]);
}
I tried to use the following code to retrieve an entire subscription object from my subscription table using the testId column.
testId column is delclared as "json" type but in reality, the content of this column is an array with a single string as follow :
["51602a95-73d1-4c24-b3b3-eee288b427e4"]
I tried to get the subscription object with this code but ot doesn't work. How can i adpat this piece of code to get the subscription object by searching the value of testId into the array ?
function getSubscriptionByTestId($testId) {
$subscription = Subscription::where('testId', $testId)->first();
return $subscription;
}
If all the values are like ["51602a95-73d1-4c24-b3b3-eee288b427e4"] depending on your database engine you could use:
$subscription = Subscription::where('testId','like', "%$testId%")->first();
You can try the following, using a raw query. I'm not yet sure how to handle this with Eloquent, but will investigate further.
return DB::select(
"SELECT * FROM subscription
WHERE testId->\"$[0]\" = \"{$testId}\""
);
i am using Fat-Free Framework to do rapid prototyping of my application. Now, whenever I try to load some data from database, i can use the load() function within the SQL\Mapper but it returned all of the column.
I found SELECT() function but it does not returning any data.
$this->load(['myId=?',$id]) will return the data along with the other columns
$this->select('name',['myId=?',$id]) should return the data from name column but i got nothing.
$this->db->exec('SELECT name FROM persons WHERE myId=?',$id) will return the the data from name column.
what is the proper way of using SELECT() from Fat-Free framework? my goal is to only retrieve single data from name column only.
The right way to do it is like this:
$table = new DB\SQL\Mapper($db, 'persons');
// assign to $results
$results = $table->load(array('myId=?', $id));
foreach($results => $row){
echo $row->name;
}
As described here: https://fatfreeframework.com/3.6/databases#SeekandYouShallFind
I want to select only year from records ane make from this data assoc array
$wynik = $model_post::model() -> findAllBySql('SELECT DISTINCT YEAR(create_time) as rok FROM tbl_post');
for($i;$i<count($wynik);$i++)
{
$rok[$wynik[$i]->rok]=$wynik[$i]->rok;
}
but I got the exception
_Property "Post.rok" is not defined._
How can I make it correct?
If I remember correctly this should work in Yii1 as well: define a variable public $rok; in your model_post class, and it will automatically be populated when you use that query.
I have query that runs in the controller:
$data['query'] = $this->Member->select_sql($id);
$this->load->view('myform');
and then outputs data in the view:
foreach ($query->result() as $row):
echo $row->post_title;
echo $row->post_user_id;
endforeach;
So this outputs me a list of posts made by a user. Now I would like to run one more query that would for each post loop through my user table and output user information next to each post. (I dont want to select data from a view or joint those 2 tables at this time in MySQL)
Any ideas?
Although it is not a good practice, the "cleanest" approach would be as follows:
Grab the CI instance in the View
Load the model containing your desired data extraction query functions
Run the function from the model in the view
So, in the view:
$CI =& get_instance();
$CI->load->model('modelname');
$result = $CI->modelname->functionname();
var_dump($result);
Tested and working.
Inject the database adapter or appropriate table object into the View.
From your code above, I'd assume this would be
$data['userModel'] = $this->User;
Then use it from there to run your query, e.g.
$user = $userModel->select_sql($row->post_user_id);
Simply
<?php
$qryd='select * from '.$tbname.'';
$queryd = $this->db->query($qryd);
$resultset = $queryd->result_array();
?>