尝试获取非对象laravel的属性“ nom_matiere”向我显示此错误

时间:2019-05-21 11:48:51

标签: laravel

我想在此页面中显示Matiere的名称,但他向我显示此错误试图获取非对象的属性'nom_matiere'

我在注释页面中进行选择以选择物料,然后在此处进行注释,这可以正常工作,但是当我在表格中显示物料名称时,会提示我错误

型号说明

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Note extends Model
 {
 protected $fillable = ['note'];


 public function matieres() 
  {
   return $this->belongsToMany(Matiere::class);
    }

}

模型材料

   <?php

    namespace App;

   use Illuminate\Database\Eloquent\Model;

    class Matiere extends Model
    {
    protected $fillable = ['nom_matiere','coef'];


     public function notes() {
    return $this->belongsToMany(Note::class);
   }


  }

我的控制器

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Note;
use App\Matiere;


class NoteController extends Controller
{

public function index()
{

      $notes = Note::paginate(5);
      $matieres = Matiere::all();
    return view('admin.notes',compact('notes','matieres'));
}



public function store(Request $request)
{
    Note::create($request->all());




     session()->flash('success',' cette note  a été enregistré avec succés');


       return redirect()->back();
}



public function update(Request $request, $id)
{
    $note = Note::findOrFail($request->note_id);
     $note = Matiere::findOrFail($request->note_id);

    $note->update($request->all());
    session()->flash('success','cette note a été modifié avec succés');

   return redirect()->back();
}


public function destroy(Request $request)
{
    $note = Note::findOrFail($request->note_id);
    $note->delete();
    session()->flash('success','cette note a  été supprimé avec succés');
   return redirect()->back();
}

}

我的观点

          <section id="no-more-tables">
            <table class="table table-bordered table-striped table-condensed cf">
              <thead class="cf">
                <tr>
                <th>id-note</th>
                  <th>La note</th>
                  <th>nom matiere</th>
                 <th>les actions</th>
                </tr>
              </thead>
              <tbody>
                @foreach($notes as $note)
                  <tr>

                  <td class="numeric"  data-title="id-note" >{{$note->id}}</td>
                  <td class="numeric"  data-title="Nom">{{$note->note}}</td>

                   <td class="numeric"  data-title="Nom">{{$note->matiere->nom_matiere}}</td>

                   <td>
                        <button href="#editEmployeeModal" class="btn btn-theme"  data-target="#editEmployeeModal "data-mynote="{{$note->note}}"   "data-mymatiere="{{$note->nom_matiere}}" data-catid={{$note->id}}  class="edit" data-toggle="modal"  ><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i> </button>
                        <button href="#deleteEmployeeModal" class="btn btn-theme" data-target="#deleteEmployeeModal" data-catid={{$note->id}} class="delete" data-toggle="modal" > <i class="material-icons" data-toggle="tooltip" title="Delete">&#xE872;</i> </button>
                    </td>

                </tr>
              </tbody>
              @endforeach

            </table>
            <div class="text-center">
              {{ $notes->links() }}
        </div>
            <div class="clearfix">

            <div class="hint-text">Affichage de  <b>5</b> sur <b>25</b> entrées</div>

           <div id="addEmployeeModal"  href="create" class="modal fade">
<div class="modal-dialog">
  <div class="modal-content">
    <form   action="{{route('notes.store')}}" method="post">
      {{csrf_field()}}
      <div class="modal-header">            
        <h4 class="modal-title">Ajouter note</h4>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      </div>
      <div class="modal-body">          
        <div class="form-group">
          <label>La note</label>
          <input type="text"  id="note" name="note" class="form-control" required>
        </div>



    </div>
    <div class="form-group select">
    <select name="matiere_id">
      <option value="">--selectionner la mtiére svp --</option>

    @foreach($matieres as $matiere)
        <option value="{{ $matiere->id }}">{{ $matiere->nom_matiere }}</option>
    @endforeach  
  </select> 
  </select>
</div>

      <div class="modal-footer">
        <input type="button" class="btn btn-default" data-dismiss="modal" value="Annuler">
        <input type="submit" class="btn btn-success" value="Ajouter">
      </div>
    </form>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

您在视图中呼叫$note->matiere,但是这种关系称为matieres,这将是一个集合。

代替:

<td class="numeric" data-title="Nom">{{ $note->matiere->nom_matiere }}</td>

您需要:

<td class="numeric" data-title="Nom">
    @foreach ($note->matieres as $matiere)
        {{ $matiere->nom_matiere }}
    @endforeach
</td>

但是,我确实怀疑您设置的关系有误。您已将Note-> Matiere关系设置为belongsToMany,这意味着您需要一个数据透视表来包含FK关系。仅当您的Note是许多Matiere的孩子时,才使用此选项。

听起来好像您的人际关系设置错误,但是如果没有真正了解您要做什么,很难告诉您确切需要做什么。

单独的问题,但是在您的update()类方法中,您还得到了:

$note = Note::findOrFail($request->note_id);
$note = Matiere::findOrFail($request->note_id);

$note->update($request->all());

这意味着您实际上是在更新Matiere模型而不是Note模型。