Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the created_at in pivot table in laravel

Im confused how to get the created_at column in my pivot table. I have exam_user table with created_at and updated_at column. Now, I have this code in my view:

@if($exam->users()->where('user_id', Auth::user()->id)->exists())
    {{ Auth::user()->assignments()->pivot->created_at }}
    <div class="alert alert-success">You have done with this exam.</div>
@else

Any idea for this?

like image 609
Mr. J Avatar asked May 30 '17 05:05

Mr. J


People also ask

What is pivot attribute in Laravel?

In laravel, When working in a many-many relationship, the user has to choose the intermediate table and it is called a pivot table in terms of Laravel. The pivot attribute communicates with this intermediate table in controllers or models in Laravel. The admin in laravel has to define the role of every other user.

How do I name a pivot table in Laravel?

Laravel's naming convention for pivot tables is snake_cased model names in alphabetical order separated by an underscore. So, if one model is Feature , and the other model is Product , the pivot table will be feature_product .

How do I create a pivot table migration in Laravel?

You have to run - php artisan make:migration create_project_user_table --create --table=project_user to create the pivot table migration file after that you can copy/paste code in this migration file and run migrate command to create pivot table in your database.


1 Answers

When setting up your relationships, you need to specify if that pivot table has timestamps.

 public function things() {
        return $this->belongsToMany('App\Thing')->withTimestamps();
    }

Edit:

try Auth::user()->assignments()->first()->pivot->created_at

like image 187
Jagrati Avatar answered Oct 11 '22 15:10

Jagrati