Label selection
Objective
In the previous section, we implemented the code for many-to-many relationships between article and tag. In this section, continue to the previous section, let's how to implement the tag selection on the UI.
Description
Development environment: Windows 7
Laravel version: 5+
Ide:phpstorm
Last lesson, we take the article tag as an example, the article and tag as many-to-many cases to explain, and the implementation of the article and tag binding. In this lesson, let's look at how the tag is chosen on the page, that is, the UI.
Select control
What is a Select control? I don't know, huh? If you do not know, look at the following effect to know.
Open form.blade.php and add a form tag under Published_at:
In fact, these things, there are some subsets of the past, specifically do not need to speak more, you can see the effect on the meaning of each parameter. This class is a type defined by bootstrap. Then open the server, visit the localhost:8888/articles/create, see, in Publish on under a more tags and a choice of control.
Since it's a choice, you'll need multiple options. For us, it's multiple tags. Change the code to add a keyword:
Now, although there is a change, our tag is static and written dead. It would be nice to wear the first $tagList or $tags (hereinafter referred to as $tags).
Since you want to get this $tags, imagine this logic, when was this $tags passed on to this created page? That is when we visit the localhost:8888/articles/create, we bring $tags together, so that we can pass $tags into this Form, right?
Let's see how it's done.
Open articlescontroller.php, find the Create () method, here is how we access localhost:8888/articles/create.
Add the following code to the Create () method:
Public Function Create () { $tags =\app\tag::lists (' name ');
Yes, we just get the name of the tag by that sentence. But now, there is only one tag in our tag list, which we created in the previous section, we need to create a few more tags to see the effect.
Open tag.php and add the following array to the Tag class:
protected $fillable = [
Add the name to the $fillable array to prevent massassignmentexception. Next switch to tinker mode: PHP artisan Tinker:
>>> \app\tag::create ([' name ' = ' work ']);=> app\tag {#656 name: "Work", Updated_at: " 2016-05-01 14:02:13 ", created_at:" 2016-05-01 14:02:13 ", id:2,}>>> \app\tag::create ([' name ' = = ') Coding ']);=> app\tag {#655 name: "Coding", updated_at: "2016-05-01 14:03:50", created_at: "2016-05-01 14:03:50 ", id:3,}>>> \app\tag::create ([' name ' = ' life ']); = = App\tag {#645 name: "Life", updated_at: "2016-05-01 14:04:29", created_at: "2016-05-01 14:04:29", id:4,}>>> \app\tag::lists (' name ');=> illuminate\support\collection {#653 all : [ " Personal ", " work ", " coding ", " life ",
Ha, through \app\tag::lists (' name '); can see our tag name list. At present, the list is the way to get the value by subscript, for example: $tags [2] The value is ' coding '; If I want to get tag by tag name? For example: the value that $tags [' coding '] takes is ' coding '. Then look at:
>>> \app\tag::lists (' name ', ' name ');=> illuminate\support\collection {#664 all : [ " Personal "=" and "personal", "work" and "work" , "coding" and "coding", "Life" and "life",
Yes, change it. Open articlescontroller.php to change the Create () method to the following:
Public Function Create () { $tags =\app\tag::lists (' name ', ' name ');
Completed. Look at the effect. Access Localhost:8888/articles/create. Good ~ have the effect, you can choose, you can use the SHIFT key to multi-select OH.
After the selection, we want to create the article, we submitted the tag exactly how many, how to check it?
Open articlescontroller.php and find the Store () method, because this method is used to store the article. Once found, we add a sentence to the method:
Public function Store (articlerequest $request) { dd ($request->input (' tags ')); $article = new article ($request->all ()); Auth::user ()->articles ()->create ($article); Return redirect (' articles ')->with ([ ' flash_message ' = ' = ' Your article has been created! ',
Add a DD (), and when you have selected all the tags through shift, you will find only one tag value when you commit to create the article. Uh? It's not a four choice. Back to form.blade.php, modify here to solve:
After changing tags to tags[], we are passing arrays, not first values.
OK, back to Articlescontrollerphp's store () method, comment out DD. The current situation is that we get all the selected tags, the next step, it is time to bind these tags with the article. This action is similar to the binding action in the previous section.
But in the previous section, when we article the tag, we used the tag ID, not the tag name. Then why did we get the name (for effect ...)? ), get the ID directly. Yes.
Locate the Create () method in articlescontroller.php and modify the following:
Public Function Create () { $tags =\app\tag::lists (' name ', ' id ');
That's it, you don't have to try, it's all right.
Back in the Store () method, write the following code to bind the tag ID article:
Public function Store (articlerequest $request) { $article = Auth::user ()->articles ()->create ($request, All ()); $article->tags ()->attach ($request->input (' tags ')); Return redirect (' articles ')->with ([ ' flash_message ' = ' = ' Your article has been created! ',
The first sentence, get to the value of the article form, the second sentence, get to the form of tags in the ID, it is important that the third sentence, the article with its label binding, through the ID of the tag.
Let's go and have a try. The article is published successfully, look at the database, switch to tinker mode:
>>> $article = App\article::find (Ten);=> app\article {#659 ID: "Ten", user_id: "1", Created_at: "2016-0 5-01 15:26:08 ", Updated_at:" 2016-05-01 15:26:08 ", title:" ASD ", Body:" ASDASDASDASD ", Published_at:" 2016-05- 00:00:00 ",}>>> $article->tags->toarray ();=> [[" id "+ 2," name "=" Work ", "Created_at" = "2016-05-01 14:02:13", "Updated_at" and "2016-05-01 14:02:13", "pivot" and ["AR ticle_id "=", "tag_id" and "2", "Created_at" and "2016-05-01 15:26:08", "Updated_at" => ; "2016-05-01 15:26:08",],], ["id" = 3, "name" = "coding", "created_at" = "2016-05- "14:03:50", "Updated_at" and "2016-05-01 14:03:50", "pivot" and "article_id" = "10", " tag_id "=" 3 "," Created_at "and" 2016-05-01 15:26:08 "," Updated_at "and" 2016-05-01 15:26:08 ", ], ], ]
Well, it's no problem. Two labels.
Show article tags
When the article is displayed, there will be a label for the article below, and we will implement it below. Open show.blade.php and create a label underneath the body div:
Tags:
@foreach ($article->tagsas $tag)
- {{$tag->name}}
@endforeach
Refresh the page to see the effect, a little ugly, do not be ugly ah, caught dead next section does not teach you how to click.
Continue, some articles have tags, some articles without tags, then make a logical judgment it:
@unless ($article->tags->isempty ()) Tags:
@foreach ($article->tagsas $tag)
- {{$tag->name}}
@endforeach
Tag is also provided for the edit page
Open articlescontroller.php, find the edit () method, in fact, and create () the same way:
Public function edit ($id) { $tags =\app\tag::lists (' name ', ' id '); $article = Article::findorfail ($id);
Visit the Localhost:8888/articles/10/edit, you can see the label, but do not know which is already selected, how to mark it?
Open article.php, we create a new method:
Public Function Gettaglistattribute () {
The corresponding form.blade.php also has to be modified:
Careful classmate should have found, why in the article.php have a Gettaglistattribute () method, and in form.blade.php directly call Tag_list can get to the value of the selected tag. This is the Laravel feature, which can be used to automatically correlate methods and objects by naming them.
Now visit Localhost:8888/articles/edit and you will find that you can display the tag that you selected earlier.
Finally, modify the method in the store () method to get the tag:
Public function Store (articlerequest $request) { $article = Auth::user ()->articles ()->create ($request, All ()); $article->tags ()->attach ($request->input (' tag_list ')); Return redirect (' articles ')->with ([ ' flash_message ' = ' = ' Your article has been created! ',
Summarize
Well, try to run the whole process, in fact, it seems smooth, or flawed. This flaw is left to you, or the next lesson to solve.