How can I add operator ternary on section blade laravel? - laravel

I try like this :
#section('title', 'Search' . '"' . empty($search) ? '' : $search . '" | Myshop')
On my view blade laravel
But it does not work
I want if $search exist, the result like this :
Search "test" | Myshop
For example $search = test
IF $search not exist, the result like this :
Search "" | Myshop
How can I do it?

to display the results that you want you can use this:
If condition appoarch
#section('title')
Search "#if(isset($search)){{$search}}#endif" | Myshop
#endsection
or
Ternary approach
#section('title')
Search "{{isset($search) ? $search : ''}}" | Myshop
#endsection
instead your existing code.

Assuming that your $search will always be defined... this should work. If it's not defined, then use isset instead of empty.
#section('title', "Search \"" . empty($search) ? null : $search . "\" | Myshop")

Related

How can we fetch images from database to dashboard using CodeIgniter 3

Image path is getting stored in the database and file is getting uploaded in the upload path but the image is not displaying in the tabular format.
Here is the code for the view:
<tbody>
<?php
$query = $this->db->query("SELECT * FROM banner_mgmt LIMIT 1");
// $url = base_url().
foreach ($query->result() as $row)
{
echo "<tr>";
echo "<td>$row->id</td>";
echo "<td>$row->banner_name</td>";
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
echo "<td>$row->banner_image</td>";
echo "</tr>";
}
?>
</tbody>
Assuming that base_url().$row->banner_image does actually result in a correct path/image name...
You need to check your string concatenation.
What you have... (overly complicated)
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
What you should have...
echo '<td><img src ="'.base_url().$row->banner_image.'" style="height:150px; width:150px;" alt="not found"></td>';
You need to think about how you want the string to turn out, which helps you determine when and where to use ', " and . properly.
Here I am wrapping the string using ' (single quotes), which allows me to use " (double quotes) in the HTML properties without much effort.
Read up on how to use base_url() in the Codeigniter user guide...
So you could have
echo '<td><img src ="'.base_url($row->banner_image).'" style="height:150px; width:150px;" alt="not found"></td>';
You could even use " (double quotes) which evaluate php but you will need to escape any 'internal' double quotes you want to display.
echo "<td><img src =\"base_url($row->banner_image)\" style=\"height:150px; width:150px;\" alt=\"not found\"></td>";
So, you need to read the answer at What is the difference between single-quoted and double-quoted strings in PHP?

Laravel: Calling Artisan command from controller not working properly

I'm trying to backup my db when I hit a spesific route.
first I made artisan command: php artisan backupDB
here is the function:
echo "Generating backup file..\n";
$filename = "backup-" . Carbon::now()->format('Y-m-d-H-i-s') . ".sql";
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
echo "Backup file path: ".storage_path() . "/app/backup/".$filename;
I run it in cli and it is working fine.
then when I tried to run it via controller:
use Illuminate\Support\Facades\Artisan;
public function backupDB{
Artisan::call("backupDB");
}
when I hit the route it prints the echo, but nothing else happened. just that.
So, I found a solution. wrap the command inside a job or just put the code inside that job, and then start the queue. By doing that the code works fine when I try to hit the route.
php artisan make:job backupDB
it will create a file inside app>jobs folder
then put the code inside handle() function.
next just call the job inside your controller or route
use App\Jobs\backupDB;
public function backup()
{
$process = new pull();
dispatch($process);
}
and you need to setup the queue, here is the docs
You don't need to reinvent the wheel. Just use this extension.
PS: I can't explain the problem you are facing, because you didn't provide any debug info: logs, backtrace, or smth like that.
PS2: Be careful with exec() functions usage

Unescaped pug attribute not processed by laravel blade

I'm using Laravel Pug https://github.com/BKWLD/laravel-pug, and the file is named *.pug.blade.php. I'm trying to set an attribute value.
| <?php $x = "any"; ?>
div(class!="{{ $x }}")
| {{ $x }}
the output html is
<div class="{{ $x }}">any</div>
Interestingly, in attribute, the php variable is accessed with no < ? php ? > or {{ }} enclosures, just like javascript variables
| <?php $x = "any"; ?>
div(class=$x)
| {{ $x }}
the HTML output
<div class="any">any</div>
Also, the dollar sign can be omitted, and interpolation can be used
| <?php $x = "any"; ?>
div(class = x + "test")
| {{ $x }}
the HTML output
<div class="anytest">any</div>

GET path not accepting 'dot' in it

I have router path with a '.' in it, my path is downloads/pass.pkpass
I have defined the path in web.php as the following
$router->get('downloads/pass.pkpass', 'PassServerController#downloadPass');
but somehow it's not working. if I remove the '.' it's working fine. what might be the problem here?
It's was working before recently I updated lumen after that it's not working.
The . is the string concatenation operator in PHP.
example
<?php
$string1 = "Test";
$string2 = "working!";
$string = $string1 . $string2;
echo $string;
?>
this will print "Test working!"
You can get your route by this may be
$router->get('downloads/pass.'.'pkpass', 'PassServerController#downloadPass');
But this is some kind of hack we can say.
You can use _ instead of . parameter that will work for sure.

Laravel 5.5 - Transfer file to S3

I have an array of filenames that looks like this
array (
'file1.jpg',
'file2.jpg',
'file3.jpg'
)
I am trying to loop through them and upload them to an S3 bucket like this..
foreach ($filenames as $filename) {
Storage::disk('s3')->put(
/* S3 */
's3foldername/' . $foldername . '/' . $filename,
/* Local Storage */
storage_path('localfolder/' . $foldername . '/' . $filename),
'public'
);
}
This isn't working for some reason, the paths all check out ok. Do I need to read the file contents first?
Storage::put() method takes either file content or a resource handle as its second argument - see the docs here: https://laravel.com/docs/5.5/filesystem#storing-files
In your case, the following should do the trick:
Storage::disk('s3')->put(
's3foldername/' . $foldername . '/' . $filename,
file_get_contents(storage_path('localfolder/' . $foldername . '/' . $filename)),
'public'
);
It's just one of the options - see the docs for more.

Resources