"类型错误:传递给Model :: __ construct()的参数1必须是类型数组,给定字符串,在TechnicienController.php中调用

时间:2018-02-21 14:41:06

标签: laravel

从我的表单中我想填写表格技术人员和用户知道我有一对一的连接我把表格给你。有人会知道如何从我的表格中立刻填写表格。

form

型号1

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
public function technicien()
{
    return $this->hasOne('App\technicien');

}

use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'email', 'password','nom','prenom','tel','mobil','role',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

模型2

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class technicien extends Model
{

    public function zoneintervention()
{
    return $this->belongsToMany('App\zoneintervention','technicien_zone','technicien_id','zoneintervention_id');

}
    public function metier()
{
    return $this->belongsToMany('App\metier','technicien_metier','technicien_id','metier_id');

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

public function tarificationtache()
{
    return $this->hasMany(Tarificationtache::class);
}

}

创建.blade.php

@extends('Layouts/app')
@section('content')
@if(count($errors))
<div class="alert alert-danger" role="alert">
 <ul>
    @foreach($errors ->all() as $message)
     <li>{{$message}}</li>
        @endforeach
 </ul>
</div>
@endif
<div class="container">
    <div class="row"></div>
    <div class="col-md-12">
        <form action=" {{url ('technicien')  }}" method="post">
         {{csrf_field()}}
            <div class="form-group">
                <label for="">Nom</label>
                <input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') }}" required autofocus>

                            @if ($errors->has('nom'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('nom') }}</strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
                <label for="">Prenom</label>
                <input id="prenom" type="text" class="form-control" name="prenom" value="{{ old('prenom') }}" required autofocus>

                            @if ($errors->has('prenom'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('prenom') }}</strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
                <label for="">Telephone</label>
                <input id="tel" type="text" class="form-control" name="tel" value="{{ old('tel') }}" required autofocus>

                            @if ($errors->has('tel'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('tel') }}</strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
                <label for="">Mobile</label>
                <input id="mobil" type="text" class="form-control" name="mobil" value="{{ old('mobil') }}" required autofocus>

                            @if ($errors->has('mobile'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('mobil') }}</strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
                <label for="">Role</label>
                <input id="role" type="text" class="form-control" name="role" value="{{ old('role') }}" required autofocus>

                            @if ($errors->has('role'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('role') }}</strong>
                                </span>
                            @endif
            </div>
            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                <label for="">E-Mail Address</label>
                <input id="email" type="text" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

                            @if ($errors->has('email'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
               <label for="password">Password</label>

                        <div class="form-group">
                            <input id="password" type="password" class="form-control" name="password" value="{{ old('password') }}" required autofocus>

                            @if ($errors->has('password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password') }}</strong>
                                </span>
                            @endif
                        </div>
            </div>
            <div class="form-group">
               <label for="password">Confirm Password</label>
                        <div class="form-group">
                            <input id="password_confirmation" type="password_" class="form-control" name="password_confirmation" value="{{ old('password_confirmation') }}" required s>
                        </div>
            </div>
            <div class="form-group">
                <label for="zoneintervention">zoneintervention</label>
                <select multiple name="zoneintervention_id[]" id="zoneintervention" class="form-control" >

                        @foreach($zoneintervention as $zoneintervention)
                         <option value="{{ $zoneintervention->id }}">
                            {{$zoneintervention->code_postal}}
                         </option>
                        @endforeach
                </select>
            </div>
            <div class="form-group">
                <label for="">Moyenne Avis</label>
                <input type="text"  name ="moyenne_avis" class="form-control"value="{{old('moyenne_avis')}}">
            </div>
            <div class="form-group">
                <label for="">Etat</label>
                <input type="text"  name ="actif" class="form-control"value="{{old('actif')}}">
            </div>
            <div class="form-group">
                <input type="submit" value = "enregistrer" class="form-control btn btn-primary">
            </div>
        </form>
    </div>
</div>
@endsection

控制器

public function create()
{

    $zoneintervention = Zoneintervention::orderBy('id', 'desc')->get();
    $metier = metier::orderBy('libelle_metier', 'desc')->get();
    $tache = tache::orderBy('libelle_tache', 'desc')->get();
    $user = user::orderBy('id', 'desc')->get();
    return view('technicien.create')->with('zoneintervention', $zoneintervention)->with('metier', $metier)->with('tache', $tache)->with('user', $user);

}
/**
 * Store a newly created resource in storage.
 *
 * @param  
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $technicien ->user_id = new user('id');
    $user->nom = $request->input('nom');
    $user->prenom = $request->input('prenom');
    $user->tel = $request->input('tel');
    $user->mobil = $request->input('mobil');
    $user->role = $request->input('role');
    $user->email = $request->input('email');
    $user->password = $request->input('password');
    $user->save();
    $technicien = new technicien();
    $technicien->actif = $request->input('actif');
    $technicien->moyenne_avis = $request->input('moyenne_avis');
    $technicien->save();
    $technicien->zoneintervention()->attach($request->zoneintervention_id);
    $technicien->users()->attach($request->user_id);
    $technicien->metier()->attach($request->metier_id);
    $technicien->tache()->attach($request->metier_id);
    return redirect('technicien');

}

1 个答案:

答案 0 :(得分:0)

你不能这样做:

$technicien->user_id = new user('id');

首先需要创建用户,然后获取他的ID:

$user = new user();
$user->nom = $request->input('nom');
$user->prenom = $request->input('prenom');
$user->tel = $request->input('tel');
$user->mobil = $request->input('mobil');
$user->role = $request->input('role');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->save();

$technicien = new technicien();
$technicien->user_id = $user->id;
$technicien->actif = $request->input('actif');
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();
$technicien->zoneintervention()->attach($request->zoneintervention_id);
$technicien->users()->attach($request->user_id);
$technicien->metier()->attach($request->metier_id);
$technicien->tache()->attach($request->metier_id);