Skip to content

Commit

Permalink
Response
Browse files Browse the repository at this point in the history
  • Loading branch information
jxjj committed Oct 11, 2024
1 parent 3d3262d commit 1df438d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 6 deletions.
9 changes: 8 additions & 1 deletion app/Nova/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ class Question extends Resource
*
* @var string
*/
public static $title = 'id';
// public static $title = 'id';
public function title()
{
$text = strip_tags($this->text);

return str_limit($text, 50);
}

/**
* The columns that should be searched.
Expand Down Expand Up @@ -54,6 +60,7 @@ public function fields(NovaRequest $request)
Boolean::make('Anonymous'),
Boolean::make('Allow Multiple'),
BelongsTo::make('Current Session', 'current_session', Session::class),
BelongsTo::make('Folder'),
Code::make('Question Info', 'question_info')->json(),
DateTime::make('Created At')->sortable(),
DateTime::make('Updated At')->sortable(),
Expand Down
97 changes: 97 additions & 0 deletions app/Nova/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Code;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\HasOneThrough;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;

class Response extends Resource
{
/**
* The model the resource corresponds to.
*
* @var class-string<\App\Response>
*/
public static $model = \App\Response::class;

/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'id';

/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
];

/**
* Get the fields displayed by the resource.
*
* @return array
*/
public function fields(NovaRequest $request)
{
return [
ID::make()->sortable(),
BelongsTo::make('Session')->searchable(),
// BelongsTo::make('Question'),
HasOneThrough::make('Question', 'session', 'App\Nova\Question'),
BelongsTo::make('User')->searchable(),
Code::make('Response Info')->json(),
DateTime::make('Created At'),
DateTime::make('Updated At'),
DateTime::make('Deleted At'),
];
}

/**
* Get the cards available for the request.
*
* @return array
*/
public function cards(NovaRequest $request)
{
return [];
}

/**
* Get the filters available for the resource.
*
* @return array
*/
public function filters(NovaRequest $request)
{
return [];
}

/**
* Get the lenses available for the resource.
*
* @return array
*/
public function lenses(NovaRequest $request)
{
return [];
}

/**
* Get the actions available for the resource.
*
* @return array
*/
public function actions(NovaRequest $request)
{
return [];
}
}
9 changes: 9 additions & 0 deletions app/Nova/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Http\Requests\NovaRequest;

class Session extends Resource
Expand Down Expand Up @@ -42,6 +44,13 @@ public function fields(NovaRequest $request)
return [
ID::make()->sortable(),
BelongsTo::make('Question'),

// response count
Number::make('Response Count', function () {
return $this->responses->count();
})->sortable(),

HasMany::make('Responses'),
];
}

Expand Down
10 changes: 5 additions & 5 deletions app/Nova/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class User extends Resource
*
* @var string
*/
public static $title = 'name';
public static $title = 'email';

/**
* The columns that should be searched.
Expand Down Expand Up @@ -86,14 +86,14 @@ public function fields(NovaRequest $request)

MorphMany::make('Auths', 'authentications', AuthenticationLog::class),

DateTime::make('Created At')->sortable(),
DateTime::make('Updated At')->sortable(),
DateTime::make('Created At')->sortable()->onlyOnDetail(),
DateTime::make('Updated At')->sortable()->onlyOnDetail(),
DateTime::make('Last Login At', function () {
return $this->lastLoginAt();
})->sortable(),
})->sortable()->onlyOnDetail(),
Text::make('Last Login IP', function () {
return $this->lastLoginIP();
})->sortable(),
})->sortable()->onlyOnDetail(),
];
}

Expand Down
12 changes: 12 additions & 0 deletions app/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public function user()
return $this->belongsTo(User::class);
}

public function question()
{
return $this->hasOneThrough(
Question::class,
Session::class,
'id', // Foreign key on the sessions table
'id', // Foreign key on the questions table
'session_id', // Local key on the responses table
'question_id' // Local key on the sessions table
);
}

public function getQuestion(): Question
{
return $this->session->question;
Expand Down

0 comments on commit 1df438d

Please sign in to comment.