How to merge all models in laravel? - laravel-5.8

I want to show all slideshow when the images are not blank.
public function index()
{
$slideAdvertise = Advertise::whereNotNull('image')->get();
$slideDesignStudio = DesignStudio::whereNotNull('image')->get();
$slideHouse = House::whereNotNull('image')->get();
$slidePhotographer = Photographer::whereNotNull('image')->get();
$slideWebsite = Website::whereNotNull('image')->get();
$slideShows = $slideAdvertise->merge($slideDesignStudio)->merge($slideHouse)->merge($slidePhotographer)->merge($slideWebsite);
return view('Home.index', compact('slideShows'));
}
Note: This code does not even have an error.

There are two things you are missing.
First the all() function at the end of the merge calls.
$slideShows = $slideAdvertise->merge($slideDesignStudio)->merge($slideHouse)->merge($slidePhotographer)->merge($slideWebsite)->all();
Second is to use arrays in case a query returns no results.
public function index()
{
$slideAdvertise = Advertise::whereNotNull('image')->get()->toArray();
$slideDesignStudio = DesignStudio::whereNotNull('image')->get()->toArray();
$slideHouse = House::whereNotNull('image')->get()->toArray();
$slidePhotographer = Photographer::whereNotNull('image')->get()->toArray();
$slideWebsite = Website::whereNotNull('image')->get()->toArray();
$slideShows = $slideAdvertise->merge($slideDesignStudio)->merge($slideHouse)->merge($slidePhotographer)->merge($slideWebsite)->all();
return view('Home.index', compact('slideShows'));
}

Related

AES-128-CBC algorithm: openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended

public function encrypt($message){
$sifrelememetodu = "AES-128-CBC";
$benimsifrem = "birgul.091!birgul!";
$sifresicozulen = openssl_encrypt($message, $sifrelememetodu, $benimsifrem);
return $sifresicozulen;
}
public function decrypt($message){
$sifrelememetodu = "AES-128-CBC";
$benimsifrem = "birgul.091!birgul!";
$sifresicozulen = openssl_decrypt($message, $sifrelememetodu, $benimsifrem);
return $sifresicozulen;
}
$data = $this->encrypt("47380908767");
$data =$this->decrypt($data);
But encrypt function is giving error that:
openssl_encrypt(): Using an empty Initialization Vector (iv) is
potentially insecure and not recommended
You just need to use the openssl_encrypt. You can find more information in PHP documentation here
You decrypt function:
public function decrypt($message){
$sifrelememetodu = "AES-128-CBC";
$benimsifrem = "birgul.091!birgul!";
$sifresicozulen = openssl_decrypt($message, $sifrelememetodu, $benimsifrem);
return $sifresicozulen;
}
The encrypt function will be:
public function encrypt($message){
$sifrelememetodu = "AES-128-CBC";
$benimsifrem = "birgul.091!birgul!";
$sifresicozulen = openssl_encrypt($message, $sifrelememetodu, $benimsifrem);
return $sifresicozulen;
}

I want to retrieve the id from the query result and reuse it for the next query laravel

I want to use the id from the select query results, then I want to use that id again to find data using wherein. but the error i found,
public function getMaut(){
$merek="samsung";
$tipe="led TV";
$display ="25-32";
$kriteria=Kriteria::all();
$alternatif = Alternatif::where('merek',$merek)->where('tipe',$tipe)->where('display',$display)->get();
$alternatif_id = (array)$alternatif->id;
$nilaialter = Nilaialternatif::whereIn('id_alternatifs',$alternatif_id);
return view('spk.index',compact('kriteria','alternatif','nilaialter','alternatif_id'));
}
Use the pluck method to retrieve all values for a given key.
$alternatif = Alternatif::where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get();
$alternatif_id = $alternatif->pluck('id');
$nilaialter = Nilaialternatif::whereIn('id_alternatifs', $alternatif_id)->get();
Alternatively, you can create a hasMany relation to 'Nilaialternatif' at 'Alternatif' model.
class Alternatif extends Model
{
public function nilaialternatifs()
{
return $this->hasMany(Nilaialternatif::class, 'id_alternatifs');
}
}
Then query the relationship like
$alternatif = Alternatif::with('nilaialternatifs')->where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get();
$nilaialter = $alternatif->nilaialternatifs;
$nilaialter = [];
$alternatif = Alternatif::where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get()->toArray();
if (count($alternatif)) {
$alternatif_ids = collect($alternatif)->pluck('id')->toArray();
$nilaialter = Nilaialternatif::whereIn('id_alternatifs', $alternatif_ids)->get()->toArray();
}
use laravel collection, https://laravel.com/docs/6.x/collections
use pluck method of collection,https://laravel.com/docs/6.x/collections#method-pluck

carregar múltiplos modelos em um controller

