How can I get separate value of the array item in the codeigniter - codeigniter-2

I have a Controller in Codeigniter which has given sample of code
$data = array();
if ($query = $this->product_model -> get_individual_records())
{
$data['records'] = $query;
}
here $data store user_id and market_id fetch from the database
what I want to do is I want to store the user_id stored in the $data array to a separate variable like
$uid = the value that that is stored in the array name $data[]
How can i get separate value of the array item in the codeigniter

Assuming you are getting the records through results() method, you can do like
$data['uid'] = $query-> uid;

Related

how to display name from database in laravel

i want to display data from my database
controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$name = User::select("NAME")->where("id", $id)->get();
$pdf = PDF::loadView('generatePDF', ['name'=>$name]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{name}}</h2>
result
[{"NAME":"name_from_database"}]
can i display without [{"name":}], so just the value of data (name_from_database) ?
Simple use find like this
$name = User::find($id)->name;
Or direct from auth
$name = \Auth::user()->name;
To get logged in user id you can use \Auth::id()
you can use:
$user_id = User::findOrFail($id)->first();
$name=$user_id->name;
test the laravel log file:
\Log::info($name);
Use first() instead of get(), because get() will return an array and first() will return the object.
DRY Code line no.1 and no.2. simple use:
$name = auth()->user()->name;
to get the name of the currently authenticated user.
Send $name to you view is fine
$pdf = PDF::loadView('generatePDF', ['name' => $name]);
Correct your code in blade file
{{ name }} -> {{ $name }}
I hope this might help you. :)
Hello Aldi Rostiawan,
$nameGet = User::select("NAME")->where("id", $id)->get();
$nameFirst = User::select("NAME")->where("id", $id)->first();
Here in both line of code the difference is only Get and First.
Get method returns an Array of objects. So for example if the query apply on many records then the code will give you many objects in an array. Or if query will be true for only one record then also it will return an array with one object.
If you know that id is primary key of he table then only one record will be fetched then you can use First function instead if Get method.
First function always return an Object. In case if query apply on many records then also first method will return you only first record as an Object.
And it seems that you have required an Object only.
You should try this:
Controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$rsltUser = User::where("id", $id)->first();
$pdf = PDF::loadView('generatePDF', ['rsltUser'=>$rsltUser]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{$rsltUser->name}}</h2>
use VALUE() to display name only.
in your case:
<h2>{{$rsltUser->value('name')}}</h2>
your name variable is an array containing a single object with a NAME attribute .. so to diplay that value, you should change your script to
<h2>{{name[0]['NAME']}}</h2>

How to insert in Laravel?

I have output object $product after inserting data into table Product:
$product = Product::create($data);
In model Product I have:
public function images()
{
return $this->hasMany("App\ProductImage");
}
The I try to add images to table product_images:
$product->images()->save(new ProductImage(UploadFiles::$files));
Relationship beetwen tables is product.id = product_images.
So, after inserting I get message:
General error: 1364 Field 'image' doesn't have a default value (SQL: insert into `product_images` (`product_id`) values (21))
OK, firstly let's deal with saving one image:
Assuming you have a product_images table with id, image, product_id, you would attach an image like so:
$image = new ProductImage(['image' => 'myImage.jpg']);
$product->images()->save($image);
Now it looks like you have an array, so you can't save that directly, you need to put it in a for loop. I don't know the format of your UploadFiles::$files, but I'll assume it's just a list of file names to save:
$files = UploadFiles::$files;
foreach($files as $file){
$image = new ProductImage(['image' => $file]);
$product->images()->save($image);
}
Alternatively, you could create the whole thing as an array and use saveMany, which would be better:
$files = UploadFiles::$files;
$productImages = [];
foreach($files as $image){
$productImages[] = new ProductImage(['image' => $image]);
}
$product->images()->saveMany($productImages);

Can it possible to save the array as a record in table using eloquent in laravel 5.3?

I am using eloquent model and there is json request which contains array and i am prossessing it using foreach like folloeing:
public function save(Request $request){
$i = 0;
$flag = 1;
foreach ($request->all() as $record) {
if($flag){
$flag = 0;
continue;
}
else{
$user = \App\barcodedb::create($record);
}
}
}
is it the correct way to save the record from array?
Try like this
$request_data = $request->all();
$data = json_decode($request_data['data']);
Store array in $data and use it
There is no need for a foreach loop. After the JSON is converted to an array, you can just parse this array as the only argument in the insert method, like this:
App\barcodedb\insert($data);
Laravel will automatically recognize such an array as an array containing multiple new records.
See this answer: https://stackoverflow.com/a/13595393

how to put and retrieve laravel collection object on Session

I know that we can insert array in Session as Session::push('person.name','torres') but how to keep laravel collection object such as $product = Product::all();,as like Session::put('product',$product);.Ho wto achieve this?
You can put any data inside the session, including objects. Seeing as a Collection is just an object, you can do the same.
For example:
$products = Product::all();
Session::put('products', $products);
dd(Session::get('products'));
Should echo out the collection.
You should convert it to a plain array, then convert them back to models:
Storing in the session
$products = Product::all()->map(function ($product) {
return $product->getAttributes();
})->all();
Session::put('products', $products);
Retrieving from the session
$products = Product::hydrate(Session::get('products'));
You can see an example of this method here.

Getting an array of blocks with the same id magento

$id = 'unique_id_block';
$cmsBlockModel = Mage::getResourceModel('cms/block');
$block = Mage::getModel('cms/block');
$cmsBlockModel->load($block, $id);
I have the code above to return an a block object with the id called unique_id_block .
How is this possible to this to return an array of blocks because I may have more than 1 block with the same ID but with different store views.
You can get all blocks with the same identifier using a collection...
$id = 'unique_id_block';
$blockCollection = Mage::getModel('cms/block')->getCollection()
->addFieldToFilter('identifier', $id);
You can then iterate over this collection as you would do an array:
foreach ($blockCollection as $block) {
//...
}
Since your question asks to have these as an array though, you can also convert the collection to an array and grab its items as in the following:
$id = 'unique_id_block';
$blockCollection = Mage::getModel('cms/block')->getCollection()
->addFieldToFilter('identifier', $id)
->toArray();
$blocks = $blockCollection['items'];
But unless there is a good reason for this, i would stick with the first example.

Resources