Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shiva's avatar
Level 5

Getting an error I'm not supposed to

I have 2 views and both of them have an iframe in it but different routes. The problem I'm getting is that I'm getting this error in my iframe

ErrorException in ClientsController.php line 233: Trying to get property of non-object

but that is the wrong controller. The view I'm working on is this my show.blade.php

    {!!  $stories->title !!}
    {!!  $stories->content !!}

    <?php
        $image = getImagesArray($stories->image);
        $id = $stories->id;
    ?>

    @if(!empty($image))
        @foreach($image as $image)
            <iframe src="{!! URL::to('authors_image/'.$id.'/'.$image) !!}" width="100%" height="600"></iframe>
        @endforeach
    @endif 

and my route for the iframe url is this

Route::get('authors_image/{id}/{image}', [
        'uses' => 'StoryController@viewStory',
        'as' => 'view.viewStory',
    ]);

and the viewStroy method in my StoryController

public function viewStory($id, $image){
        echo "This is a pdf story";
    }

but it keeps getting the clientController. This is the order of my routes


Route::group(['middleware' => ['accountActivated']], function(){

    Route::get('authors_image/{id}/{image}', [
        'uses' => 'StoryController@viewStory',
        'as' => 'view.viewStory',
    ]);

});

Route::group(['middleware' => ['clientAuth']], function(){

    Route::get('authors_image/{slug}/{f}', [
        'uses' => 'clientsController@pdfStory',
        'as' => 'slug.pdfStory',
    ]);

});

and this is the view for the client section

@foreach($stories as $story)
        <p>{!! $story->content !!}</p>

        <?php
            $image = getImagesArray($story->image);
        ?>

        @if(!empty($image))
            @foreach($image as $f)
                <iframe src="{!! URL::to('authors_image/'.$story->slug.'/'.$f) !!}" width="100%" height="600"></iframe>
            @endforeach
        @endif
    @endforeach
0 likes
9 replies
vipin93's avatar

what's what's your relationship and check also your middlware

Shiva's avatar
Level 5

@vipin93 - I have 2 middlewares this is them My ClientAuth.php

public function handle($request, Closure $next, $guard = 'client_auth')
    {
        if(!Auth::guard($guard)->check()){
            return redirect()->to('client/login')->with('success', 'Denied was a success');
        }
        
        return $next($request);
    }

My AccountActivated

public function handle($request, Closure $next, $guard = 'authors_admin')
    {
        if(!Auth::guard($guard)->check()){
            return redirect()->to('login')->with('success', 'Denied was a success');
        }
        return $next($request);
    }

This is my relationship in my Story.php

public function author(){
        return $this->belongsToMany('App\Modules\Authors\Models\Author', 'author_story', 'story_id', 'author_id');
    }

My Author.php

public function story(){
        return $this->belongsToMany('App\Modules\Authors\Models\Story', 'author_story', 'author_id', 'story_id');
    }
Shiva's avatar
Level 5

This is my StoryController

<?php
namespace App\Modules\Authors\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Modules\Authors\Models\Story;
use App\Modules\Authors\Models\Author;
use Illuminate\Support\Facades\Input;
use Validator;
use DB;

class StoryController extends Controller
{
    public function index()
    {
        $stories = Story::all();
        return view('authors::authors.stories.index', compact('stories'));
    }

    public function create()
    {
        $author_id = auth()->guard('authors_admin')->user()->id;
        $author_name = auth()->guard('authors_admin')->user()->name;
        $author_surname = auth()->guard('authors_admin')->user()->surname;
        $author = $author_name.' '.$author_surname;
        $menu_options = Author::pluck('name', 'id');

        return view('authors::authors.stories.create', compact('author', 'author_id', 'menu_options'));
    }

    public function store()
    {
        $stories = new Story();
        $input = Input::all();
        $validation = Validator::make($input, Story::$rules);

        if($validation->fails())
        {
            return redirect()->route('stories.create')
                            ->withInput()
                            ->withErrors($validation)
                            ->with('message', 'There were validation errors');
        }

        if($validation->passes())
        {
            $stories->title = Input::get('title');
            $stories->content = Input::get('content');
            $stories->type = Input::get('type');
            $stories->image = Input::get('image');
            $slug = str_slug($stories->title);
            $stories->slug = $slug;
            $authorId = (array) array_get($input, 'author_id');
            $stories->fill($input)->save();
            $stories->author()->attach($authorId);
            $stories = Story::all();

            return view('authors::authors.stories.index', compact('stories')); 
        }
    }

    public function edit($id)
    {
        $stories = Story::find($id);
        $author_id = auth()->guard('authors_admin')->user()->id;
        $author_name = auth()->guard('authors_admin')->user()->name;
        $author_surname = auth()->guard('authors_admin')->user()->surname;
        $author = $author_name.' '.$author_surname;

        if(is_null($stories))
        {
            return redirect()->route('stories.edit');
        }

        return view('authors::authors.stories.edit', compact('stories', 'author', 'author_id'));
    }

    public function update($id)
    {
        //Gets all the input in the fields in the form
        $input = Input::all();

        //Checks the input fields against the validation rules in the Frame model
        $validation = Validator::make($input, Story::$rules);

        //If the validation fails the a message will pop up saying that there was validation errors
        if($validation->fails())
        {
            return redirect()->route('stories.edit')
                                ->withInput()
                                ->withErrors($validation)
                                ->with('message', 'There were validation errors');
        }

        //If the validation passes then it gets saved to the database
        if($validation->passes())
        {
            $stories = Story::FindOrFail($id);
            $stories->title = Input::get('title');
            $stories->content = Input::get('content');
            $stories->image = Input::get('image');
            $stories->type = Input::get('type');
            $slug = str_slug($stories->title);
            $stories->slug = $slug;
            $stories->update();

            return redirect()->route('stories.index', $id);
        }
    }

    public function destroy($id)
    {
        //This deletes the frame
        Story::find($id)->delete();
        $stories = Story::all();

        return view('authors::authors.stories.index', compact('stories'));
    }

    public function show($id)
    {
        $stories = Story::find($id);

        return view('authors::authors.stories.show', compact('stories'));
    }

    public function viewStory($id, $image){
        echo "This is a pdf story";
    }

}

and this is my ClientsController

<?php

namespace App\Modules\Authors\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use App\Modules\Menus\Models\Menu;
use App\Modules\Authors\Models\Client;
use App\Modules\Authors\Models\Story;
use App\Modules\Authors\Models\Comment;
use Auth;
use Mail;
use DB;
use Response;

class ClientsController extends Controller
{
    public function index()
    {
        // Displays all users
        $clients = Client::all();
        return view('authors::authors.clients.index', compact('clients')); //call the module first and then the view
    }

    public function create(){
        return view('authors::authors.clients.create');
    }

    public function store()
    {
        $clients = new Client();

        //Gets all the input in the fields in the form
        $input = Input::all();

        //Checks the input fields against the validation rules in the User model
        $validation = Validator::make($input, Client::$rules);

        //If the validation fails the a message will pop up saying that there was validation errors
        if($validation->fails()){
            return redirect()->route('clients.create')
                    ->withInput()
                    ->withErrors($validation)
                    ->with('message', 'There were validation errors');
        }

        //If the validation passes then an email is sent to the user
        if($validation->passes()){
            // dd(Config::get('mail'));

            $clients->name = Input::get('name');
            $clients->email = Input::get('email');
            $clients->password = bcrypt(Input::get('password'));

            //Gets the password that was generated from the User model
            

            //Takes the generated password and hashes it to make it secure
            // $clients->password = bcrypt($password);

            $data = array(
                    'name' => Input::get('name'),
                    'email' => Input::get('email'),
                    'password' => Input::get('password'),
                );

            // echo "<PRE>";
            // print_r($data);
            // die();

            // Sends the data into an email to be sent off
            Mail::send('templates::emails.client', $data, function($message){
                $message->to(Input::get('email'), Input::get('name'))->subject('Your login details');
            });

            //Saves the user
            $clients->save();
            $clients = Client::all();
            return view('authors::authors.clients.index', compact('clients'))->with('success', 'Email to client has been sent.');
        }
    }

    public function edit($id)
    {
        $clients = Client::find($id);

        if(is_null($clients))
        {
            return redirect()->route('clients.edit');
        }

        return view('authors::authors.clients.edit', compact('clients'));
    }

    public function update($id)
    {
        $input = Input::all();
        $validation = Validator::make($input, Client::$rules);

        if($validation->fails()){
            return redirect()->route('clients.edit')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors');
        }

        if($validation->passes()){
            $clients = Client::FindOrFail($id);
            // $clients->password = bcrypt($password);

            $data = array(
                    'name' => Input::get('name'),
                    'email' => Input::get('email'),
                    $clients->password = bcrypt(Input::get('password'))
                );

            // echo "<PRE>";
            // print_r($data);
            // die();

            $clients->update($data);
            return redirect()->route('clients.index', $id);
        }
    }

    public function destroy($id)
    {
        //This deletes the frame
        Client::find($id)->delete();
        $stories = Client::all();
        return view('authors::authors.clients.index', compact('stories'));
    }

    public function showLogin()
    {
        //Gets the login view from app/modules/users/views
        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
        return view('authors::clients.login', compact('menus_child')); //call the module first and then the view
    }

    public function doLogin(Request $request){
       
        if (auth('client_auth')->attempt(array('email' => $request->input('email'), 'password' => $request->input('password'))))
        {
            return redirect()->route('client.dashboard');
        }else{
            return back()->with('error','your username and password are wrong.');
        }
    }

    public function doLogout(){
        //Allows the user to log out
        Auth::logout();
        return redirect()->route('client.showLogin');
    }

    public function clientDashboard(){
        // echo "Client Dashboard";
        $stories = Story::where('type', 'private')->get();
        return view('authors::clients.index', compact('stories'));
    }

    public function privateSlug($slug){
        $stories = Story::where('slug', $slug)->get();
        $slug = $slug;

        $story = Story::with('author')->where('slug', $slug)->first();
        // $name = $story->author->first()->email;
        $name = $story->author->first()->name;
        $surname = $story->author->first()->surname;

        $comment = Story::with('comment')->where('slug', $slug)->first();
        $test = $comment->comment->toArray();

        return view('authors::clients.single-story', compact('stories', 'slug', 'test', 'name', 'surname'));
    }

    public function privateComment(Request $request, $slug)
    {
     
        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
        $stories = Story::where('slug', $slug)->get();

        $comments = new Comment();

        $input = Input::all();

        $validation = Validator::make($input, Comment::$rules);

        if($validation->fails())
        {
            return redirect()->route('')
                            ->withInput()
                            ->withErrors($validation)
                            ->with('message', 'There were validation errors');
        }

        if($validation->passes())
        {
            $comments->name = Input::get('name');
            $comments->comment = Input::get('comment');

            $storyId = (array) array_get($input, 'story_id');


            $data = array(
                        'name' => Input::get('name'),
                        'comment' => Input::get('comment')
                    );

            Mail::send('templates::emails.comment', $data, function($message){
                $message->to('[email protected]', Input::get('name'))->subject('A new comment was added');
            });

            $comments->fill($input)->save();
            $comments->story()->sync($storyId);
            $comments = Comment::all();
            return view('open::public.single-story', compact('menus_child', 'stories', 'slug'));
        }
    }

    public function pdfStory($slug, $f){

        $test = Story::all();

        $gh = DB::table('stories')->select('image')->where('slug', $slug)->first();

        $test = getImagesArray($gh->image);

        if(!empty($test)){
            foreach($test as $f){
                $filename = $f;
            }
        }

        $image = 'authors_images/'.$filename;

        // echo "<PRE>";
        // print_r($file);
        // die();
        $path = public_path($image);

        return Response::make(file_get_contents($path), 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="'.$filename.'"'
        ]);
    }
}
Shiva's avatar
Level 5

Oh sorry. It's

$test = getImagesArray($gh->image);
vipin93's avatar
$tests = getImagesArray($gh->image);

dd($tests); ? // lets see whats u getting 
Shiva's avatar
Level 5

@vipin93 - after doing that my iframe is giving me a denied access message that I have in my clientAuth middleware. But it should as far as I know be refrencing the AccountActivated middleware

vipin93's avatar
$author_id = auth()->guard('authors_admin')->user()->id; //u can not use 

another guard just use

$author_id = Auth::id(); //current guard id 

Please or to participate in this conversation.