In my Create function, from my products controller. I make several calls to other models that are used to mount combos on the view blade.
$grade = Grade::all();
$marca = Marca::all();
$ncm = Ncm::all();
$clafiscal = Clafiscal::all();
$Otributaria = Origemtributaria::all();
$unidade = Unidade::all();
...
return view('products.create',
compact(
'page',
'etiqueta',
'compCusto',
'grade',
'marca',
'ncm' ,
'clafiscal',
'Otributaria',
'grupo',
'colecao'...);
Is it possible for me to reduce these calls?
I believe there is no other way to compact multiple values.
But, if your problem is related to the dirty controller (with many function calls and responsibilities), you should try to create a more specific method, with will handle this dependencies load and dependencies compact.
Try something like this:
public function loadDependencies()
{
$dependencias = [];
$dependencias['grade'] = Grade::all();
$dependencias['marca'] = Marca::all();
$dependencias['ncm'] = Ncm::all();
$dependencias['clafiscal'] = Clafiscal::all();
$dependencias['Otributaria'] = Origemtributaria::all();
$dependencias['unidade'] = Unidade::all();
return compact($dependencias);
}
public function create()
{
return view('products.create', $this->loadDependencies());
}

How to call information from one model to another Codeigniter

I'm stuck on this from a while.Can't figured it out.I reed documantion, tried with several videos and tried like 10 different ways, nothing is working yet.So I have one view/model for one thing, in this example Destination and I have separate files for Offers.The controllers for both are in one file.I want tho the information that is in destination to go to Offers as well.Please help I can't figure out what I'm missing:
So here is the most important parts:
destination_model.php
<?php class Destination_model extends CI_Model
{
public function getDestinationDetails($slug) {
$this->db->select('
id,
title,
information
');
$this->db->where('slug', $slug);
$query = $this->db->get('destinations')->row();
if(count($query) > 0)
{
return $query;
}
else
{
// redirect(base_url());
}
}
public function getOffersByDestination($destination_id)
{
$this->db->select('
o.short_title,
o.price,
o.currency,
o.information,
o.long_title_slug,
oi.image,
c.slug as parent_category
');
$this->db->from('offers o');
$this->db->join('offers_images oi', 'oi.offer_id = o.id', 'left');
$this->db->join('categories c', 'c.id = o.category');
$this->db->group_by('o.id');
$this->db->where('o.destination', $destination_id);
$this->db->where('o.active', '1');
return $this->db->get();
} }
And then in the controller for offers I put this:
$this->load->model('frontend/destination_model');
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
All I need is the title and the information about the destination.
Here is the whole controller for the offers:
$data = $this->slugs_model->getOfferDetails(strtok($this->uri->segment(2), "."));
$this->load->model('frontend/offers_model');
$this->load->model('frontend/destination_model');
$this->params['main'] = 'frontend/pages/offer_details';
$this->params['title'] = $data->long_title;
$this->params['breadcumb'] = $this->slugs_model->getSlugName($this->uri->segment(1));
$this->params['data'] = $data;
$this->params['images'] = $this->slugs_model->getOfferImages($data->id);
$this->params['similar'] = $this->slugs_model->getSimilarOffers($data->category, $data->id);
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
$this->params['offers'] = $this->offers_model->getImportantOffers($data->offers, $data->category, $data->id);
You need to generate query results after you get it from model,
e.g: row_array(), this function returns a single result row.
here's the doc: Generating Query Results
try this:
$this->load->model('frontend/destination_model');
$data['destination'] = $this->destination_model->getOffersByDestination($data->id)->row_array();
$this->load->view('view_name', $data);
And in your view echo $destination['attribut_name'];,
or you can print the array, to see if it's work print_r($destination);

TYPO3 Extbase: How to render the pagetree from my model?

I want to create some kind of sitemap in extbase/fluid (based on the pagetree). I have loaded the pages table into a model:
config.tx_extbase.persistence.classes.Tx_MyExt_Domain_Model_Page.mapping.tableName = pages
I have created a controller and repository, but get stuck on the part wich can load the subpages as relation into my model.
For example:
$page = $this->pageRepository->findByPid($rootPid);
Returns my rootpage. But how can I extend my model that I can use $page->getSubpages() or $page->getNestedPages()?
Do I have to create some kind of query inside my model? Or do I have to resolve this with existing functions (like the object storage) and how?
I tried a lot of things but can simply figure out how this should work.
you have to overwrite your findByPid repository-method and add
public function findByPid($pid) {
$querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$this->setDefaultQuerySettings($querySettings);
$query = $this->createQuery();
$query->matching($query->equals('pid', $pid));
$pages = $query->execute();
return $pages;
}
to get all pages. Than you can write your own getSubpages-method like
function getSubpages($currentPid) {
$subpages = $this->pagesRepository->findByPid($currentPid);
if (count($subpages) > 0) {
$i = 0;
foreach($subpages as $subpage) {
$subpageUid = $subpage->getUid();
$subpageArray[$i]['page'] = $subpage;
$subpageArray[$i]['subpages'] = $this->getSubpages($subpageUid);
$i++;
}
} else {
$subpageArray = Array();
}
return $subpageArray;
}
i didn't test this method, but it looks like this to get alle subpages.
i wonder that i could´t find a typo3 method that return the complete Page-Tree :( So i write a little function (you can use in an extbase extension), for sure not the best or fastes way, but easy to extend or customize ;)
first you need an instance of the PageRepository
$this->t3pageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
this->t3pageRepository->init();
make the init, to set some basic confs, like "WHERE deletet = 0 AND hidden = 0..."
then with this function you get an array with the page data and subpages in. I implement yust up to three levels:
function getPageTree($pid,$deep=2){
$fields = '*';
$sortField = 'sorting';
$pages = $this->t3pageRepository->getMenu($pid,$fields,$sortField);
if($deep>=1){
foreach($pages as &$page) {
$subPages1 = $this->t3pageRepository->getMenu($page['uid'],$fields,$sortField);
if(count($subPages1)>0){
if($deep>=2){
foreach($subPages1 as &$subPage1){
$subPages2 = $this->t3pageRepository->getMenu($subPage1['uid'],$fields,$sortField);
if(count($subPages2>0)){
$subPage1['subpages'] = $subPages2;
}
}
}
$page['subpages'] = $subPages1;
}
}
}
return $pages;
}

Resources