Laravel Eloquent 关系:HasRelationships.php FatalErrorExexeption

Laravel Eloquent Relationship: HasRelationships.php FatalErrorExexeption

我有一个asset_category table(列'asset_category_id','category')和一个asset table(列'asset_id', 'asset_category_id') 并希望显示 asset table 中的('asset_id''asset_category_id.category,' 列)而不仅仅是'asset_id' n 'asset_category_id' 列。

Asset_CatoriesController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Asset_category;

class Asset_CategoriesController extends Controller
{
    public function asset_category(){
        $asset_categories = Asset_category::all();
        return view('category', ['asset_categories' => $asset_categories]);
    }

    public function add(Request $request){
        $this->validate($request, [
            'asset_category_id' => '',
            'category' => 'required'
        ]);
        $asset_categories =  new Asset_category;
        $asset_categories ->asset_category_id = $request->input('asset_category_id');
        $asset_categories ->category = $request->input('category');
        $asset_categories ->save();
        return redirect('/category') ->with('info', 'New Category Saved Successfully!');
    }

    public function update($id){
        $asset_categories = Asset_category::find($id);
        return view('update', ['asset_categories' => $asset_categories]);

    }

    public function edit(Request $request, $id){

        $this->validate($request, [
            'asset_category_id' => '',
            'category' => 'required'
        ]);
        $data = array(
            'category' => $request ->input('category')
        );
        Asset_category::where('asset_category_id', $id)->update($data);
        return redirect('/category') ->with('info', 'Category Updated Successfully!');
    }

    public function delete($id){
        Asset_category::where('asset_category_id', $id)
            ->delete();
        return redirect('/category') ->with('info', 'Category Deleted Successfully!');
    }
}

资产控制器

<?php

namespace App\Http\Controllers;

use App\Asset;
use App\Asset_category;
use App\Manufacturer;
use App\Department;



use Illuminate\Http\Request;

class AssetController extends Controller
{
    public function asset(){
        $assets = Asset::all();
       // return view::make('viewAsset')->with('assets', $assets);
        return view('viewAsset', ['assets' => $assets]);
    }

    public function manufacturer(){
        $manufacturers = Manufacturer::all();
        return view('asset', ['manufacturers' => $manufacturers]);
    }

    public function add(Request $request){
        $this->validate($request, [
            'asset_id' => '',
            'asset_category_id' => 'required',
            'manufacturer_id' => 'required',
            'department_id' => 'required',
        ]);
        $assets =  new Asset;
        $assets  ->asset_id  = $request->input('asset_id');
        $assets  ->asset_category_id = $request->input('asset_category_id');
        $assets  ->manufacturer_id = $request->input('manufacturer_id');
        $assets  ->department_id = $request->input('department_id');
        $assets  ->save();
        return redirect('/viewAsset') ->with('info', 'New Asset Saved Successfully!');
    }

    public function update($id){
        $assets = Asset::find($id);
        return view('updateAsset', ['assets' => $assets]);

    }

    public function edit(Request $request, $id){

        $this->validate($request, [
            'asset_id' => '',
            'asset_category_id' => 'required',
            'manufacturer_id'=> 'required',
            'department_id' => 'required'
        ]);
        $data = array(
            'asset_category_id' => $request ->input('asset_category_id'),
            'manufacturer_id' => $request ->input('manufacturer_id'),
            'department_id' => $request ->input('department_id')
        );
        Asset::where('asset_id', $id)->update($data);    
        return redirect('/viewAsset') ->with('info', 'Asset Updated Successfully!');
    }

    public function delete($id){
        Asset::where('asset_id', $id)
            ->delete();
        return redirect('/viewAsset') ->with('info', 'Asset Deleted Successfully!');
    }
}

Asset.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Asset_category;

class Asset extends Model
{
    protected $primaryKey = 'asset_id';

    public function category(){

        return $this->belongsTo('Asset_category');
        //$this->belongsTo('Asset_category');
        //Asset_category::where('asset_category_id', $this->asset_category_id)->first()->category;
    }

}

Asset_category.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Asset;

class Asset_category extends Model
{
    protected $primaryKey = 'asset_category_id';

    public function asset() {
        return $this->hasMany('Asset', 'asset_category_id');
    }
}

查看Asset.php

 @foreach($assets->all() as $asset)
 <tr>
    <td>{{ $asset->asset_id}}</td>
    <td>{{ $asset->category->category}}</td>

当我 运行 项目时,我得到一个 FatalErrorExeception 上面写着

Class 'Asset_category' not found in HasRelationships.php

您在命名空间中有 Asset_category。尝试改变这个:

return $this->belongsTo('Asset_category');

对此:

return $this->belongsTo(Asset_category::class);
  1. 您必须以 @endforeach
  2. 结束 @foreach
  3. 您必须在模型中声明 protected $table = 'table_name';,因为默认情况下 laravel 生成 table 名称为模型 class 名称的复数形式
  4. 尝试return这个:

    return $this->belongsTo('Asset_category');

    return $this->hasMany('Asset', 'asset_category_id');

对此:

return $this->belongsTo(Asset_category::class);

return $this->hasMany(Asset::class, 'asset_category_id');