Php 数组到字符串的转换(SQL:insert-into-taches(libelle_-tache、Tarif、metier_-id、,

Php 数组到字符串的转换(SQL:insert-into-taches(libelle_-tache、Tarif、metier_-id、,,php,laravel,Php,Laravel,我有两个表,一个表“métier”和一个表“téche”,一个表有一对多连接,“métier”有几个“téche”。在我的形式“ajouter téches”中,我有一个组合框,从中选择与“téche”关联的“métier”。当我验证创建时,我得到了上面的错误。感谢您的帮助 创建blade.php @extends('Layouts/app') @section('content') @if(count($errors)) <div class="alert alert-danger" r

我有两个表,一个表“métier”和一个表“téche”,一个表有一对多连接,“métier”有几个“téche”。在我的形式“ajouter téches”中,我有一个组合框,从中选择与“téche”关联的“métier”。当我验证创建时,我得到了上面的错误。感谢您的帮助

创建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 ('tache')  }}" method="post">
         {{csrf_field()}}


            <div class="form-group">
                <label for="">Libelle Tache</label>
                <input type="text"  name ="libelle_tache" class="form-
control"value="{{old('libelle_tache')}}">
            </div>
            <div class="form-group">
                <label for="">Tarif</label>
                <input type="text"  name ="Tarif" class="form-
control"value="{{old('tarif')}}">
            </div>




            <div class="form-group">
                <label for="metier">metier</label>
                <select name="metier_id" id="metier" class="form-
control">

                        @foreach($metiers as $metier)
                         <option value="{{$metier->id}}">
                            {{$metier->libelle_metier}}
                         </option>
                        @endforeach
                </select>
            </div>




            <div class="form-group">
                <input type="submit" value = "enregistrer" 
class="form-control btn btn-primary">
            </div>
        </form>
    </div>
</div>


<script>

@endsection
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
class TacheController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $Listmetier=metier::orderBy('libelle_metier')->get();
    $Listtache=tache::all();
    return view('tache.index',['tache'=>$Listtache]);


}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{

    //$metiers = Costcenter::lists('libelle_metier', 'id');
    $metiers = Metier::orderBy('id', 'desc')->get();
    return view('tache.create')->with('metiers', $metiers);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $tache = new Tache();
    $tache ->libelle_tache =$request->input('libelle_tache');
    $tache ->Tarif =$request->input('Tarif');
    $tache ->metier_id =$request->input($tache->id);
    $tache->save();
    return redirect('tache');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $tache=Tache::find($id);
    return view('tache.edit',['libelle_tache'=>$metier],
['Tarif'=>$tache]);
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $tache =Tache::find($id);
    $tache->delete();

    return redirect('tache');
}
}
@extends('Layouts/app')
@节(“内容”)
@如果(计数($errors))
    @foreach($errors->all()作为$message)
  • {{$message}}
  • @endforeach
@恩迪夫 {{csrf_field()}} 诽谤性塔奇 塔里夫 梅蒂埃 @foreach($metiers as$metier) {{$metier->libelle_metier} @endforeach @端部
tachecontroller.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 ('tache')  }}" method="post">
         {{csrf_field()}}


            <div class="form-group">
                <label for="">Libelle Tache</label>
                <input type="text"  name ="libelle_tache" class="form-
control"value="{{old('libelle_tache')}}">
            </div>
            <div class="form-group">
                <label for="">Tarif</label>
                <input type="text"  name ="Tarif" class="form-
control"value="{{old('tarif')}}">
            </div>




            <div class="form-group">
                <label for="metier">metier</label>
                <select name="metier_id" id="metier" class="form-
control">

                        @foreach($metiers as $metier)
                         <option value="{{$metier->id}}">
                            {{$metier->libelle_metier}}
                         </option>
                        @endforeach
                </select>
            </div>




            <div class="form-group">
                <input type="submit" value = "enregistrer" 
class="form-control btn btn-primary">
            </div>
        </form>
    </div>
</div>


<script>

@endsection
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
class TacheController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $Listmetier=metier::orderBy('libelle_metier')->get();
    $Listtache=tache::all();
    return view('tache.index',['tache'=>$Listtache]);


}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{

    //$metiers = Costcenter::lists('libelle_metier', 'id');
    $metiers = Metier::orderBy('id', 'desc')->get();
    return view('tache.create')->with('metiers', $metiers);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $tache = new Tache();
    $tache ->libelle_tache =$request->input('libelle_tache');
    $tache ->Tarif =$request->input('Tarif');
    $tache ->metier_id =$request->input($tache->id);
    $tache->save();
    return redirect('tache');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $tache=Tache::find($id);
    return view('tache.edit',['libelle_tache'=>$metier],
['Tarif'=>$tache]);
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $tache =Tache::find($id);
    $tache->delete();

    return redirect('tache');
}
}

更改:

$tache ->metier_id = $request->input($tache->id);
$tache ->metier_id = $request->input('metier_id');
至:

$tache ->metier_id = $request->input($tache->id);
$tache ->metier_id = $request->input('metier_id');

请发布完整的错误stacktrace您正在使用
$tache->id
在保存之前,它还没有id。