Consulting in Eloquent

Asked

Viewed 21 times

1

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

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);
  • I suggest you take a look at the documentation of the eloquent/Aravel https://laravel.com/docs/master/eloquent. In advance, the find function has as an integer that would be the id (Event::find(1)) defined in its table or an array of integers (Event::find([1, 2, 3])).

  • Pass an id(integer) instead of the string... if the id you want to search for is 1 it would be Event::find(1);

  • My wish would be a variable, which would contain the user id that is currently logged in.

  • @Noscin Improve your question, what do you really want to do? Which variable will be used? The code is complete?

  • Thanks for trying to help me in the query I managed to resolve by putting $data = Event::Where('userid', $user)->get();

1 answer

1


Well I usually create a little differently this, I find it more intuitive and easy to understand and manipulate, you can use as you think best:

Add the uses of the classes at 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 the Models, and attribute them to __construct:

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

class HomeController extends Controller {
    private $event;

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

This way you can already access the information of Event using their methods all(), find() and related... Note that we have added the use Auth; at the beginning of the file, with this we can access information about the authentication 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 the use Auth; at the beginning and accessing its attributes along its Controller through the Auth::user()->id.

  • 1

    Thanks for the tip Darlei,.

Browser other questions tagged

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