In my Laravel 8 application I two models, a User and Optout. My Optout model has a user_id column and a user is able to create an optout through a front-end form which populated this table with an entry with their user id.
On my User model when I try to create a relationship to get the optout I get an empty object instead of the data from my optout? Why? What am I missing?
/**
* The accessors to append to the model's array form.
*
* #var array
*/
protected $appends = [
'has_marketing_optout'
];
/**
* Determine if the user is currently subscribed or not
*
* #return bool
*/
public function getHasMarketingOptoutAttribute()
{
try {
return $this->hasOne(Optout::class, 'user_id');
} catch (\Exception $e) {
return false;
}
}
You don't need attributes for it. You need to create a relationship.
public function output()
{
return $this->hasOne(Optout::class);
}
Then you can access it like $user->output
If you want only one field from that relation you can define attribute.
/**
* The accessors to append to the model's array form.
*
* #var array
*/
protected $appends = [
'has_marketing_optout'
];
public function output()
{
return $this->hasOne(Optout::class);
}
public function getHasMarketingOptoutAttribute()
{
return $this->output->field_name;
}
Related
I can not delete a row using a simple eloquent query. Even when I am using eloquent can not get the data from DB. I am getting null. But, in DB query method at least I am getting data but can not delete then. Following is my code:
DB::transaction(function () use ($lead, $comment, $request) {
$lead->save();
$lead->comments()->save($comment);
if ($request->deleteAppointment) {
$calendarEvent = DB::table('calendar_events')->where('id', $request->appointmentId)->first(); // I am getting data here.
$calendarEvent = CalendarEvent::find($request->appointmentId); // But, here I am getting null, don't know why!
if ($calendarEvent != null) {
$calendarEvent->delete();
}
}
My goal is to get the data using Eloquent and then Delete from database.
update:
My Database Table
CalendarEvent.php model
class CalendarEvent extends Model
{
use SoftDeletes;
/**
* #var array
*/
protected $casts = [
'event_begin' => 'datetime',
'event_end' => 'datetime',
'options' => 'array',
];
/**
* #var array
*/
protected $guarded = [
'id',
];
/**
* #return mixed
*/
public function users()
{
return $this->morphedByMany(User::class, 'eventable');
}
/**
* #return mixed
*/
public function attendees()
{
return $this->morphedByMany(User::class, 'eventable')->withPivotValue('role', 'atendee');
}
/**
* #return mixed
*/
public function companies()
{
return $this->morphedByMany(Company::class, 'eventable')->withPivotValue('role', 'company');
}
/**
* #return mixed
*/
public function invitees()
{
return $this->morphedByMany(User::class, 'eventable')->withPivotValue('role', 'invitee');
}
/**
* #return mixed
*/
public function leads()
{
return $this->morphedByMany(Lead::class, 'eventable')->withPivotValue('role', 'lead');
}
}
Why not just:
CalendarEvent::where('id', $request->appointmentId)->delete();
Also, check the deleted_at column. If that is not null, then the select will return null, unless you add the ->withTrashed() method.
When using Eloquent objects, the SoftDelete trait is used, when using DB:: directly, then the SoftDelete trait is not used.
I have a relationship between work 'days' and projects of different types. So my 'days' record has a reference to my 'projects' table twice because I have two different types of projects called 'Series' and 'Event'.
In my 'days' resource I've created two fields as such:
BelongsTo::make('Series','series',Project::class)->sortable()->nullable(),
BelongsTo::make('Event','event',Project::class)->sortable()->nullable(),
What I'm trying to do is filter the projects by their types so I've created this:
public static function relatableProjects(NovaRequest $request, $query){
return $query->where('type', 'Series');
}
I've tried making relatableSeries and relatableEvents but they don't work. How can I make this connect to the fields correctly without having to create two separate tables for 'series' and 'events'.
The relatableQuery above winds up filtering both resource fields.
Because relatableQuery() is referencing a relatableModel() (so relatableProjects() references the Project model) I was able to create another model solely for the purpose of helping with this.
I created an Event model which references the same projects table and then was able to create a relatableEvents() method to use the where() filter query.
Note: I did have to also create an Event resource which references the Event model since this is how Nova works but was able to hide it from being accessed which you can find more information about here
See revised BelongsTo fields and new model below:
Day resource
/**
* Build a "relatable" query for the given resource.
*
* This query determines which instances of the model may be attached to other resources.
*
* #param \Laravel\Nova\Http\Requests\NovaRequest $request
* #param \Illuminate\Database\Eloquent\Builder $query
* #return \Illuminate\Database\Eloquent\Builder
*/
public static function relatableProjects(NovaRequest $request, $query){
return $query->where('type', 'Series');
}
/**
* Build a "relatable" query for the given resource.
*
* This query determines which instances of the model may be attached to other resources.
*
* #param \Laravel\Nova\Http\Requests\NovaRequest $request
* #param \Illuminate\Database\Eloquent\Builder $query
* #return \Illuminate\Database\Eloquent\Builder
*/
public static function relatableEvents(NovaRequest $request, $query){
return $query->where('type', 'Event');
}
/**
* Get the fields displayed by the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function fields(Request $request)
{
return [
ID::make()->hideFromIndex()->hideFromDetail()->hideWhenUpdating(),
BelongsTo::make('User','user',User::class)->sortable(),
BelongsTo::make('Budget','budget',Budget::class)->sortable()->nullable(),
BelongsTo::make('Series','series',Project::class)->sortable()->nullable(),
BelongsTo::make('Event','event',Event::class)->sortable()->nullable(),
DateTime::make('Last Updated','updated_at')->hideFromIndex()->readOnly(),
new Panel('Schedule',$this->schedule()),
new Panel('Time Entry',$this->timeEntries()),
];
}
Event model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'projects';
protected $casts = [
'starts_on' => 'date',
'ends_on' => 'date',
];
public function event(){
return $this->belongsTo('App\Event');
}
public function project(){
return $this->belongsTo('App\Project');
}
}
i know it is an old question but i was facing same problem with laravel nova belongsto field, in some resource i have a belongsto that relates to users but these should be of 'supervisor' role in another resource i hace a belongsto field relating to users but these should be of role 'guard', as laravel nova belongsto field just takes all users in both selects all users appeared and it seems nova belongsto field doe not have a way, or at least i did not find it to scope the query, so what i did was creating a php class named BelongstoScoped this class extends laravel nova field BelongsTo so i overwrote the method responsible of creating the query
<?php
namespace App\Nova\Customized;
use Laravel\Nova\Query\Builder;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Http\Requests\NovaRequest;
class BelongsToScoped extends BelongsTo
{
private $modelScopes = [];
//original function in laravel belongsto field
public function buildAssociatableQuery(NovaRequest $request, $withTrashed = false)
{
$model = forward_static_call(
[$resourceClass = $this->resourceClass, 'newModel']
);
$query = new Builder($resourceClass);
//here i chaned this:
/*
$query->search(
$request, $model->newQuery(), $request->search,
[], [], ''
);
*/
//To this:
/*
$query->search(
$request, $this->addScopesToQuery($model->newQuery()), $request->search,
[], [], ''
);
*/
//The method search receives a query builder as second parameter, i just passed the result of custom function
//addScopesToQuery as second parameter, thi method returns the same query but with the model scopes passed
$request->first === 'true'
? $query->whereKey($model->newQueryWithoutScopes(), $request->current)
: $query->search(
$request, $this->addScopesToQuery($model->newQuery()), $request->search,
[], [], ''
);
return $query->tap(function ($query) use ($request, $model) {
forward_static_call($this->associatableQueryCallable($request, $model), $request, $query, $this);
});
}
//this method reads the property $modelScopes and adds them to the query
private function addScopesToQuery($query){
foreach($this->modelScopes as $scope){
$query->$scope();
}
return $query;
}
// this method should be chained tho the field
//example: BelongsToScoped::make('Supervisores', 'supervisor', 'App\Nova\Users')->scopes(['supervisor', 'active'])
public function scopes(Array $modelScopes){
$this->modelScopes = $modelScopes;
return $this;
}
}
?>
In my users model i have the scopes for supervisors and guard roles like this:
public function scopeActive($query)
{
return $query->where('state', 1);
}
public function scopeSupervisor($query)
{
return $query->role('supervisor');
}
public function scopeSuperadmin($query)
{
return $query->role('superadmin');
}
public function scopeGuarda($query)
{
return $query->role('guarda');
}
So in the laravel nova resource i just included the use of this class
*remember the namespace depends on how you name your file, in my case i created the folder Customized and included the file there:
use App\Nova\Customized\BelongsToScoped;
In the fields in nova resource i used like this:
BelongsToScoped::make('Supervisor', 'supervisorUser', 'App\Nova\Users\User')
->scopes(['supervisor', 'active'])
->searchable()
So that way i could call the belongsto field in the nova resources which filter users depending on modle scopes.
I hope this helps someone, sorry if my English is not that good.
Using backpack for laravel (love it).
So, I have this piece of code every time an user stores data (FinanceCrudController):
$finance = $request->all();
if ($request->input('shouldPay') == 'Yes') {
Mail::to($request->user())->send(new NewBill($finance));
return parent::storeCrud();
}
else {
return parent::storeCrud();
}
And this is what my Mailable (NewBill) looks like:
class NewBill extends Mailable
{
use Queueable, SerializesModels;
/**
* The finance instance.
*
* #var Finance
*/
public $finance;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($finance)
{
//
$this->finance = $finance;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.newbill');
}
}
I can then pass the data from finance table by calling it out in newbill.blade.php like so:
{{ $finance['name'] }}
However, what I can't quite figure out is how to grab the relational data.
I'm using a Category table to handle categories. So, if I call the category on my blade view:
{{ $finance['category_id'] }}
I'm only gonna get the ID number for the field on the Category table.
How can I produce the actual category name on the email instead of the id of the field?
Any public property defined on your mailable class will automatically be made available to the view. So, you could do something like this:
public $finance;
public $category;
public function __construct($finance)
{
$this->finance = $finance;
}
public function build()
{
$this->category = \App\Category::where('id', $this->finance['category_id'])->first();
return $this->view('emails.newbill');
}
And then access to the category name with $category->name in the view.
https://laravel.com/docs/5.3/mail#view-data
Laravel newbie here, sorry if this is painfully obvious but I've been stuck on it for ages!
Objective: To mass-assign a Quote::create() database insertion with the full values from the form, plus set the User ID to the currently logged in user.
Problem: The user_id column is never written to the database. Every other column is, but user_id remains as 0.
I have of course tried adding user_id to the $fillable array, but I don't want it to be user-fillable - I want it to be set by Laravel's Auth::id() function.
Any ideas why this won't get stored? Is it because the $quote->create() function doesn't factor in previously set data and just takes its parameter as everything to be saved? If so how do I do this?
Here's my controller's store() function:
/**
* Stores a created quote in the database
*
* #param QuoteRequest $request
*
*/
public function store(QuoteRequest $request)
{
// This method will only get fired if QuoteRequest passes
$quote = new Quote;
$quote->user_id = Auth::id();
$quote->create($request->all());
echo 'Job done';
}
Here's my Quote model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class Quote extends Model {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'quotes';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'quote_person',
'quote_value',
'quote_date'
];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = [ ];
/*
* Request/User many-to-one relationship
*/
public function user()
{
return $this->belongsTo('App\User');
}
/*
* Belongs to current User scope
*/
public function scopeMine($query)
{
return $query->where('user_id', Auth::id());
}
}
Try this and see if it works.
public function store(QuoteRequest $request)
{
// This method will only get fired if QuoteRequest passes
$quote = new Quote;
$quote->fill($request->all());
$quote->user_id = Auth::id();
$quote->save();
echo 'Job done';
}
The create function is considered mass assignment and is thusly affected by $fillable / $guarded. It's also a static function, so $quote->create() is making an entirely new Eloquent instance - that's why your manually assigned user_id is lost.
You can use Model::unguard() to temporarily turn off the protection.
Long story short: I'm building a "privacy" page where uses can chose what shows up and what does not show up on their profiles.
I am considering having a 1:m table user:privacy and just have entries for the keys they want private. If they don't exist they are public. Hope this makes sense.
Table would be user_privacy and will have 3 columns: id, user_id, privacy_key (string, i.e. email/phone/cell/etc)
Is there a way to simple query by the keys i will define that i can run to determine if the user has a key or not or do i have to go extra lengths to add a function to the user model to do this (trying to avoid, love the magic-ness of eloquent)
Basically i want to have a condition that sounds like "if ($user->privacy->email or $user->privacy->phone)"
Thanks and hope i was clear enough, lol
You could add a function to your user model:
public function isPrivate($attribute){
$privacyAttribute = $this->privacy->first(function($model) use ($attribute){
return $model->key == $attribute; // key being the column in the privacy model
});
return !is_null($privacyAttribute);
}
And then do your if statement this way:
if ($user->isPrivate('email') or $user->isPrivate('phone'))
Or a different implementation (usage is the same)
private $privacyAttributes = null;
public function isPrivate($attribute){
if($this->privacyAttributes == null){
$this->privacyAttributes = $this->privacy()->lists('key');
}
return in_array($attribute, $this->privacyAttributes);
}
User Model header:
/**
* Class User
* #package Interallmas
*/
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
/**
* #var null|array
*/
protected $privacy_keys = NULL;
Privacy Relationship:
/**
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function privacy() {
return $this->hasMany('Interallmas\Privacy');
}
Privacy functions:
/**
* #return bool
*/
public function privacy_initialized() {
return ($this->privacy_keys !== NULL);
}
/**
* #return void
*/
public function initialize_privacy() {
if (!$this->privacy_initialized()) {
$this->privacy_keys = [];
foreach ($this->privacy as $privacy) {
$this->privacy_keys[] = $privacy->privacy_key;
}
}
}
/**
* #param $key
* #return bool
*/
public function isPrivate($key) {
$this->initialize_privacy();
return (in_array($key,$this->privacy_keys));
}
So: Whenever i access the isPrivate($key) method, i cache the result for the next use so i don't hit the server too hard - the function may be accessed once or more - i just query once, the first time. I believe for my needs, this is the best way to do it.
I think a simple count > 0 check should suffice. This requires you to have defined the relationship with the hasMany method for the User Model.
if (count($user->privacy) > 0) {
...
}