How to use relationship data outside of relationship field in Laravel Nova? - laravel

I am trying to figure out how to do something seemingly simple with Laravel Nova, but I can't figure this out.
What I want to do is reference relationship data in a text field. I see in Nova, and understand how to reference a relationship via the HasOne, HasMany ... facades. But what I want to do is get relationship data like this:
Text::make('State', $this->state->name)
This doesn't work, and something I noticed when trying to debug is that each function in a Nova resource seems to run multiple times. Here is the logging I added:
public function fields(Request $request) {
logger($this->state->name)
}
When I do this, there are 3 logging instances, the first 2 containing the state name, and the third not. I think that may have something to do with it not working, but don't know what might be causing this.
Thank you in advance for help!

There's a simple way to get relationship data into a Nova Text field, just use a closure:
Text::make('State', function() {
return $this->state->name;
})
As for the multiple calls to the fields function, the answer has to do with the question: Do you have another Resource in your Nova folder that is related to the Resource we are discussing? If so, that is why -- it needs to call fields to display it properly. You can examine the following query string parameters to get more insight: viaResource, viaResourceId, and viaRelationship.

Related

Laravel 5: find the page number of an entry

I'm working on a custom forum and when a post is added to a thread I have to notify the subscribed users. In the message I have to place the link for the user to go straight to the post. Something like this:
/forum/discussion/1/ateliers-et-expositions?page=2#forum-message-3
To be able to do that I need to know the page number of the entry. Does this feature exist on Laravel already? I couldn't find it in their docs and I know about features that exist but are not in their docs, so maybe it's there already and I'm just not aware of it.
I would like to avoid making a select in the database a loop through it to find the page number, if possible.
You most likely look for get() method of Request class. Assuming you want to get this in your controller method, say foo(), you should have:
use Illuminate\Http\Request;
public function foo(Request $request)
{
$page = $request->get('page');
...

Laravel: form method != save method?

I am new to Laravel coming from CakePHP where the form and save method for a form is one and the same function name. I saw in many Laravel tutorials that the from method (that displays the form) is different than the method to save form (that actually saves data). Why using 2 different method names?
For example what's wrong with:
pub function xyz(Request $request)
{
if($results->isMethod('post')){
... then save and return redirect
}
... the code for showing the form in case there is no POST.
then having 2 routes one for GET and one for POST on the same url?
It is because people like to filter out things at route level not in controller, Also it helps developer to apply middleware grouping for each route separately. so that they can apply roles and permission etc. easily at route level.
It will looks horrible if mix all things in controller.
Think about middleware and groups in your code.
It is because you don't wanna mix a lot of logic in the same method . The case you have simple is the simple scenario . But there will be case where you wanna pass initial data in the create form . You have to write logic for that also in the same method and while you store the data you need to do the validation and calculate other business logic . If you combine all those things in one method it will mix all the things in one method and code difficult to read

Why is my Laravel Eloquent accessor not showing up in the response?

I have a Model Review that has a unix timestamp as 1 of it's attributes (table columns).
I use 2 accessors inside this model:
public function getReviewDateAttribute($value)
{
return strftime('%A %e %B %Y', $value);
}
public function getReviewDateIsoAttribute()
{
return Carbon::createFromTimestamp($this->review_date)->toDateTimeString();
}
getReviewDateAttribute works as expected and shows up in the collection of models when I write a query.
However getReviewDateIsoAttribute does not. What could be the reason for this?
A subquestion: If I use the same attribute in both functions, how can I use the original format as input value?
You should be adding it to the $appends array. It isn't spinning through all available methods looking for getXXXXXAttribute. The other one is used because it is an accessor for an actual attribute, this one is not an actual attribute.
class YourModel ....
{
protected $appends = ['review_date_iso'];
...
}
Laravel 5.5 Docs - Eloquent - Serialization - Appending Values to JSON
This is probably because ReviewDateIso is not an actual column and therefore will not show up in the model collections...you can access it directly by calling the method directly
$model->getReviewDateIsoAttribute()
According to the docs accessors a ways of changing the value of a column before it is returned to the method that queried it.
If you want it to show up when you call the collection as Json append it to the output with
protected $appends = ['name_of_attribute'];
I tried queries in tinker and with dd method. So I got confused since I cant view my new accessor in the attribute list.
I thought I missed something. Then later I noticed appended attributes are shown separately when we dd the query result than the usual attribute JSON.
I lost time thinking my accessor is not working. Its just I`m not checking in the right way or right place. So I``m attaching the following image, if anyone gets stuck on accessors like me, this might help them.
In fact it depends on what you are trying to achieve. If you are going to display anything in Blade for example, you can use whenever you want $object->review_date_iso to get this attribute value.
However if you are returning Json responses (for example API), you need to tell Eloquent model to append extra fields to your model when transforming to JSON format.
You probably have review_date attribute in your model (this is the column in database I suppose) but you don't have review_date_iso in your table in database, so in your model you need to use:
protected $appends = ['review_date_iso'];
but you don't have to include here review_date because it's already in your table in database and your accessor will be automatically fired.

Adding attribute to a user when register in Laravel 5.1

When a user Register, I want to add location data for the user.
I get those with GeoIP.
So, each time a user is created, I would like somewhere to add Country, City, etc.
I was thinking about setting Hidden fields in view, but I think it a hugly way to do it, and I'm sure there is a better way to do...
Any Idea???
Any time I create a record that needs extra data, involves inserting additional records into additional tables, etc, I create a service class. Something like "UserCreator" then I pass it the input, do any additional operations, wrap multiple database calls in a transaction and so on.
That said there are so many ways to do what you want. You could Input::merge(...) then save, you could separate the process of creating a user from your controller / route function, etc.
If you are just getting started with Laravel and/or your project is rather simple, then you probably want to look at Input::merge
I solved it using Request Contructor as said here in laracast
In the Form Request's constructor I inject \Illuminate\Http\Request. I can then add my value to the request instance as such:
public function __construct(\Illuminate\Http\Request $request)
{
$request->request->add(['date_of_birth' => implode('-', $request->only('year', 'month', 'day'))]);
}

Accessing model from controller?

I want to get data from my database and then in my model I wish to do some php stuff to the data before passing it to my view.
Normally I would do this to get all of my data:
->with('content', Content::all());
But I have set up a function in my model called test:
public function test(){
//get and modify data here
}
How can I access this using:
->with
from my controller?
Is this the one you are looking for?
Model:
public static function foo($bar) {
return static::where('foo', '=', 'bar');
}
Controller:
->with('content', Foo::foo("test"));
What you are trying to do isn't really explained correctly but I'll try to answer anyway.
From what I understand, you want to do some data transformation in the model before it is used. But your comment in the test() function say that you want to GET and modify data.
In laravel, functions for getting and modifying data are separated:
For getting data, you can define a query scope ( http://four.laravel.com/docs/eloquent#query-scopes ). A query scope is usually a shortand for adding some parameters to an existing query.
For modifying data before it is read from the model, or before assinging it to the model, Eloquent provide a system called mutators (http://four.laravel.com/docs/eloquent#accessors-and-mutators ). A mutator is a method called when reading or assiging value to a field. It is usualy to convert php type to a database type (for example array to json on save, then json to array on read). A mutator can be used to populate some extra value or do some extra checks to the value.
I hope I've helped you a bit. If you don't find it is a correct answer, please clarify your question so I can help you a little more.

Resources