Reputation: 525
Alright, from the title says. I want to generate links in Laravel Blade. The catch, my content came from a Database.
Id | Content | Status
1 | <a href="{{ asset('public/images/projects/p1_002728.jpg') }}"> | A
My controller
public static function ViewPerProject()
{
$data = Projects::GetProjectDetails();
return view('works.project1', ['projectPackage' => $data]);
}
and my View
@foreach($projectPackage as $project)
{!! $project->Content !!}
@endforeach
In which my link generates this when I inspect element.
<a href="{{ asset('public/images/projects/p1_002728.jpg') }}">
I want my link to generate the full path which is somewhat like this
<a href="localhost/arc/public/images/projects/p1_002728.jpg">
Any idea guys?
Upvotes: 0
Views: 2854
Reputation: 21681
I think you can try this:
<a href="{{url('images/projects/p1_002728.jpg')}}">
Upvotes: -1
Reputation: 62368
You're basically storing a blade template inside of your database, which contains both Blade and PHP code. In order to display properly, this is a string that must be processed by the Blade templating engine, as well as the PHP engine.
There are two options.
First, you could dump the string from the database to a temporary file, and then use the @include
directive to include your temporary file. This will obviously have a bit of overhead since you'll be performing a lot of disk I/O depending on how many records you're processing.
Second, you can call the Blade processing and the PHP processing manually. This will involve a call to eval()
, but it isn't any more dangerous than writing the string out to a file and then including it (both of which are dangerous if $project->Content
contains any type of user input!).
Your code for option two would look like:
@foreach($projectPackage as $project)
{!! eval('?>'.Blade::compileString($project->Content) !!}
@endforeach
First, the Blade::compileString()
method will take the string from your database and compile it from a blade template into standard HTML/PHP.
Second, the call to eval()
will run your HTML/PHP through the PHP processor and return the resulting string. The blade template string processed by eval()
must start with a ?>
to start eval()
out in HTML mode, instead of PHP mode (eval()
docs here).
And third, the blade {!! !!}
directives will print out that string without escaping anything.
Upvotes: 0
Reputation: 1288
That's not gonna work. You cannot run php function in that way. It is like echo inside of echo.
{{!! '<a href="{{ asset('images/projects/p1_002728.jpg') }}">' !!}
If you really want to save links to your database for some reason, why not save it like this:
<a href="[domain]public/images/projects/p1_002728.jpg">
This is similar a shortcode of Wordpress.
So before you display your data, you need to prepare it. Pass your data to your parser.
@foreach($projectPackage as $project)
{!! parseDomain($project->Content) !!}
@endforeach
And create a helper that will change the [domain]
to your full domain or public path.
function parseDomain($content){
$domain = asset('/');
return str_replace('[domain]',$domain,$content);
}
But still, I hope you could just save the images/projects/p1_002728.jpg
to your database instead of saving the whole element then echo it.
Upvotes: 1
Reputation: 1005
The service should return this kind of array. just adjust the path to ./ or ../ as per you requirement.
public static function ViewPerProject()
{
$package = ['./images/projects/p1_002728.jpg','./images/projects/p1_002728.jpg']
$this->data['projectPackage'] = $package;
$this->data['someOtherData_1'] = [];
$this->data['someOtherData_2'] = "test";
return view('works.project1')->with('data',$this->data);
}
In view you can use like this.
@foreach($projectPackage as $data['projectPackage'])
<a href="{{$projectPackage}}"></a>
@endforeach
Upvotes: -1