this has been driving me nuts...
I'm in my view I'm calling
Auth::user()->userlists
which brings me back an array of values like
{"id":"1","user_id":"1","name":"list1".....}
all I want to do is bring back the names I've tried all these;
Auth::user()->userlists->name
Auth::user()->userlists()->name
Auth::user()->userlists->name->get()
Auth::user()->userlists.name
But I always get an error such as "Undefined property"
How do I return this single property, it's in my array for all the items but I'm clearly just getting the syntax incorrect...?
The reason i'm trying to do this is that I need the values placed in a drop down box, it's finding the correct rows in the table but displaying all the data instead of just the name
Form::select('userlist_id', Auth::user()->userlists);
Many thanks.
You say a User has many Userlist models, so it's a collection not single model, thus you could work with it in a loop, BUT there is better way:
$lists = Auth::user()->userlists()->lists('name','id');
// To make it available in all the views, place this for example in a controller:
View::share('userlists', $lists);
This will fetch id and name for the related models collection and return it as an array, so it's just the one you will use in a Form builder:
Form::select('userlist_id', $lists)
Related
I have relationship between User and Attendance, where in Attendance table I have field start and end which are date fields. I'm able to retrieve the data through relationship. I'm getting User Absences for current month and afterwards I want to add "additional" field/variable/attribute with array of this dates(parsed to Carbon) to User model. Is it possible?
I was trying to use booted() method with static::retrieved function, where I call another.
When I need to check those absences in view to display a Calendar I iterate too many times (previously I've created a method inside Users model to retrieve and parse those data).
I need to achieve something like this:
if(in_array($someDates, $user->additional_array)) [...]
Previously I've got:
if(in_array($someDates, $user->methodToReturnAdditionalArray())) [...]
Any idea?
You can use the Laravel Attributes and define the new method for getting the data you want
Example:
public function getAdditionalArrayAttribute()
{
// return the list of array items you want;
}
And then you can use it like here:
$model->additional_array;
In my Blade file, there are two dropdowns, "Countries" and "Regions". I want the Regions dropdown only to display the values linked to the country selected in the "Countries" dropdown.
For that, I believe, I have to pass a variable for $countries and $regions in order to compact them and call the variables in the blade file.
This is what I currently have (which is totally wrong), but what should it be?
public function create()
{
$countries = Countries::orderBy('name')->get();
$regions = Regions::where('countries_id' == $countries('id'))->orderBy('name')->get();
return view('admin.cities.create', compact('regions', 'countries'));
}
I know that the $regions variable part "where('countries_id' == $countries('id'))" is incorrect, but I've tried multiple other ways, and still not able to figure it out.
Is this variable = model::where(foreign_key == parent_table ID) possible?
All my relationships have been set up with Laravel Eloquent already.
Any assistance would be appreciated
I started to write how to fetch data from controller(Country::all and Regions::all), and what to do in blade file and ajax functions that does stuff on events and figured - maybe there already is a solution.
Sorry, I don't have 50 rep to comment, but if you follow what they did here (both answers), I think you're gonna figure out what to do in your example :)
Keep in mind you need the part in the bottom of the page!
Laravel dynamic dropdown country and state
My goal is to retrieve all of a user's 'items' and display then in my view grouped by their 'status'. There are 4 possible statuses, each with their own <div> on the page containing the items' info. After some poking around I believe I need to use the groupBy() method like so:
$items = Item::ownedBy( Auth::id() )->groupBy('status')->get();
This does seem to do some sort of grouping, but when I iterate over the collection I get a max of 4 results, one for each status. This doesn't really solve my problem as I need to display all of a user's items for each status, not just one. I must be missing something here, I'd really like to avoid making a query for each status and displaying them that way. I suppose I could filter the collection by status and create 4 new collections, but isn't this what groupBy() is supposed to do?
You can do that very easy with laravel and Collections. Collections have powerful API and a lot of handy methods, to easily achieve what you want.
Your mistake here is that you are calling groupBy on the QueryBuilder object which returns only groupped by records from the database. Instead you must select all of the records you need, then they will be returned as a collection. After that you can manipulate the collection as you wish. So what you need is:
$items = Item::ownedBy( Auth::id() )->get()->groupBy('status');
You can view all of the Colletion class useful methods here.
I have two tables:
Cards
Notes
Each Card has multiple Notes. So there is a relation between them like this:
class Card extends Model {
public function notes ()
{
return $this->hasMany(Note::class);
}
}
Ok well, all fine.
Now I need to understand the concept of these two lines:
$card()->$notes()->first();
and
$card()->$notes->first();
What's the difference between them? As you see in the first one $note() is a function and in the second one $note isn't a function. How will they be translated in PHP?
The first one points out to the card table and the second one points out to the notes table, right? or what? Anyway I've stuck to understand the concept of tham.
I don't know about $ before the $notes in your code but if you trying to say something like this.
1- $card->notes()->first();
2- $card->notes->first();
In the code in line 1, first you have a $card model and then you wanted to access all notes() related to that $card, and because of adding () after notes you simply call query builder on notes, show you can perform any other database query function after that, something like where, orderBy, groupBy, ... and any other complicated query on database.
But in the second one you actually get access to a collection of notes related to that $card, we can say that you get all related notes from database and set it into laravel collections and you are no more able to perform database query on notes.
Note: because laravel collections have some methods like where(), groupBy(), whereIn(), sort(), ... you can use them on the second one, but in that case you perform those methods on collections and not database, you already get all results from database
I query an entity:
$trip_redirect_id = Trip::whereUniqueToken($unique_token)->select('id')->get();
and try to get the id value with $trip_redirect_id->id or $trip_redirect_id['id'] but I get exception that the field is not present.
When I dump the $trip_redirect_id object, I can see the id and it's value and they are correct (I checked with the database). But how can I access the value inside php?
You are using get() which is going to be returning a Collection of objects so you won't be able to get the attributes of the models unless you iterate through the collection, even if it's just one item in the collection, or just grab the first record. Use this instead:
Trip::whereUniqueToken($unique_token)->select('id')->first();
Turns out, if I use auto-incrementing IDs, I can access the id attribute of the newly created model, right after it has been created in the DB.
So, this is the whole fix:
$trip->save();
$trip_redirect_id = $trip->id;