I get this above mentioned error when i was trying to write a search and filter query for gridview,here is code:
Store Model: Relation
public function getStoreNamesCombo(){
return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName;
}
}
StoreSearch Model:
$query->joinWith(['author',
'storeNamesCombo']);
if(!empty($this->storeNamesCombo)){
$query->andWhere('storeNames.classId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.platformId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.familyId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.subFamilyName LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.variantName LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR CONCAT(storeNames.classId, " ", storeNames.platformId, " ", storeNames.familyId, " ", storeNames.subFamilyName, " ", storeNames.variantName) LIKE "%' . $this->storeNamesCombo . '%"'
);
Gridview
'value' => function($q) use ($storeFamilies, $storeClasses, $storePlatforms){
return implode('.', array_filter([
$storeClasses[$q->storeName->classId] ?? null,
$storePlatforms[$q->storeName->platformId] ?? null,
$storeFamilies[$q->storeName->familyId] ?? null,
$q->storeName->subFamilyName,
$q->storeName->variantName,
]));
Error
Trying to get property of non-object
Error Line: Store Model Relation.
UPDATE:
public function getStoreName()
{
return $this->hasOne(StoreNames::className(), ['id' => 'storeNameId'])->via('transaction');
}
thanks in advance,
Make sure relation is fetched first to be safe.
public function getStoreNamesCombo()
{
if ($this->storeName) {
return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName;
}
return null;
}
EDIT:
BUT in your code one thing does not make sense. You are using value of getStoreNamesCombo() as the condition for query searching through storeNames table which is something like an inception idea.
I guess what you want to do is:
Set field in form model that will be filled with search term for the query condition.
public $storeNameSearch;
User fills that through the form for example. Now you can use it for the query.
if (!empty($this->storeNameSearch)){
$query->andWhere([
'or',
['like', 'storeNames.classId', $this->storeNameSearch],
['like', 'storeNames.platformId', $this->storeNameSearch],
['like', 'storeNames.familyId', $this->storeNameSearch],
['like', 'storeNames.subFamilyName', $this->storeNameSearch],
['like', 'storeNames.variantName', $this->storeNameSearch],
]);
}
BTW checking concatenated fields here seems redundant.
You can use getStoreNamesCombo() (->storeNamesCombo) on the prepared model in the gridview.
Edit 2:
If some of the columns are integers it sounds more reasonable to build conditions differently. If you can filter each column separately and there are filter fields user can fill it would be something like:
$query
->andFilterWhere(['storeNames.classId' => $this->storeClassId])
->andFilterWhere(['storeNames.platformId' => $this->storePlatformId])
->andFilterWhere(['storeNames.familyId' => $this->storeFamilyId])
->andFilterWhere(['like', 'storeNames.subFamilyName', $this->storeFamilyName])
->andFilterWhere(['like', 'storeNames.variantName', $this->storeVariantName]);
where first 3 columns are integers and last two are strings.
Related
$files = $request->file('attachment');
$attachmentDataPush = NULL;
if ($request->hasfile('attachment')) {
foreach ($files as $file) {
$message->attach($file->getRealPath(), [
'as' => $file->getClientOriginalName(),
'mime' => $file->getMimeType()
]);
$filename = 'AttachmentsBox/' . $message->getId() . '/' . $file->getClientOriginalName();
Storage::put($filename, $file->get());
$attachmentDataPush = $attachmentDataPush . "<" . $file()->getClientOriginalName() . ">";
}
}
I want to add the code I wrote in $ attachmentDataPush side by side in <> but it gives an error. "Function name must be a string" What do you think may be the problem. ?
$attachmentDataPush = $attachmentDataPush . "<" . $file()->getClientOriginalName() . ">";
Line gives an error.I will insert the transaction into the database.
$attachmentDataPush = $attachmentDataPush . "<" . $file->getClientOriginalName() . ">";
I functioned as file (). I accidentally put brackets. The top one is correct.
I have the following query:
$venues = Venue::select(['id', 'name'])
->where('name', 'LIKE', "%{$query}%")
->orderByRaw("CASE " .
"WHEN name like '{$query}%' THEN 0 " . // start with
"WHEN name like '% {$query}%' THEN 1 " . // start of a later word
"ELSE 3 " .
"END"
)
->limit(5)
->get();
The issue is the above query is vulnerable to SQL injection.
How can I fix this?
Parameter bindings is explained here:
https://laravel.com/docs/5.6/queries#raw-expressions
But if I do:
$venues = Venue::select(['id', 'name'])
->where('name', 'LIKE', "%{$query}%")
->orderByRaw("CASE " .
"WHEN name like '?%' THEN 0 " . // start with
"WHEN name like '% ?%' THEN 1 " . // start of a later word
"ELSE 3 " .
"END",
[
$query,
$query,
]
)
->limit(5)
->get();
I get different results.
Try adding the percent to the query param, like this:
...
->orderByRaw("CASE " .
"WHEN name like ? THEN 0 " . // start with
"WHEN name like ? THEN 1 " . // start of a later word
"ELSE 3 " .
"END",
[
"{$query}%",
"% {$query}%",
]
)
...
I have build a query to search on text using n_make and n_month using if, where and or clause
However - unsure why is n_month not getting executed, search only happening for text if its in n_make and not in n_month
I want a search to display output if its shown in either n_make or n_month
// Filter by search in title
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.id = ' . (int) substr($search, 3));
}
else
{
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.n_make LIKE ' . $search . ' )')
or
$query->where('( a.n_month LIKE ' . $search . ' )')
;
}
}
else
{
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.n_make LIKE' . $search . ') OR ( a.n_month LIKE' . $search . ')');
}
I am receiving a syntax error when typing punctuation into my website form such as ' or &. I am using the below code on the page when receiving this error. What am I missing or doing wrong that will fix this issue?
$name=$_REQUEST["title"];
$stdate=$_REQUEST["sdate"];
$endate=$_REQUEST["edate"];
$staddr=$_REQUEST["staddr"];
$addr2=$_REQUEST["staddr2"];
$city=$_REQUEST["city"];
$state=$_REQUEST["state"];
$zip=$_REQUEST["zip"];
$desc=$_REQUEST["desc"];
$file=$_REQUEST['photo'];
$link=$_REQUEST["link"];
$user=$_REQUEST["user"];
/***************** DELETE QUERY ****************************/
$date2 = date('Y-m-d');
$qry = "DELETE FROM table WHERE STR_TO_DATE(`endate`, '%Y-%m-%d') < '".$date2."'";
$del = mysql_query($qry);
$query = "INSERT INTO table (fname,stdate,endate,addr1,addr2,city,state,zip,name,size,type,content,link,description,user) VALUES('" . mysql_real_escape_string($name) . "','$stdate','$endate','" . mysql_real_escape_string($staddr) . "','" . mysql_real_escape_string($addr2) . "','$city','$state','$zip','".str_replace(' ','',$name)."-".$stdate."-".$file.".png','0',' ',' ','" . mysql_real_escape_string($link)."','" . mysql_real_escape_string($desc) . "','$user')";
$q2=mysql_query($query) or die('Error, query failed'. mysql_error());
if($q2) {
echo "ok";
} else {
echo "error ".$query.mysql_error();
}
?>
The issue stems from the query line
I am using a 'While' loop to run through an array of users' first names, last names, and emails. When my 'While' loop executes, and I echo the data, it successfully shows the row data from the database, but it does not show Row 1. It starts at row 2 and spits everything out properly, but why is it skipping row 1?
$query= "SELECT * FROM email_list";
$result=mysqli_query($dbc, $query);
$row=mysqli_fetch_array($result);
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}
mysqli_close($dbc);
One thing I have found, is if I put this section:
$row=mysqli_fetch_array($result);
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
above the 'While' loop, then it does successfully show the first row of data, and all the other rows too, so, if that's confusing, coding it like this (the long way - below) actually works, but it seems redundant and I want to use best DRY practices:
$query= "SELECT * FROM email_list";
$result=mysqli_query($dbc, $query);
$row=mysqli_fetch_array($result);
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}
mysqli_close($dbc);
You have 2 calls to $row=mysqli_fetch_array($result); before you print your result. One before the loop and the other as the expression in the while statement.
Try deleting the one before the while loop.
$result=mysqli_query($dbc, $query);
// kill this line $row=mysqli_fetch_array($result);
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}