Getting an array of blocks with the same id magento - 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.

Related

How to decrement product quantity after checkout in Laravel (With Session)

Can you help me how to decrement after doing a checkout. So far this is the only idea I have but it shows an error called "Object of class Illuminate\Database\Eloquent\Collection could not be converted to number
". Thank you for the reply. Also I am using the one with session.
Note. I edit and put another image for more clearer explanation and here is the code
public function postCheckout(Request $request){
$books = Product::all();
if (!Session::has('cart')){
return view('shop.shoppingcart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$order = new Order();
$order->cart = serialize($cart);
$order->address = $request->input('address');
$order->name = $request->input('name');
Auth::user()->orders()->save($order);
$q=$cart->totalQty;
$r=$books-$q;
Product::table('books')
->where('id',$id)
->update(array('quantity' => $r));
Session::forget('cart');
return redirect("home");
}
And the error is "Object of class Illuminate\Database\Eloquent\Collection could not be converted to number"
You have this line of code:
$books = Product::all();
Here, $books is a collection.
And after a few lines, this one:
$r=$books-$q;
which of course can't execute without error because you're trying to subtract a number from a collection.
I assume you don't actually want the whole book collection, but the number of books. In that case, replace $books = Product::all(); with $books = Product::count(); and your code should work fine.
However, that line counts all the books, and I guess you only want to decrease the quantity of the purchased books. Unfortunately, that's not what you code does. For example, in this code:
Product::table('books')
->where('id',$id)
->update(array('quantity' => $r));
where does the $id variable come from? You don't set its value anywhere. What you should do is iterate every item of your cart, get the ID and quantity of each item, and perform an update query for each item.

How I can paginate DB::select(...) result and get links() method?

I have a big and difficult SQL query. It works fine for me. And I have the following code in the controller:
public function index(OpenRegDepDataReportInterface $openRegDepDataReport, Request $request): Renderable
{
$applications = $openRegDepDataReport->getReport($advertisers, $category);
return view('applications.index', compact('applications'));
}
So, the method getReport gives me a result of DB::select('<here is my big diffecult SQL>'), and, as well known it's an array.
But as you can see I'm trying to pass the result on a view. And, of course, I would like to call $applications->links() in the view, like for eloquent collection. Which is proper and faster way to do that?
doc
To display pagination at the table, you must call the select and then the pagination method.
in Controller:
$test = DB::table('users')->select('id')->paginate(10);
in View:
$test->links();
So based on the documentation if your $applications returns a Query Builder result, then just append ->paginate(10); to it.
https://laravel.com/docs/master/pagination#paginating-query-builder-results
$applications = $openRegDepDataReport->getReport($advertisers, $category)->paginate(10);
Simple answer, use paginate() method:
$basicQuery = DB::select(DB::raw("<here is the big diffcult SQL query>"));
However, paginate() works only on collections, and since you have an array of objects, you need to turn it into a collection first, using the forPage() method:
The forPage method returns a new collection containing the items that would be present on a given page number. The method accepts the page number as its first argument and the number of items to show per page as its second argument:
$collection = collect($basicQuery);
$chunk = $collection->forPage(2, 3);
$chunk->all();
Complicated answer: build a paginator instance yourself:
$perPage = 10;
$page = $request->input("page", 1);
$skip = $page * $perPage;
if($take < 1) { $take = 1; }
if($skip < 0) { $skip = 0; }
$basicQuery = DB::select(DB::raw("<here is the big diffcult SQL query>"));
$totalCount = $basicQuery->count();
$results = $basicQuery
->take($perPage)
->skip($skip)
->get();
$paginator = new \Illuminate\Pagination\LengthAwarePaginator($results, $totalCount, $take, $page);
return $paginator;
I would recommend using the paginate() method.
You can read more about Laravel's pagination.

Laravel - How to convert Stdclass object to Array

I'm facing this problem when use database in Laravel. How can i convert that to Array the most simpletest?
$data = DB::table('users')->get();
Please try this. this will return array of objects.
$result = json_decode(json_encode($data, true));
*Updated
if you want to convert all nested properties to the array, try this.
$result = json_decode(json_encode($data, true), true);
get() will return a collection. If you want to get an array of objects, use the toArray() method:
$data->toArray();
If you want to convert every object to an array too, do this:
$data->map(function($i) {
return (array)$i;
})->toArray();
I usually run into this problem when I use DB::select and manually write my sql:
$sql = 'SELECT *
FROM ba_pics ba
INNER JOIN pages pgs
ON ba.service_page_id = pgs.id';
$baPics = DB::select($sql);
$baPics = json_decode(json_encode($baPics, true), true);
return view('beforeAndAfter',['baPics'=>$baPics, 'lodata'=>'no lodata yet']);
Here is another way using PHP's array_map function:
$sql = 'SELECT *
FROM ba_pics ba
INNER JOIN pages pgs
ON ba.service_page_id = pgs.id';
$baPics = DB::select($sql);
$baPics = array_map(function($i) {
return (array)$i;
}, $baPics);
return view('beforeAndAfter',['baPics'=>$baPics, 'lodata'=>'no lodata yet']);

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 can I get separate value of the array item in the codeigniter

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;

Resources