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]);
}
Related
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));
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.
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 have a problem like this
<a href="{{route('essayanswers.show',[$essayAnswers]) }}" >
I have define route for this in web.php , like this.
Route::resource('essayanswers', 'EssayAnswerController');
EssayAnswer {#541 ▼
#fillable: array:4 [▼
0 => "user_id"
1 => "essay_id"
2 => "essay"
3 => "content"
]
#connection: null
#table: 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 [▶]
}
But when I use dd command inside the EssayAnswerController I Got a input like this.I canno't acces the attributes of essayAnswer
How to define route for this in web.php file in laravel?
Try something like this as per documentation
Route::resource('essayanswers', 'EssayAnswerController', ['parameters' => [
'show' => 'essayAnswer'
]]);
I am trying to edit my data field with following elequent but its not picking up the data, any help appreciated.
following is the data controller i am using:
public function edit($name)
{
$category = category::where('name', $name)->get()->all();
dd($category);
return view('/editcategory')->with('category', $category);
}
while checking the output it not picking up the data from the db
output:
array:1 [▼
0 => category {#190 ▼
#table: "category"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:5 [▶]
#original: array:5 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
Don't use both get() and all() in the same time.
It should be:
public function edit($name)
{
$category = category::where('name', $name)->all();
[...]
}