Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel多对多关系存储函数_Php_Laravel - Fatal编程技术网

Php Laravel多对多关系存储函数

Php Laravel多对多关系存储函数,php,laravel,Php,Laravel,我在拉拉维尔有一个多对多关系的问题 我的数据库是一个愿望列表,包含了朋友、愿望和希望的朋友。我的愿望朋友表只有愿望id、朋友id和时间戳 我的模型是: <?php namespace App; use Illuminate\Database\Eloquent\Model; use App\Wish; class Friend extends Model { protected $table = "friends"; protected $primaryKey = "i

我在拉拉维尔有一个多对多关系的问题

我的数据库是一个愿望列表,包含了朋友、愿望和希望的朋友。我的愿望朋友表只有愿望id、朋友id和时间戳

我的模型是:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Wish;

class Friend extends Model
{
    protected $table = "friends";
    protected $primaryKey = "id";
    protected $fillable = array("name", "age");
    public $timestamps = true;

    public function wishes()
    {
        return $this->belongsToMany('App\Wish', 'wish_friend')->withTimestamps();
    }
}
这是我的好友视图create.blade

@extends('layouts.master')
@section('content')
<div class="container-fluid">
    <h1>Adicionar uma nova curso</h1>
    <h4>Insira toda a informação sobre a curso.</h4>
    <a href="{{URL::route('friend.index')}}" class="btn btn-default">Voltar atrás</a>
    <hr>

    <form action="{{URL::route('friend.store')}}" method="POST">
        <div class="form-group">
            <label for="name" class="control-label">Nome:</label>
            <input type="text" id="name" name="name" class="form-control" required>
        </div>
        <div class="form-group">
            <label for="age" class="control-label">Age:</label>
            <input type="number" id="age" name="age" class="form-control" required>
        </div>
        <div class="form-group">
            <label for="wish" class="control-label">Wishes:</label>
            <select id="wish" name="wish" class="form-control" multiple required>
                @foreach($wishes as $wish)
                    <option value="<?php echo $wish->id; ?>"><?php echo $wish->name; ?></option>
                @endforeach
            </select>
        </div>
        <input type="submit" class="btn btn-primary" value="Inserir">
        <input type="hidden" name="_token" value="{{csrf_token()}}">
    </form>
</div>
@stop
@extends('layouts.master'))
@节(“内容”)
阿迪西奥纳尔乌马新库索酒店
这是一种信息,这是一种诅咒。

诺姆: 年龄: 祝愿: @foreach($wish as$wish) 如果你学习,你会找到其他方法:

这是回答问题的简单方法:

 public function store(Request $dados) {
    $friend = Friend::create($dados->all());
    foreach ($dados->wish as $wish) {
        $friend->wishes()->attach($wish);//wishes is the method name that you define in model
    }
    if(is_null($friend)) {
        return redirect()->route("friend.index")->withErrors("Erro ao criar curso. Por favor, tente novamente.");  
    }
    else {
        return redirect()->route("friend.index")->with("Curso inserido com sucesso!");
    }
当然,当您的输入是多个时,您应该将表单输入更改为数组选择
name=“wish[]”

<div class="form-group">
    <label for="wish" class="control-label">Wishes:</label>
    <select id="wish" name="wish[]" class="form-control" multiple required>
        @foreach($wishes as $wish)
            <option value="<?php echo $wish->id; ?>"><?php echo $wish->name; ?></option>
        @endforeach
    </select>
</div>

祝愿:
@foreach($wish as$wish)

这没用?你能帮我做点别的吗?
<div class="form-group">
    <label for="wish" class="control-label">Wishes:</label>
    <select id="wish" name="wish[]" class="form-control" multiple required>
        @foreach($wishes as $wish)
            <option value="<?php echo $wish->id; ?>"><?php echo $wish->name; ?></option>
        @endforeach
    </select>
</div>