webfuelcode
3 weeks ago
155
2
Laravel

How to make it visible for non members

Posted 3 weeks ago by webfuelcode

A single post page shows error

Trying to get property 'id' of non-object

The error shows the line where if the statement is placed which @if (auth()->user()->id == $link->user_id) in the below file.

@extends('layouts.app')

@section('content')
    <div class="container">
        {{--Site messages--}}
        @include('partials.msg')
        {{--Site messages ends--}}
        <div class="clearfix">
            <a href="{{ route('link.index') }}" class="btn btn-warning float-left">List</a>
            <a href="{{ route('link.create') }}" class="btn btn-primary float-right">Add</a>
        </div>
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card border-0">
                    <h4 class="card-header">
                        {{ $link->title }}
                    </h4>
                    <div class="card-body">
                        <p class="card-text">
                            {{ $link->description }}
                        </p>
                        
                        <div class="card-text">
                            {{ $link->url }}
                        </div>
                        @if (auth()->user()->id == $link->user_id)
                            <div class="card-link float-right">
                    
                                <div class="actions form-inline">
                                    <a href="{{route('link.edit', $link->id)}}" class="btn btn-info btn-sm badge form-group m-1">Edit</a>

                                    <form action="{{route('link.destroy', $link->id)}}" method="POST" class="form-group">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger badge m-1">Delete</button>
                                    </form>
                                </div>
                                
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

Controller file is here

function __construct()
    {
        return $this->middleware('auth')->except('index', 'show');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $links = Link::paginate(5);
        return view('list', compact('links'));
    }

	public function show(link $link)
    {
        return view('single', compact('link'));
    }

Please sign in or create an account to participate in this conversation.