Add a custom attribute to a Laravel / Eloquent model on load?

I’d like to be able to add a custom attribute/property to an Laravel/Eloquent model when it is loaded, similar to how that might be achieved with RedBean’s $model->open() method. For instance, at the moment, in my controller I have: public function index() { $sessions = EventSession::all(); foreach ($sessions as $i => $session) { $sessions[$i]->available = … Read more

Laravel Migration Change to Make a Column Nullable

I created a migration with unsigned user_id. How can I edit user_id in a new migration to also make it nullable()? Schema::create(‘throttle’, function(Blueprint $table) { $table->increments(‘id’); // this needs to also be nullable, how should the next migration be? $table->integer(‘user_id’)->unsigned(); } 11 Answers 11

Get Specific Columns Using “With()” Function in Laravel Eloquent

I have two tables, User and Post. One User can have many posts and one post belongs to only one user. In my User model I have a hasMany relation… public function post(){ return $this->hasmany(‘post’); } And in my post model I have a belongsTo relation… public function user(){ return $this->belongsTo(‘user’); } Now I want … Read more

Eloquent Collection: Counting and Detect Empty

This may be a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(…)->get() is empty, as well as counting the number of elements. We are currently using !$result to detect empty result, is that sufficient? As for count($result), does it actually … Read more