User:model; Users: Table name; user_id Key value
Relation:public function Tasks () {return $this->belongstomany (' Task ', ' task_id ');}
Task:model name; tasks: table name; task_id Key value
Relation:public function Ower () {return $this->belongstomany (' User ', ' user_id '),}//Note: By default, if you do not specify a key field, you will use the Owner_ ID as key
$user = User::first ();
$user->tasks ()->attach (2); Action will be performed on relation
Task::where (' title ', ' like ', '% $searchdata% ')->get () Full-text Search
In the Laravel relation operation, if you perform a relation relational table operation on a row with multiple rows of data, the system performance will be severely affected by querying the database multiple times. For example, one possible way is to eager oading.
For example, if you have 10 user in the above relationship, you should display
@foreach ($tasks as $task)
<li><strong>{{$task->owner->name}}</strong> have the following tasks {{$task->title}}</li>
@endforeach
PHP code can be improved a little bit to get the data set after the incoming blade template,
$tasks = Task::with (' owner ')->get (); By the modification of this sentence, laravel access to the database will be reduced to 1 times, not 11 times!! (n+1 problem)
The naming convention norm in Laravel and the relation n+1 problem