Laravel 5.2 - How to view @foreach in View, only logs referring to the authenticated user?

Asked

Viewed 475 times

2

I have a record table with a field id_user, this table has data entered by some system user. I need that when the user authenticates in the system, only see the records that he created. That is, the records that have the id_user = id 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
  • you could put your Model? you could make available what you have done so far? Eloquent or DB?

  • Thank you for the reply Virgilio. Follow the codes. My View is bringing all the records from the table webinars.

  • I already made an answer!

1 answer

2


In his controller in that line:

Webinar::all();

change:

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

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

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'));
    }
}
  • Error running view: Badmethodcallexception in C: xampp www app-dlp.com vendorLaravel framework src Illuminate Database Query Builder.php line 2450: Call to Undefined method Illuminate Database Query Builder::all()

  • @Brunooliveira I’m so sorry when you do where the method to bring everyone changes from all() for get(), I’ve already done the editing, because, it’s the builder what’s going on

  • 1

    Thank you so much!! It worked perfect. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.