How do i fetch data inside a controller using laravel - php

I just started learning laravel.
I don't know how can I fetch data inside a controller,
I want to use that data inside a controller to make more rules.
//Get Data From Database
$db = DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->get();
$name = $db['donor_name'];
$email_id = $db['email_id'];
DonorDetail is my model to connect to my database.
I tried this code, but getting undefined error.
I'm trying to select only 1 row using the unique donation_id and use data of other columns of data inside my controller.

get returns a collection. You should use first to get the first result from your query:
DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->first();
If no results were found, null will be returned, so you should also pay attention to that.
You only provided us with a small portion of your code, but it seems like you're doing a lot of things in a non-Laravel way. It's only natural because, as you mentioned, you're just starting with Laravel. My recommendation to you is to check out Laracast's free "Laravel From Scratch" course:
https://laracasts.com/series/laravel-6-from-scratch
It's by far the best resource for developers taking their first steps in Laravel (and it also has good resources for experienced developers).
Good luck!

you can use dd() to see the data.
dd($db);
However, If you want to fetch only first matching data, use following
$db = DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->first();
$name = $db->donor_name;
$email_id = $db->email_id;

Related

What is a simple process to define contain with find function

Care to put light to the subject. Issue is that when I do what ever to code I find that code does not go further to retrieve data from table Sectors and table courses. I have applied find() I use CakePHP 4.x to make clear that it is latest version of CakePHP called Strawberry. I also do not understand that why does my code get affected while I apply simple code structure to run my code.
Take a look at code snippet to run a simple find query with CakePHP Strawberry.
$Sectors is not defined but I need to know a way to define and apply $Sectors variable so that I have given value for relative table structure.
Table Does not load because of a problem I do not understand yet but guys help me out I'll update my code and make use of real code that is to determine a structure for code that will work for CakePHP Strawberry.
code:
$this->loadModel('Sectors');
$this->loadModel('Courses');
$Sectors = $Sectors->find('all')->contain('Sectors', 'Courses');
pr($sectors);
$this->contain(['Sectors', 'Courses']);
$this->set(compact('sectors'));
To retrieve data from table so that I get data relation from Sectors and Courses table.
error:
Call to a member function find() on null
Answer is simple but problem is CakePHP would keep single model with multiple query but not multiple model with single query.

yii ActiveRecord always querying database

Using Yii 1 and ActiveRecord -
I'm pretty new in both.
$types = CasinoSgTypes::model();
$types->findAll();
$types->findByPk(3);
I thought Yii AR will try to search in recently received data first instead of that I've got 2 calls to the database. Probably I'm using it in a wrong way?
sure i can walk through the array of results received by the first query (findAll) manually, but I'd like to do this by means of AR.
in other words is there a way to force AR search inside already received data and only then ask the database or smth like this. how to use AR+Yii models in a right way to avoid unnecessary queries ?
When you call $types->findAll(); the data isn't saved in the AR class and as such you cannot search using it. An easier alternative to searching in the data is to use query caching:
$types->cache(3600)->findByPk(3);

best way to preventing form resubmission of codeigniter

Always confused about form resubmission . I know Header and session are the right way. But I don't know how can I proper use in codeigniter. Suppose
For single insert and update query
$this->db->query(' insert/up query');
$this->session->set_flashdata('success_message','successfully inserted');
redirect('my_contoller/home/index');
For pass $data array
$data['pass_data']="some array elements";
$this->session->set_flashdata('pass_data_from_flash_data',$data['pass_data']);
redirect('my_contoller/home/index',$data);
If above technique are right how can I pass query's data for retrieving. Suppose I have a query which return many data. Such as
$query = $this->db->query(" a query which return large data");
$data['return_large_result']=$query->result_array();
I just confused to using set_flashdata function. Is the right way?
The above method mentioned you are using is a valid web development design pattern. Codeigniter is a bit messy for this method but essentially yes it is the right way within Codeigniter.
Other frameworks such as Laravel support this feature better, allowing you to access old input via Input::old() amongst other methods.

Codeigniter with xpath and on duplicate key update

Looking for a bit of advice.
I have an update script for my website’s database, but as yet - I’ve not been able to figure out how to convert it to the Codeigniter framework ie. it’s saved as it’s own file in the home directory of my website, and executed from there.
There’s a couple of things making it difficult for me to figure out:
It uses php xpath - not difficult to see how I would convert this to a Codeigniter controller, but I’m wondering if update_batch would be a better solution for me as opposed to using a foreach loop as each row is update by just 1 index column. There’s next to no documentation available on this however…
My code utilises MySql’s On duplicate feature which as I understand is not supported in Codeigniter.
Code:
$string = 'http://mywebsiteetc.com/xml/gzip/';
$xml = simplexml_load_file("compress.zlib://$string");
foreach ($xml->xpath('//merchant') as $row)
{
$merchant_id = $row['id'];
$merchant_name = $row['name'];
mysqli_query($con, "INSERT INTO merchants (merchant_id, merchant_name) VALUES ('$merchant_id', '$merchant_name') ON DUPLICATE KEY UPDATE merchant_id = '$merchant_id', merchant_name = '$merchant_name'");
}
Can anyone steer me in the right direction as to how to implement the above into a controller/model environment - and of course, advise me of the most efficient way to do all of the above if there is one?
Cheers!
Basically you need to know more about the MVC structure of Codeigniter, you can start reading here for the controller.
In your setup:
create a file in controller app/controllers/update.php
in your update.php you can write your functions in public function index()
Parse your xml and pass it in Model (create a model for this setup)
then save/inserts it one by one or per batch , put the code in your Model
Again you have to read the documentation for a better understanding how to separate your MVC and make it a one functionality

Check Existance Of Entry In Model Within View File using Yii

This might sound kind of basic (and it probably is), but I've been trying to get this for a few hours now and either I need a break from coding or it just wont budge.
I have an active record variable which brings database entries from a model:
$variable = Model::model()->findAll();
So I have $variable available in my view file and I want to check for the existence of a specific entry within the results. I am using the primary key of an entry available in the $variable, but I can't seem to get it working.
What is the correct way to check if a given entry is contained within that variable from the view file, not the controller?
PS: I do not want to iterate through the result set, it wouldn't be very efficient for my application.
Thanks.
If I got you right then:
Indeed its better not to have such pieces of code in a view file.
If you are forced to use CActiveRecord.fin*() methods, consider using findByPk($pk). If it returns null - no such record.
Consider having your models extending PcBaseArModel as that class has 'checkExists($pk)' method that looks natural in the check you need to perform. This base class has other goodies - check its documentation.
Try this code:
$data = Post::model()->findAllBySql("select * from tbl_post where id=".$data->id);
or
$post=Post::model()->find(array(
'condition'=>'postID=:postID',
'params'=>array(':postID'=>$data->id)
));

Categories