How to change default format at created_at and updated_at value laravel

In your Post model add two accessor methods like this: public function getCreatedAtAttribute($date) { return Carbon\Carbon::createFromFormat(‘Y-m-d H:i:s’, $date)->format(‘Y-m-d’); } public function getUpdatedAtAttribute($date) { return Carbon\Carbon::createFromFormat(‘Y-m-d H:i:s’, $date)->format(‘Y-m-d’); } Now every time you use these properties from your model to show a date these will be presented differently, just the date without the time, for example: … Read more

Synchronizing a one-to-many relationship in Laravel

Unfortunately there is no sync method for one-to-many relations. It’s pretty simple to do it by yourself. At least if you don’t have any foreign key referencing links. Because then you can simple delete the rows and insert them all again. $links = array( new Link(), new Link() ); $post->links()->delete(); $post->links()->saveMany($links); If you really need … Read more

What is the difference between destroy() and delete() methods in Laravel?

destroy is correct method for removing an entity directly (via object or model). Example: $teetime = Teetime::where(‘date’, ‘=’, $formattedDate)->firstOrFail(); $teetime->destroy(); delete can only be called in query builder Example: $teetime = Teetime::where(‘date’, ‘=’, $formattedDate)->delete(); From documentation: Deleting An Existing Model By Key User::destroy(1); User::destroy(array(1, 2, 3)); User::destroy(1, 2, 3); Of course, you may also run … Read more

Laravel Eloquent Relations: ->latest()

latest() is a function defined in Illuminate\Database\Query\Builder Class. It’s job is very simple. This is how it is defined. public function latest($column = ‘created_at’) { return $this->orderBy($column, ‘desc’); } So, It will just orderBy with the column you provide in descending order with the default column will be created_at.

Laravel Eloquent LEFT JOIN WHERE NULL

This can be resolved by specifying the specific column names desired from the specific table like so: $c = Customer::leftJoin(‘orders’, function($join) { $join->on(‘customers.id’, ‘=’, ‘orders.customer_id’); }) ->whereNull(‘orders.customer_id’) ->first([ ‘customers.id’, ‘customers.first_name’, ‘customers.last_name’, ‘customers.email’, ‘customers.phone’, ‘customers.address1’, ‘customers.address2’, ‘customers.city’, ‘customers.state’, ‘customers.county’, ‘customers.district’, ‘customers.postal_code’, ‘customers.country’ ]);