почему выборка данных не работает в блейде, но работает, а dd? - PullRequest
1 голос
/ 24 марта 2020

просмотр файла блейда

@extends('layouts.app') @section('content')

<h2>Mac Address</h2>
<div class="container">
    <div>
        <a href={{ route( 'macAddress.create')}} class="btn btn-warning" style="float-left">ADD NEW MAC ADDRESS</a>
    </div>

    <br>
    <div style="float:left">
        <table class="table table-stripped">
            @if(count($macs) > 0)
            <tr>
                <th scope="col">#</th>
                <th scope="col">Mac Address</th>
                <th scope="col">Assigned Room</th>
                <th scope="col">Created Date</th>
                <th scope="col">Updated Date</th>
                <th scope="col">Actions</th>
            </tr>
            @foreach($macs as $mac)
            <tr>
                <th scope="col">{{$mac->id}}</th>
                <th scope="col">{{$mac->address}}</th>
                {{dd($mac->hotelRooms->roomNumber)}};
                <th scope="col">{{($mac->hotelRooms->roomNumber)}}</th>
                <th scope="col"><small>{{$mac->created_at}}</small></th>
                <th scope="col"><small>{{$mac->updated_at}}</small></th>
                <td scope="col">
                    <div class="btn-group  btn-group-sm">
                        <a href="/macAddress/{{$mac->id}}/edit" class="btn btn-primary btn-sm">
                            <i class="fa fa-edit"></i></a>
                        <form action="{{ route('macAddress.destroy', $mac->id)}}" method="POST">
                            @csrf {{method_field('DELETE')}}
                            <a type="submit" class="btn btn-danger btn-sm">
                                <i class="fa fa-times"></i></a>
                        </form>
                    </div>
                </td>
            </tr>
            @endforeach
        </table>
    </div>
    @else
    <p>No Mac Address found</p>
    @endif
</div>
@endsection

файл модели

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\HotelRooms;
use App\MacAddress;

class MacAddress extends Model
{
    public $primarykey = 'id';
    public $foreignkey = 'roomId';
    protected $fillable = [
        'address','roomId',
    ];

    public function hotelRooms(){
        return $this->belongsTo('App\HotelRooms','roomId');
    }
   }
?>

файл контроллера

    <?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Hotels;
use App\HotelRooms;
use App\User;
use App\Role;
use App\MacAddress;
use Gate;
use DB;
use Illuminate\Support\Facades\Auth;
use Validator;

class HotelsController extends Controller
{   
    public function filterHotelByMac(Request $request){
        $macSent = $request->get('macSent');
        $roomId = DB::table('mac_addresses')->where('address',$macSent)->value('roomId');
        $hotelId = DB::table('hotel_rooms')->where('id', $roomId)->value('hotelId');
        $hotels = Hotels::where('id',$hotelId)->get();
        return view('hotels.index')->with('hotels', $hotels);
    }

    public function index()
        {
            $userId = Auth::user()->roleId;
            $role = Role::find(2)->id;
            $hotelId = Auth::user()->hotelId;

            if($userId == $role){
                $hotels=Hotels::where('id',$hotelId)->get();
                return view('hotels.index')->with([
                    'hotels', $hotels
                    ]);
            }else{ 
                $hotels = Hotels::all();
                return view('hotels.index')->with('hotels', $hotels);
            }
        }
   }
?>

Маршрут

Маршрут :: get ('hotels / filterHotelByMa c', 'HotelsController@filterHotelByMac'); Route :: resource ('hotels', 'HotelsController');

Ошибка

Фасад \ Ignition \ Exceptions \ ViewException Попытка получить свойство 'roomNumber' необъекта (представление: D: \ LaravelProjects \ HotelMgmt \ resources \ views \ macAddress \ index.blade. php) http://localhost: 8000 / macAddress

результат дд

Ма c Адрес ДОБАВИТЬ НОВЫЙ МА C АДРЕС

"PAG101"

Ма c Адрес

...