Laravel 5.2 - How to display in @foreach in View, only records regarding the authenticated user?

2

I have a records table with a id_user field, in this table there are data entered by some users of the system. I need when the user authenticates in the system, only see the records that he created. That is, records that have id_user = id of the authenticated user.

Created Table

Schema::create('webinars', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('id_user')->unsigned();
            $table->foreign('id_user')->references('id')->on('users');
            $table->string('name', 255);
            $table->text('description');
            $table->dateTime('create_date');
            $table->time('time_duration');
            $table->timestamps();
});

Route

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

    Auth::routes();

    Route::get('/', 'HomeController@index');
    Route::get('/webinar/gerenciarwebinar', 'GwebinarController@index');
});

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Webinar;

class GwebinarController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $webinars = \App\Webinar::all();
        return view('webinar.gwebinar', compact('webinars'));
    }
}

Model

namespace App;    
use Illuminate\Database\Eloquent\Model;

class Webinar extends Model
{
    //
}

View

@foreach($webinars as $webinar)   
<tr>
    <td>{{ $webinar->name }}</td>
    <td>{{ $webinar->create_date }}</td>
    <td>{{ $webinar->time_duration }}</td>
    <td><a href="" type="button" class="btn btn-sm btn-secondary margin-inline">@lang('webinario.lang08')</a></td>
    <td><a href="" type="button" class="btn btn-sm btn-secondary margin-inline">@lang('webinario.lang09')</a></td>
    <td>
        <div class="dropdown margin-inline">
            <button type="button" class="btn btn-sm btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                @lang('webinario.lang10')
            </button>
            <ul class="dropdown-menu" aria-labelledby="" role="menu">
                <a class="dropdown-item" href="javascript: void(0)">
                <i class="left-menu-link-icon icmn-pencil2"><!-- --></i>&nbsp;&nbsp;&nbsp; @lang('webinario.lang12')</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item swal-btn-warning" href="javascript: void(0)"><i class="left-menu-link-icon icmn-bin"><!-- --></i>&nbsp;&nbsp;&nbsp;
                @lang('webinario.lang13')</a>
            </ul>
        </div>
    </td>
</tr>
@endforeach
    
asked by anonymous 02.11.2016 / 17:26

1 answer

2

On your% of_this line :

Webinar::all();

switch:

Webinar::where("id_user", Auth::user()->id)->get();

by specifying a controller filter, with the value of where ( Auth::user() ) that returns the logged-in user. (do not forget to use Auth::user()->id namespace , which has already been added in the code sample) and change from use Illuminate\Support\Facades\Auth; to all()

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Webinar;
use Illuminate\Support\Facades\Auth;

class GwebinarController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $webinars = Webinar::where("id_user", Auth::user()->id)->get();
        return view('webinar.gwebinar', compact('webinars'));
    }
}
    
02.11.2016 / 19:49