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.
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.