Post your problem here.
Laravel Nova Mastery relationship issue
Trying to get through the Laravel Nova series and am stuck on video #5. I get a column error even though I believe I have followed everything correctly. I did post over on that discussion but thought I might get some traction here.
https://laracasts.com/series/laravel-nova-mastery/episodes/5
Thanks for any assist you can provide bugsysha. Hopefully something deceptively simple.
I, like others have mentioned on their posts, am stuck at the relationship part between user and post tables (I am using client and project tables).
I have followed the video up to where Marcel gets added as the user. I have followed the comments which tell me what is missing is a pivot table (as mentioned by xeneas99). Also looked at rushughes git pages for this.
On my projects resource I see the client (client name not client id which is good!). When I edit to assign a client to the project I get the SQL error "unknown column" etc. etc. Rewind video, make sure all is well, etc. etc.
Been banging my head for a few days now. Someone out there will ask me for some pastes but I'll wait to see where you out there think I am going wrong. I have tried several relationship options, changed tables, changed id data types, dropped table an re-migrated with changes.
Hard to find a more helpful answer to what - to me - is a specific issue. I know once this works, like other successes, I'll cheer.
Please do not truncate the error. That is the only thing that can hint what is wrong.
Client model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Client extends Model { // Protect primary key public $primaryKey = 'client_id';
/**
* The relationship the Nova resource corresponds to
*
* [tk] 201022 - added while testing Client-Project relationship
*
* @return string
*/
public function projects()
{
return $this->hasMany(Projects::class);
}
}
Project model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model { // Protect primary key public $primaryKey = 'project_id';
/**
* The relationship the Nova resource corresponds to
*
* [tk] 201022 - added while testing Client-Project relationship
*
* @return string
*/
public function client()
{
return $this->belongsTo(Client::class);
}
}
Client resource:
namespace App\Nova;
use Illuminate\Http\Request; use Laravel\Nova\Fields\Gravatar; use Laravel\Nova\Fields\HasMany; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest;
class Client extends Resource { /** * The model the resource corresponds to * * @var string */ public static $model = \App\Client::class;
/**
* The group the resource corresponds to
*
* @var string
*/
public static $group = 'Client';
/**
* The single value that should be used to
* represent the resource when being displayed
*
* @var string
*/
public static $title = 'client';
/**
* The columns that should be searched
*
* @var array
*/
public static $search = [
'client',
];
/**
* Get the fields displayed by the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function fields(Request $request)
{
return [
ID::make(__('Client ID'), 'client_id')
->sortable(),
ID::make(__('Client Type ID'), 'client_type_id')
->sortable()
->rules('required'),
Text::make(__('Client'), 'client')
->sortable()
->rules('required', 'max:255'),
Text::make(__('Description'), 'client_desc')
->rules('required', 'max:255'),
HasMany::make('Projects')
];
}
/**
* Get the cards available for the request
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
Project resource:
namespace App\Nova;
use Illuminate\Http\Request; use Laravel\Nova\Fields\BelongsTo; use Laravel\Nova\Fields\Gravatar; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest;
class Project extends Resource { /** * The model the resource corresponds to * * @var string */ public static $model = \App\Project::class;
/**
* The group the resource corresponds to
*
* @var string
*/
public static $group = 'Project';
/**
* The single value that should be used to
* represent the resource when being displayed
*
* @var string
*/
public static $title = 'project';
/**
* The columns that should be searched
*
* @var array
*/
public static $search = [
'project',
];
/**
* Get the fields displayed by the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'project_id')
->sortable(),
BelongsTo::make(__('Client'), 'client')
->sortable()
->rules('required'),
Text::make(__('Project'), 'project')
->sortable()
->rules('required', 'max:255'),
Text::make(__('Description'), 'project_desc')
->rules('required', 'max:255'),
];
}
/**
* Get the cards available for the request
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
This seems to be exactly what happens in the video and I am mostly there just can't find the client_id column.
https://www.youtube.com/c/CleanCodeStudio
I was helped indirectly by Zach @ Clean Code Studio with his video on Nova relationships (https://www.youtube.com/watch?v=bSkfvbaYl7Q).
I noticed in his "BelongsTo" code he called out the ID after the table and guess what? Result I needed instantly.
So my code for my relationship now looks like:
Client model:
public function client() { return $this ->belongsTo(Client::class, 'client_id'); }
Project model:
public function project() { $this->hasMany(Project::class, 'project_id'); }
Please or to participate in this conversation.