samedi 22 août 2015

laravel 5 Handling relationships

I am looking into how I can handle my relationships. At the moment I have the migration

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('userName')->default('');
        $table->string('userEmail')->default('');
        $table->string('slug')->default('');
        $table->timestamps();
    });

    Schema::create('clients', function (Blueprint $table) {
        $table->increments('id');
        $table->string('clientName')->default('');
        $table->string('contactEmail')->default('');
        $table->string('slug')->default('');
        $table->timestamps();
    });

    Schema::create('projects', function (Blueprint $table) {
        $table->increments('id');
        $table->string('projectId')->default('');
        $table->integer('clientId')->unsigned()->default(0);
        $table->foreign('clientId')->references('id')->on('clients')->onDelete('cascade');
        $table->integer('userId')->unsigned()->default(0);
        $table->foreign('userId')->references('id')->on('users')->onDelete('cascade');
        $table->string('clientStatus')->default('');
        $table->string('contactName')->default('');
        $table->string('projectName')->default('');
        $table->timestamps();
    });
}

So that creates three tables where the clients and users are related to a project. In Client.php, I then have

public function project()
{
    return $this->belongsTo('App\Project');
}

I have the same in User.php. Then in Project.php I have

public function client()
{
    return $this->hasOne('App\Client');
}

public function user()
{
    return $this->hasOne('App\User');
}

So I think that this is all set up correctly. A project should only have one client and user.

This is whats confusing me. In my ProjectsController, I have the usual create function. When I display the view, I want to pass it the names of all the clients in the database and the users, so they can be chosen from a select box. To achieve this I do

public function create()
{
    $clients = Client::lists('clientName', 'id');
    $users = User::lists('userName', 'id');
    return View::make('projects.create', compact('clients', 'users'));
}

This works fine, the id is the value and the name is displayed. After a client and user are selected and the form submitted, I need to pass this into my projects table (along with the other data).

The HTML for the select looks something like

<select id="clientName" class="form-control" name="clientName">
    <option value="1">Client 1</option>
</select>

How can I get the value for the selected client/user? If I do

$clientID = Input::get('clientName');

It gives me Client 1. Also, is this the best way to do it. Basically, I want to prepopulate a select with all my users and clients. When one is selected and the form submitted, I want to insert the selected user/clients id into my table.

Thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1JnKfpZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire