mardi 17 avril 2018

Laravel Eloquent How To Reterieve Records for the loggin user

I have created a page which shows the students of the current logged in teacher. The user can type in the search and get the students based on the search.

I wrote this function :

public function search(){
        $q = Input::get( 'q' );
        if($q != ""){
        $students = Students::where( 'user_id', '=', auth()->user()->id )->where( 'first_name', 'LIKE', '%' . $q . '%' )->orWhere ( 'subject', 'LIKE', '%' . $q . '%' )->orWhere ( 'class', 'LIKE', '%' . $q . '%' )->orWhere ( 'status', 'LIKE', '%' . $q . '%' )->paginate (15)->setPath ('');
        $pagination = $students ->appends ( array (
                    'q' => Input::get ( 'q' ) 
            ) );
        if (count ( $seq ) > 0)
            return view ( 'students' )->with( 'seq' , $students )->withQuery ( $q );
        }
            return view ( 'students' )->withMessage ( 'No Details found. Try to search again !' );
    }

But when the user type a search value and hit submit. All the students for all the users get displayed and not just the logged in user.

I did tried to put auth()->user()->id in every orWhere Clause , but it doesn't seems to work. Where do I place this , so only the logged in user will be able to view the students that belongs to him.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire