阻止错误Laravel为foreach()

时间:2018-02-13 13:54:10

标签: laravel

我有两张桌子,一张桌子“métier”和一张桌子“tâche”,有一对多连接,“métier”有几个“tâche”。在我的形式“ajoutertâches”中我想要一个组合框,我从中选择与“tâche”相关的“métier”。这些是我的表格以及我的进展和错误。

型号1

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


}

模型2

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class metier extends Model
{
 public function metier()
{
    return $this->hasMany(Tache::class);
}
}

控制器1     

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()
{
    $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');
}

/**
 * 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');
}
}

控制器2

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Metier;

class MetierController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $Listmetier=metier::all();
    return view('metier.index',['metier'=>$Listmetier]);
}

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

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $metier = new Metier();
    $metier ->libelle_metier =$request->input('libelle_metier');
    $metier->save();
    return redirect('metier');

}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show(metier $id)
{
    return view('metier.edit',['libelle_metier'=>$metier]);
}

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

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $metier=Metier::find($id);
    return view('metier.edit',['libelle_metier'=>$metier]);
}

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

    return redirect('metier');
}
}

查看

@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">

                        @foreach($metiers as $metier)

                         <option value="{{$metiers}}">
                            {{$metiers->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

1 个答案:

答案 0 :(得分:0)

您收到错误是因为您没有将$metiers传递给视图。因此,请将代码更改为:

return view('tache.create')->with('metiers', $metiers);