I have this peace of code:
auth()->user()->notify(new TestNotification($oneParam, $twoParam));
My auth()->user() return the user logged in like:
App\Models\User\User {#1094 ▼
+timestamps: false
#fillable: array:8 [▶]
#hidden: array:1 [▶]
#appends: array:1 [▶]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▶]
#original: array:13 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
#accessToken: Laravel\Passport\Token {#1055 ▶}
}
And I have in User Model the use Notifiable; trait and use Illuminate\Notifications\Notifiable; import.
VSC extension: PHP Intelephense v1.7.1
PHP 7.4.9
Laravel Framework 8.52.0
Use notification facade instead of method.
auth()->user()->notify(new TestNotification($oneParam, $twoParam));
Replace with this:
Notification::send(auth()->user(), new TestNotification($oneParam, $twoParam));
Related
I like to use livewire instead of controller
public function index()
{
return view('advert.index', [
'posts' => Advert::latest()->filter(
request(['search', 'category', 'author'])
)->paginate(18)->withQueryString()
]);
}
in livewire <livewire:advert.home :querys="request(['search', 'category', 'author'])">
public function mount($querys)
{
$this->querys = $querys;
}
public function render()
{
$this->posts = Advert::latest()->filter([$this->querys])->paginate($this->perPage)->withQueryString();
}
but nothing work
it is mostly say
Livewire component's [advert.home] public property [posts] must be of
type: [numeric, string, array, null, or boolean]. Only protected or
private properties can be set as other types because JavaScript
doesn't need to access them.
I am lost i use this but it don't work well and show error above. and look same data passed to livewire component.
and there is no difference between livewire $this->posts and 'posts' from controller i can't figure about that.
like
'posts' dd($posts) from controller
Illuminate\Pagination\LengthAwarePaginator {#1412 ▼
#total: 2
#lastPage: 1
#items: Illuminate\Database\Eloquent\Collection {#1402 ▼
#items: array:2 [▼
0 => App\Models\Advert {#1409 ▼
#with: array:3 [▶]
#fillable: array:8 [▶]
#connection: "mysql"
#table: "adverts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:3 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => App\Models\Advert {#1410 ▼
#with: array:3 [▶]
#fillable: array:8 [▶]
#connection: "mysql"
#table: "adverts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:3 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
#perPage: 18
#currentPage: 1
#path: "https://awento.ddns.net/search"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}
and livewire $this->posts
ddd($posts)
Illuminate\Pagination\LengthAwarePaginator {#1470 ▼
#total: 2
#lastPage: 1
#items: Illuminate\Database\Eloquent\Collection {#1458 ▼
#items: array:2 [▼
0 => App\Models\Advert {#1465 ▼
#with: array:3 [▶]
#fillable: array:8 [▶]
#connection: "mysql"
#table: "adverts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:3 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => App\Models\Advert {#1467 ▼
#with: array:3 [▶]
#fillable: array:8 [▶]
#connection: "mysql"
#table: "adverts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:3 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
#perPage: 15
#currentPage: 1
#path: "https://awento.ddns.net"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}
After some trail and error i realise the issue is with whole data if we arrange in some form of data that there will be no issue. So Now i create a array for transfer data with some foreach. If there is another better way please let me know.
$postss = Advert::latest()->filter($this->querys)->paginate($this->perPage)->withQueryString();
$posts = array();
foreach($postss as $post)
{
array_push($posts, $post);
}
$this->posts = $posts;
It seem paginate don't work in livewire using limit now.
I have three tables : "Users", "tickets" and "events". I want to get tickets of the users and the details of tickets is in events. I want to get data from all table using relationships.
i need user and ticket of user and event of that ticket
Also am i doing it properly?
I have made relations with all the tables.
table structure is:
users
-username, user_id
ticket_orders
-user_id , ticket_id, event_id
events
-event_id, event_name
For Users
public function ticketOrder(){
return $this->hasMany('App\TicketOrder', 'user_id', 'user_id');
}
For tickets
public function user(){
return $this->belongsTo('App\User', 'user_id', 'user_id');
}
public function events(){
return $this->belongsTo('App\Event', 'event_id', 'event_id');
}
for events
public function ticketOrder(){
return $this->hasMany('App\TicketOrder', 'event_id', 'event_id');
}
I tried this in controller
here CreateEvent and Event is same. On my code i am using "CreateEvent" instead of "Event"
public function showMyTickets()
{
$user = Auth::user();
$ticket= $user->with('TicketOrder','TicketOrder.CreateEvents')->where('user_id',$user->user_id)->get();
dd($ticket);
}
this is what i got in dd($ticket)
the data are in relations. now i dont know how to retrive it and am i doing it correctly.
Collection {#1164 ▼
#items: array:1 [▼
0 => User {#970 ▼
#fillable: array:6 [▶]
#hidden: array:3 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:11 [▶]
#original: array:11 [▶]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"TicketOrder" => Collection {#1163 ▼
#items: array:1 [▼
0 => TicketOrder {#1068 ▼
#fillable: array:8 [▶]
#hidden: array:8 [▶]
#connection: "mysql"
#table: "ticket_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:11 [▶]
#original: array:11 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"CreateEvents" => CreateEvent {#1155 ▼
#fillable: array:22 [▶]
#hidden: array:1 [▶]
#connection: "mysql"
#table: "create_events"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:26 [▶]
#original: array:26 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
}
]
}
]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
]
}
I need to pass data from a view file to a controller file. The purpose is to show the user the previous data at the time of edit the record. My view file contains this code:
Edit
I called the dd method before passing it to the controller. Here is the result of dd:
FeesType {#287 ▼
#table: "fees_types"
#fillable: array:2 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [▶]
#original: array:5 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
When I receive the object in the controller file it shows this result on dd:
FeesType {#283 ▼
#table: "fees_types"
#fillable: array:2 [▶]
#connection: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
The problem is the connection variable returns a null in controller but in view it returns my current connection database: mysql.
Here is my controller file's edit method:
public function edit(FeesType $feesType)
{
//
//$feesType = FeesType::find($feesType->id);
dd($feesType);
return view('feestype.edit',['feesType'=>$feesType]);
}
and here is my route definition:
Route::resource('feestype','FeesTypesController');
I don't know the reason behind this. Can anybody help me with that?
try like this:
ROUTE
Edit
CONTROLLER
public function edit($id)
{
$feesType = \App\FeesType::find($id);
dd($feesType);
return view('feestype.edit',['feesType'=>$feesType]);
}
Hi I have the following code under my edit function of my project:
$bankAcc = BankingAccount::find($id)->where('id', $id)->with('userprinciple')->with('banks')->first();
When I dd the $bankAccount variable I get the following results:
BankingAccount {#1590 ▼
#fillable: array:8 [▶]
#table: "banking_accounts"
+timestamps: true
#hidden: array:1 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:14 [▶]
#original: array:14 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▶]
#touches: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
But I only want the data under the attributes in an object. So that I can for example do this in my blade:
{{ $bankAcc->userprinciple->principle }}
I keep getting various errors as I have been trying - mostly trying to show property of non-object. So how do I convert that data into and object?
I'm not sure this is really a laravel question but, when I dd(die and dump) this dd($user->friends()); I get the following. I did notice that it is a collection. I'm not sure if that means something different or not. But it still should be an array of items I believe. With the first user at the [0] mark and next [1], etc...
Collection {#184 ▼
#items: array:2 [▼
0 => User {#189 ▼
#table: "users"
#fillable: array:7 [▶]
#hidden: array:3 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:11 [▶]
#original: array:13 [▶]
#relations: array:1 [▶]
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => User {#190 ▼
#table: "users"
#fillable: array:7 [▶]
#hidden: array:3 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:11 [▶]
#original: array:13 [▶]
#relations: array:1 [▶]
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
So then when I try to do something like :
foreach($user->friends() as $friend) {
dd($friend);
}
This is what I get after doing that:
User {#189 ▼
#table: "users"
#fillable: array:7 [▶]
#hidden: array:3 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:11 [▶]
#original: array:13 [▶]
#relations: array:1 [▶]
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
I want it to loop through all the users not just the first one. Is there a reason it is doing this. Am I doing the foreach wrong or does it have something to do with the collection?
When you do your foreach, you're only seeing one entry because of the dd(). Remember, it is "dump and die", so on the very first iteration, you're dumping the record and then dying.
Try this out:
foreach($user->friends() as $friend) {
print_r($friend);
}
If you just want to treat it as an array, use toArray on the collection first. Eg:
$friends = $user->friends()->toArray();
foreach($friends as $friend){
...some stuff...
}
Otherwise, use the features of the laravel collection as per the docs here:
http://laravel.com/docs/5.2/collections