Consulting in Eloquent

0

I have a script in laravel which in eloquent I need to put a condition in eloquent instead of selecting all as it is, I need to select it according to the id of the user.

I checked that I should use Event :: find instead of Event: all (), but it is returning error.

$events = [];
            $data = Event::all();
            if($data->count()) {
                foreach ($data as $key => $value) {
                    $events[] = Calendar::event(
                        $value->title,
                        true,
                        new \DateTime($value->start_date),
                        new \DateTime($value->end_date.' +1 day'),
                        null,
                        // Add color and link on event
                     [
                         'color' => '#ff0000',
                         'url' => '#',
                     ]
                    );
                }
            }
            $calendar = Calendar::addEvents($events);
    
asked by anonymous 19.10.2018 / 19:34

1 answer

1

Well I usually create it a bit differently, I find it more intuitive and easy to understand and manipulate, you can use as you see fit:

Add class uses to the beginning of the file:

namespace App\Http\Controllers;
use App\Event;
use Auth;

class HomeController extends Controller {
    ...

Create the variables that will be used to access Models , and assign them in __construct :

namespace App\Http\Controllers;
use App\Event;
use Auth;

class HomeController extends Controller {
    private $inscrito;

    public function __construct(Event $event){
         $this->event = $event;
    }
    ...

In this way you can already access the information of Event using its methods all() , find() and related ... Note that we add use Auth; to the beginning of the file, with this we can access authentication information at any time, for example:

public function testeFuncao(){
    $data = $this->event->find(Auth::user()->id);
    dd($data);
}

Or you can adapt what you already have by just adding use Auth; at the beginning and accessing its attributes along your Controller through Auth::user()->id .

    
19.10.2018 / 19:56