Table custom like this
I want get data from table " custom " by id in block magento 2. Please help me! Thanks all
UPDATE
I want get data by " custom_id "
UPDATE
function __construct
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Webkul\MpSellerMapLocator\Model\Mpsellermaplocator $mpSellerMapLocatorModel,
array $data = []
) {
parent::__construct($context, $data);
$this->mpSellerMapLocatorModel = $mpSellerMapLocatorModel;
}
get data
$customId = 4;
$marketplaceMpsellermaplocator = $this->mpSellerMapLocatorModel->addFieldToFilter('custom_id', $customId);
echo "<pre>";var_dump($marketplaceMpsellermaplocator->getData());
exit;
-
1mageplaza.com/magento-2-module-development/… this can help you.Rizwan Khan– Rizwan Khan2019年06月07日 04:33:01 +00:00Commented Jun 7, 2019 at 4:33
-
Name field coming from join or name is the field of your custom table?Amit Bera– Amit Bera ♦2019年06月07日 04:39:00 +00:00Commented Jun 7, 2019 at 4:39
1 Answer 1
Use load by Id to get data by id.
Try with below way.
Add below code in your block file.
<?php
namespace Test\Module\Block;
class TestBlock extends \Magento\Framework\View\Element\Template
{
protected $customTable;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Namespace\ModuleName\Model\customTableFactory $customTable
) {
$this->customTable = $customTable;
parent::__construct($context);
}
public function getLoadProduct($id)
{
return $this->customTable->create()->load($id);
}
}
In the above code, I inject class \Namespace\ModuleName\Model\customTableFactory in your case you have to inject your model class. and pass your entity_id in $id
That's it! I hope it helps.
Update:
You can also get data by another field like below.
protected $customTablecollection;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Webkul\MpSellerMapLocator\Model\ResourceModel\Mpsellermaplocator\collection $mpSellerMapLocatorModel,
array $data = []
) {
parent::__construct($context, $data);
$this->mpSellerMapLocatorModel = $mpSellerMapLocatorModel;
}
And then you can use your variable directly in your code:
$customId = 4;
$marketplaceMpsellermaplocator = $this->mpSellerMapLocatorModel->addFieldToFilter('custom_id', $customId);
echo "<pre>";var_dump($marketplaceMpsellermaplocator->getData());
exit;
-
can you get it by name ?Brian NN– Brian NN2019年06月07日 04:40:45 +00:00Commented Jun 7, 2019 at 4:40
-
Yes you can get it by name also but you have to use another methode.Chirag Patel– Chirag Patel2019年06月07日 04:41:17 +00:00Commented Jun 7, 2019 at 4:41
-
can you help me ? :(((Brian NN– Brian NN2019年06月07日 04:42:49 +00:00Commented Jun 7, 2019 at 4:42
-
Check my updated answer :)Chirag Patel– Chirag Patel2019年06月07日 04:45:33 +00:00Commented Jun 7, 2019 at 4:45
-
Hi Chirag Patel! You are right, thank you. can you check update question? i have a new issue, get data by custom_idBrian NN– Brian NN2019年06月07日 04:55:23 +00:00Commented Jun 7, 2019 at 4:55