doumo6356 2016-03-02 19:53
浏览 119

在laravel中使用动态创建的锚点href进行搜索

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:

John Doe is affiliated with:

  • Pepsi Company
  • Cold Drinks Inc Company
  • Sports Drinks Company

John Doe is affiliated with:

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

2.Whats the best way to structure my database. Should I use a single column for each affiliation or put all the affiliations in one.

  • 写回答

1条回答 默认 最新

  • doubeng3412 2016-03-03 01:32
    关注

    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. You want the affiliations table to hold the id of the user and the id of the company.

    First, create 3 database migrations in your command line:

    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.

    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.

    php artisan migrate
    

    You have several options at this point when you query your database. For example, a raw SQL query might look like:

    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. I like to create Db models in my App/Db folder. For example, this is what the /App/Db/Companies.php may look like.

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

    You would make your other Db table models (Affiliations & Users) too, and in your application, a query might look like this:

    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. More on 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');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?