2

So I'm using the Forms & HTML package and I'm having some difficulties with outputting the data. For example:

when I use this code:

{!! Form::model($article) !!}
    {!! Form::textarea('categories', null) !!}
{!! Form::close() !!}

What I want to achieve here, is to list all of the categories that the article is related to. Like this:

<textarea name="categories">
    Cat1
    Cat2
    Cat5
</textarea>

But, I don't know how can I tell the form to output only a specific column, so what happens is that the textarea is filled with json data (which includes all of the rows).

I know that the best solution is to make a multiple selector, but in this case - I must use textarea.

5
  • Can you do in simple html tags and then loop with php to list all categories? Commented Jan 18, 2017 at 11:17
  • @EddyTheDove I can't do that, because the form I'm using, is used to both create and update resources. Commented Jan 18, 2017 at 11:20
  • Ok. I understand. But you could still use the form as you are using now, and do that only on the textarea. I can show you a sample code if you want. Commented Jan 18, 2017 at 11:21
  • Are you sure you want a textarea?It seems you want a multiple select to me. Commented Jan 18, 2017 at 11:26
  • @Mihai, usually I would prefer a multiple selector, but in this case, I must use textarea. Commented Jan 18, 2017 at 11:40

2 Answers 2

1

Try this:

{!! Form::textarea('categories', implode('\n', $article->categories()->select('colName')
         ->get()->pluck('colName')->all()) !!}

Replace colName with the name of the column you want

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

6 Comments

Your article fetch doesn't makes sense. first u select the desired column, and then u get it, and after that u pluck it, which is unnecessary and then u fetch all of the rows.
Did you try the command? First I select only one column (to minimize SQL output), then I get the collection, then I pluck a column name from the collection (it loses the key value structure), then I convert that into an array (using all()), then I implode the array for new line inserts. Do you understand now?
Firstly, without the pluck (which you say is unnecessary), the collection when converted to an array will be in a key value form which you don't want. Secondly, I don't fetch all the rows, all() here is used to convert the collection to an array. The query has already been fired before calling all() at the get() stage
Look at my answer, if you think your solution is better, I would like your explanation why.
Both solutions are the same actually. select('colName')->get()->pluck('colName') is the same as ->pluck('colName') and all() is an alias of toArray. In terms of performance both are exactly the same, but I'm glad my answer helped you find a solution
|
1

To make the form fields available to be included both in an update form (Form::model) and in a create form (Form::open), I did something like this:

{!! Form::textarea('categories', isset($article) ? implode(PHP_EOL, $article->categories()->pluck('name')->toArray()) : null)

What I did there, was first checking if the $article exists. If it does, it means that I've used the Form::model. when the $article exists, I retrieve the list of the categories as an array and implode in the PHP_EOL ("\n").

But if there's no $article, It means that the form is supposed to be a creation form, so the data will be empty.

I've come up with this solution thanks to Paras. But even tho our results are the same, the query to these results are different.

My result query was:

select `display_name` from `categories` inner join `article_categories` on `categories`.`id` = `article_categories`.`category_id` where `article_categories`.`article_id` = ?

Paras' result query is:

select `display_name`, `article_categories`.`article_id` as `pivot_article_id`, `article_categories`.`category_id` as `pivot_category_id` from `categories` inner join `article_categories` on `categories`.`id` = `article_categories`.`category_id` where `article_categories`.`article_id` = ?

Both of the queries are the same, but it looks like my query is a bit smaller, so I'll go with that query.

1 Comment

Nice work on the sql comparison. Now that you've printed both queries, I do think that a direct pluck might be slightly better in performance. Thanks for letting me know! This should be the right answer, upvoting

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.