尝试在 laravel 中建立关系时出错尝试读取字符串上的属性“mapel”

问题描述 投票:0回答:1

当我尝试 Laravel 关系时,当我想在索引视图中它为空之前显示它时,但当我稍微更改它时,出现了这样的错误,我不明白尝试读取属性“mapel”挂在绳子上

这是我的代码

优健模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Ujian extends Model
{
    use HasFactory;

    protected $fillable = [
        'nis',
        'mapel',
        'nilai',
    ];

    public function siswa()
    {
        return $this->belongsTo(Siswa::class, 'nis', 'nis');
    }

    public function mapel()
    {
        return $this->belongsTo(Mapel::class, 'mapel', 'id');
    }
}

模型siswa

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Siswa extends Model
{
    use HasFactory;

    protected $fillable = [
        'nis',
        'nama',
    ];

   public function ujian()
{
    return $this->hasMany(Ujian::class, 'nis', 'nis');
}
}


模型地图

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Mapel extends Model
{
    use HasFactory;

    protected $fillable = [
        'mapel',
    ];

    public function ujian()
    {
        return $this->hasMany(Ujian::class, 'mapel', 'id');
    }
}

ujian控制器

<?php

namespace App\Http\Controllers;

use App\Models\ujian;
use Illuminate\Http\Request;
use App\Models\Siswa;
use App\Models\Mapel;

class UjianController extends Controller


{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $ujianList = Ujian::with('siswa', 'mapel')->get();
        return view('ujian.index', compact('ujianList'));
    }
    

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $siswaList = Siswa::all();
        $mapelList = Mapel::all();
        
        return view('ujian.create', compact('siswaList', 'mapelList'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'nis' => 'required',
            'mapel' => 'required',
            'nilai' => 'required',
        ]);
    
        Ujian::create([
            'nis' => $request->nis,
            'mapel' => $request->mapel,
            'nilai' => $request->nilai,
        ]);
    
        return redirect()->route('ujian.index')->with('success', 'Data ujian berhasil ditambahkan!');
    }

    /**
     * Display the specified resource.
     */
    public function show(ujian $ujian)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(ujian $ujian)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, ujian $ujian)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(ujian $ujian)
    {
        //
    }
}

index.blade.php 视图

@extends('layouts.template')

@section('content')
    <div class="card">
        <div class="card-body">
            <h1>Data Ujian</h1>
            @if (session('success'))
                <div class="alert alert-success">
                    {{ session('success') }}
                </div>
            @endif

            <table class="table">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Nis</th>
                        <th>Nama</th>
                        <th>Mapel</th>
                        <th>Nilai</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($ujianList as $index => $ujian)
                        <tr>
                            <td>{{ $index + 1 }}</td>
                            <td>{{ $ujian->siswa->nis }}</td>
                            <td>{{ $ujian->siswa->nama }}</td>
                            <td>{{ $ujian->mapel->mapel }}</td>
                            <td>{{ $ujian->nilai }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection

我不知道如何解决这个问题,请帮助我,请帮助我,请lsp

php laravel eloquent laravel-8 laravel-blade
1个回答
0
投票

您已定义相同的关系名称和字段名称。如果更改关系名称,它将被修复。

Ujian
模型中,更新关系:

public function mapelRelation()
{
    return $this->belongsTo(Mapel::class, 'mapel', 'id');
}

另外,更新刀片文件:

<td>{{ $ujian->mapelRelation->mapel }}</td>

这应该可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.