मेरे पास एक asset_category टेबल (कॉलम 'asset_category_id', 'category') और एक asset टेबल (कॉलम 'asset_id', 'asset_category_id') और केवल 'asset_id' n 'asset_category_id' कॉलम के बजाय asset टेबल से (कॉलम 'asset_id', 'asset_category_id.category,') प्रदर्शित करना चाहते हैं।

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

एसेट.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');
    }
}

viewAsset.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
0
Nii 30 अक्टूबर 2017, 04:44

2 जवाब

सबसे बढ़िया उत्तर
  1. आपको @foreach को @endforeach के साथ समाप्त करना होगा
  2. आपको मॉडल में protected $table = 'table_name'; घोषित करना होगा क्योंकि डिफ़ॉल्ट रूप से लार्वा तालिका नाम को मॉडल के वर्ग नाम के बहुवचन रूप में उत्पन्न करता है
  3. इसे वापस करने का प्रयास करें:

    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');

0
Nyicip 30 अक्टूबर 2017, 05:23

आपके पास नामस्थान में Asset_category है। इसे बदलने का प्रयास करें:

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

इसके लिए:

return $this->belongsTo(Asset_category::class);
1
Jeff 30 अक्टूबर 2017, 05:20