【问题标题】:Laravel Invalid datetime format: 1366 Incorrect integer value:Laravel 无效的日期时间格式:1366 不正确的整数值:
【发布时间】:2018-02-14 22:36:40
【问题描述】:

我有两个表,一个表“métier”和一个具有一对多连接的表“tâche”,一个“métier”有几个“tâche”。在我的表格“ajouter tâches”中,我有一个组合框,我从中选择与“tâche”相关的“métier”。 现在我只想用 libelle_metier 填充表 metier 中的组合,然后将我选择的 libelle_metier 的 ID 添加到表 tache 中,然后将其添加到 metier_id。有什么帮助吗?

模型指标

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class metier extends Model
{
public function metier()
{
return $this->hasMany(Tache::class);
}
}

模型指标

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class tache extends Model
{
    public function metier()
    {
        return $this->belongsTo(Metier::class);
    }

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


  }

tache.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}}">
                                {{$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

tache 控制器

<?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('metier_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');
}
}

【问题讨论】:

    标签: laravel


    【解决方案1】:

    如果我理解正确,您想通过 metier id 循环遍历 metiers 您需要更改

    tache.blade.php

     ...
     <option value="{{ $metier->id }}">
     ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-14
      • 2021-07-23
      • 2021-04-21
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      相关资源
      最近更新 更多