I have a question regarding the use of quotes in Laravel to explain I put a code snippet.
public function index(Request $request){
$query=trim($reques->('serchText'));
$categorias=DB::table('categoria')
->where('nombre','LIKE','%'.$query.'%')
->where('condicion','=','1')
->orderBy('idcategoria','DESC')
->paginate(7);
return view('almacen.categoria.index',["categorias"=>$categoria,"serchText"=>$query]);
}
in the part where I return the view I use single quotes but if I put normal quotes it still returns the view, I don't know which one I should use and why, on the other hand in the section where I assign the value to the "categories" parameter
["categorias"=>$categoria,"serchText"=>$query]
Is it okay if I write it with those quotes or should it be with simple food, what is in quotes "categories" becomes an object like "serchText"?
The use of quotes is independent of the framework, that is to say that this is a characteristic of the PHP Language, the use of quotes is to specify a literal type String, when declaring Strings in PHP there are not only those two ways to do it, but that there are 4 ways:
Single Quotation Mark
This is the most basic way to declare strings. Example:
Unlike the heredoc and double-quoting syntax, variables and escape statements for special characters will not be expanded when enclosed within a single-quoted string. That is,
\n
and$a
will be displayed as literal strings.Double Quotation
If a string is delimited with double quotes ("), PHP will interpret the following escape sequences as special characters:
The most important feature of double quoting a string is the fact that variable names are expanded. That is, it
"$a"
will show the value assigned to a$a
and not $a as a literal string, example:In conclusion, single quotes show the text as it is defined, and the use of double quotes interprets special characters and also expands the value of the variables to show it.
Going to your question, both ways are fine, if you wanted to have variable array indices for example you could use double quotes to define something like:
Although the above could also be done with single quotes as follows:
However this could make the code a bit unreadable in case you concatenate more values.
For more information on the other ways to define strings, check the official PHP documentation http://php.net/manual/es/language.types.string.php
The following English SO question may also be helpful to you https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php