0

I want to implode array with quotes in controller here is my code but that's not doing what I want, starting points and ending points still empty. I tried a lot of code but couldn't get out of it. Thanks for the help.

$product = Product::find($id);
$product = collect($product->images->pluck('image_path'))->implode("','");
return $product;

What I really want to do is send this array into the javascript code in the laravel blade.

initialPreview: ['{{ $product}}'],

But this time, the output is as follows and I can't get the result I want again.

initialPreview: ['img/uploads/urun/1552304918.jpeg','img/uploads/urun/1552304918.jpeg'],
5
  • 3
    Can you show us an example of what image_path returns and what your current code returns and what it should return :)? Commented Mar 11, 2019 at 13:15
  • img/uploads/urun/1552304918.jpeg','img/uploads/urun/1552304918.jpeg Commented Mar 11, 2019 at 13:21
  • @KasperFranz asked for 3 thing and you gave him 1 thing? I think I understand what you mean though. After imploding you could just prepend and append a quote to the string to get something like 'img1.jpeg','img2.jpeg' Commented Mar 11, 2019 at 13:26
  • 1
    You've made an edit to your post so I might answer that as well. {{$var}} escapes characters, use {!! $product !!} to not escape caracters Commented Mar 11, 2019 at 13:27
  • @TeunissenStefan sorry about that, the code from there returns multiple image paths from the database. I want to use I am trying to use initial Preview for kartik-v/bootstrap-fileinput Commented Mar 11, 2019 at 13:28

3 Answers 3

4

Building the javascript string yourself for this purpose has more hidden complexity than you might realize. Not only do you have to implode the array and add quotes and commas to make a valid javascript string, but you also need to securely escape special characters in the strings before they're imploded.

Since your actual goal is to seed an array for javascript, in Laravel it's better to build your php array and then defer to @json for the proper encoding.

To build the array of paths:

$product = Product::find($id);
$product_paths = $product->images->pluck('image_path'); // value: ["img/...","img/..."]

Usage in a Blade view (in a script block):

<script>
    //...

    initialPreview: @json($product_paths),

</script>

Usage in a Blade view (inside an HTML attribute):

<!-- Seeding a data-* attribute, e.g. for jQuery -->
<ul data-initial-preview='@json($product_paths)'></ul>

<!-- Seeding a Vue component property -->
<example-component :initial-preview='@json($product_paths)'></example-component>

Note: The usage of single quotes ' in this context, not double quotes ", is important due to the way quotes are escaped by json_encode() behind the scenes.

Sign up to request clarification or add additional context in comments.

Comments

2

My Laravel is a little rusty, but this should be what you want to do:

$product = Product::find($id);
$product = collect($product->images->pluck('image_path'))
           ->transform(function ($item, $key) {
              return "'" . $item ."'";
           })
           ->implode(",");
return $product;

Changes:

  • Added transform function call.
  • Removed the quote from the implode call

This solution can be prettier, but this is a very quick way to do it.

Comments

0

my function to automatize the protected $fillable

 $x = collect(Schema::getColumnListing('templatefields'))
   ->transform(function ($item, $key) {
             return "'" . $item ."'";
           })
           ->implode(",");

echo "[".  $x.']'; 

1 Comment

It seems, you took part of the code from the checked answer. A good practice and answer refers to others whose code you used and explain what your changes are supposed to do. In this case, your answer (generating the fillable attribute of a data class from "templatefields") is not related to the intent of the question (passing an array of strings to javascript). Admitted, the question's title is very bad and too unspecific. Newcommers can't comment but a comment under the checked answer would fit better than a separate answer of an unrelated problem with your own data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.