简体   繁体   English

使用laravel中动态创建的anchor href搜索

[英]Search using dynamically created anchor href in laravel

I want to include a dynamicaly created anchor that links to a search page that displays all users from that company. 我想包含一个动态创建的锚点,该锚点链接到显示该公司所有用户的搜索页面。 Users are allowed to be affiliated with more than one company. 允许用户隶属于多家公司。 The information is being pulled from a mysql database example: 该信息是从mysql数据库示例中提取的:

John Doe is affiliated with: John Doe隶属于:

  • Pepsi Company 百事可乐公司
  • Cold Drinks Inc Company 冷饮公司
  • Sports Drinks Company 运动饮料公司

John Doe is affiliated with: John Doe隶属于:

<a href="search/useraffiliation1"> Pespi Company</a>
<a href="search/useraffiliation2"> Cold Drinks Inc Company</a>
<a href="search/useraffiliation3"> Sports Drinks Company</a>

1.Whats the best way to go about doing this. 1.执行此操作的最佳方法是什么?

2.Whats the best way to structure my database. 2.什么是构建数据库的最佳方法? Should I use a single column for each affiliation or put all the affiliations in one. 我应该为每个从属关系使用一个单独的列还是将所有从属关系合并为一个。

EDIT: I just realized that I was responding to the comments and didn't really answer your original question, so I updated my examples below. 编辑:我只是意识到我正在回应评论,并没有真正回答您的原始问题,因此我在下面更新了示例。

If you are using Laravel and artisan fully to build your database and track migrations, here is what you need to do for the 3 tables . 如果您完全使用Laravel和artisan来构建数据库并跟踪迁移,那么这是对3个表要做的。 You want the affiliations table to hold the id of the user and the id of the company. 您希望从属关系表包含用户ID和公司ID。

First, create 3 database migrations in your command line: 首先,在命令行中创建3个数据库迁移:

php artisan make:migration create_users_table --table="users"
php artisan make:migration create_companies_table --table="companies"
php artisan make:migration create_affiliations_table --table="affiliations"

You will now have 3 migration files in your /database/migrations folder. 现在,/ database / migrations文件夹中将包含3个迁移文件。

The files will look like this: 文件将如下所示:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableUsers extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('email')->unique();
            $table->string('password', 60);
            //more fields @see https://laravel.com/docs/master/migrations#creating-columns
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
}

-- -

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableCompanies extends Migration
{
    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name', 100);
            //more fields @see https://laravel.com/docs/master/migrations#creating-columns
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::drop('companies');
    }
}

-- -

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableAffiliations extends Migration
{
    public function up()
    {
        Schema::create('affiliations', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->integer('company_id')->unsigned();
            $table->primary(['user_id', 'company_id']);
        });
    }

    public function down()
    {
        Schema::drop('affiliations');
    }
}

Now run artisan one more time in your console to create the tables. 现在,在控制台中再次运行artisan,以创建表。

php artisan migrate

You have several options at this point when you query your database. 在查询数据库时,此时有几个选项。 For example, a raw SQL query might look like: 例如,原始SQL查询可能类似于:

SELECT c.* FROM affiliations a JOIN companies c ON (a.company_id=c.id) WHERE a.user_id=1;

or 要么

SELECT u.* FROM affiliations a JOIN users u ON (a.user_id=u.id) WHERE a.company_id=1;

However, it's probably better to use eloquent models since you are using Laravel. 但是,最好使用雄辩的模型,因为您正在使用Laravel。 I like to create Db models in my App/Db folder. 我喜欢在App / Db文件夹中创建Db模型。 For example, this is what the /App/Db/Companies.php may look like. 例如,这就是/App/Db/Companies.php的样子。

<?php 

namespace App\Db;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Companies extends Model {

    use SoftDeletes;

    public function affiliations() {
        return $this->hasMany('App\Db\Affiliations', 'company_id', 'id');
    }
}

THIS IS JUST AN EXAMPLE, AND NOT TESTED. 这仅是示例,未经测试。

Refer to https://laravel.com/docs/master/eloquent-relationships#defining-relationships 请参阅https://laravel.com/docs/master/eloquent-relationships#defining-relationships

You would make your other Db table models (Affiliations & Users) too, and in your application, a query might look like this: 您还将创建其他Db表模型(从属关系和用户),并且在您的应用程序中,查询可能如下所示:

class CompanyController extends Controller {

    public function search($id) {
        $company = Companies::find($id);
        $company->load('affiliations.users');

        foreach ($company->affiliation as $aff) {
            echo $aff->user->name;
        }       
    }
}

You could switch that around too where you find the users that have an affiliation with that company id. 您也可以在找到与该公司ID有关联关系的用户的地方进行切换。 More on querying relations: https://laravel.com/docs/master/eloquent-relationships#querying-relations 有关查询关系的更多信息: https : //laravel.com/docs/master/eloquent-relationships#querying-relations

There are several ways to achieve your desired result, so really read up on those eloquent relationships! 有几种方法可以达到您想要的结果,因此请认真阅读那些雄辩的关系! It seems like a lot of work for something so simple but when you are building larger applications it's very handy. 如此简单的事情似乎需要大量工作,但是在构建大型应用程序时非常方便。

Pertaining to your original question, the route for this could look like: 与您的原始问题有关,此路由可能类似于:

Route::get('/search/company/{id}', 'CompanyController@search');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM