使用附件提交表单后如何插入数据透视表?

时间:2019-03-25 21:57:26

标签: laravel eloquent

我使用的是Laravel雄辩的语言,并且尝试使用Attach将表单中的选定用户ID和生成的票证ID插入到数据透视表中,但是我不知道该怎么做。

存储功能

 public function store(Request $request
 {        
     $ticket = new Ticket;
     $ticket->organisation_name = $request['organisation_name'];
     $ticket->postal_address = $request['postal_address'];
     $ticket->physical_address = $request['physical_address'];
     $ticket->description_brief = $request['description'];
     $ticket->hours_dedicated = $request['hours'];
     $ticket->commencement_date = $request['start_date'];
     $ticket->due_date = $request['due_date'];
     $ticket->client_id = $request['client_id'];
     $ticket->save();

     //trying to use attach here

     return redirect('/home');
 }

表格

 <form action="TicketsController@store" method="POST">
        {{csrf_field() }}
        <div class="form-group">
            <label>Organisation Name:</label>
            <input type="text" class="form-control" name="organisation_name" placeholder="Enter Organisation Name">
        </div>
        <div class="form-group">
            <label>Postal address:</label>
            <input type="text" class="form-control" name="postal_address" placeholder="">
        </div>
        <div class="form-group">
            <label>Physical address:</label>
            <input type="text" class="form-control" name="physical_address" placeholder="">
        </div>
        <div class="form-group">
            <label>Client:</label>
            <select class="form-control" name="client_id">
                @foreach ($forms as $form)
                <option>{{$form->client->client_id}}</option>
                @endforeach
            </select>
        </div>
        <div class="form-group">
            <label>Description:</label>
            <input type="text" class="form-control" name="description" placeholder="">
        </div>
        <div class="form-group">
            <label>Hours:</label>
            <input type="text" class="form-control" name="hours" placeholder="">
        </div>
        <div class="form-group">
            <label>Start date:</label>
            <input type="text" class="form-control" name="start_date" type="date" placeholder="">
        </div>
        <div class="form-group">
            <label>Due date:</label>
            <input type="text" class="form-control" name="due_date" type="date" placeholder="">
        </div>
        <div class="form-group">
            <label>User:</label>
            <select class="form-control" name="id">
                @foreach ($users as $user)
                <option>{{$user->id}}</option>
                @endforeach
            </select>
        </div>
        <input type="submit" name="submit" class="btn btn-primary">

    </form>

User.php

 class User extends Authenticatable
 {
 use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
public function tickets(){
    return$this->belongsToMany(Ticket::class,'ticket_user','ticket_id','id');
}

Ticket.php

 class Ticket extends Model
 {

public function client(){
    return $this->belongsTo(Client::class,'client_id');
}
public function users(){
    return $this->belongsToMany(User::class,'ticket_user','id','ticket_id');
}

protected $primaryKey = 'ticket_id';
public $timestamps = false;

 }

1 个答案:

答案 0 :(得分:0)

根据documentation,您只需传递要附加的id。在$ticket->save();之后,您可以添加以下内容:

$ticket->users()->attach($request->input('id'));
如果您传递ID数组,模型,ID集合或模型集合,则

attach()也将起作用。