laravel 5 edit elequent not working - php

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();
[...]
}

Related

Paginator data don't show well in livewire "public property [posts] must be of type: [numeric, string, array, null, or boolean]"

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.

How to get the data of third table and second table from first table?

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"
}
]
}

Laravel Data passing from view to controller

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]);
}

Laravel collections iteration with foreach loop

its a simple query in a controller using Order model:
$orders = Order::where('user_id', auth()->user()->id)
->orderBy('created_at', 'desc')
->get();
return dd($orders);
dd gives following results: (which is correct)
Collection {#269 ▼
#items: array:2 [▼
0 => Order {#270 ▼
#dates: array:1 [▶]
#fillable: array:6 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
1 => Order {#271 ▼
#dates: array:1 [▶]
#fillable: array:6 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
]
}
but when i iterate through the $orders collection foreach loop, only the first array is is shown, the second array is not accessible... whats wrong with foreach loop... ?
$temp='';
foreach($orders as $order){
$temp.= $order->product; // accessing the belongTo method
}
dd($temp);
here is the output (only one array is shown):
"{"id":13,"created_at":"2018-06-06 15:28:21","updated_at":"2018-06-06 18:36:28","type":0,"title":"product 3","description":"this is product no 3 with 5 images","images":"night-product-watch-dramatic-84475_1528310188.jpeg,night-product-watch-dramatic-84475_1528310188.jpeg","alive":1,"user_id":1,"deleted_at":null} ◀"
If you are building an array of products across all orders you cannot use string concatenation. Use an array and push items onto it.
$products = [];
foreach ($orders as $order) {
if ($order->product) {
$products[] = $order->product;
}
}
dd($products); // will be an array of all other products.
Try this:
$temp= [];
foreach($orders->all() as $order){
$temp[] = $order->product;
}
And you may edit your query
$orders = Order::where('user_id', auth()->user()->id)
->with('products')
->orderBy('created_at', 'desc')
->get();

Laravel trying to query relatinship model hasMany

I have the following tables in my application:
projects
projects_plot_types
Projects can have many plot types. I have the following relationship setup in the projects model.
/**
* The plot types that belong to the project.
*
* #return Object
*/
public function plotTypes()
{
return $this->hasMany('App\Models\Project\ProjectsPlotTypes');
}
I want to be able to get the plot type by querying it's name on the project model.
I have tried this, but it does not work:
$project->with('plotTypes')->whereHas('plotTypes', function ($query) use ($row) {
$query->where('name', $row->plot_name);
})->first()->plotTypes->first()->id;
Can someone point me in the right direction?
The output of $result as per below comment is:
Project {#705 ▼
#table: "projects"
#fillable: array:7 [▶]
+timestamps: true
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:10 [▶]
#original: array:10 [▶]
#relations: array:1 [▼
"plotTypes" => Collection {#769 ▼
#items: array:1 [▼
0 => ProjectsPlotTypes {#774 ▼
#table: "projects_plot_types"
#fillable: array:2 [▶]
+timestamps: false
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:4 [▼
"id" => "2"
"project_id" => "1"
"name" => "TYPE 3 - VENTILATION"
"budget" => "245.69"
]
#original: array:4 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
You may also use join query like below
$result = Illuminate\Support\Facades\DB::table('projects')
->join('projects_plot_types', 'projects_plot_types.id', '=', 'projects.project_plot_id')
->where('projects_plot_types.name', '=', $row->plot_name)
->first();
print_r($result);
Maybe this is what you are looking for:
$result = $project->with(['plotTypes' => function($query) use ($row) {
return $query->where('name', $row->plot_name)->first();
}])->first();
dd($result); // print the result and die
UPDATE 1
Fetch the first item from the collection that has BelongsTo relationship:
$plotTypeResult = $result->plotTypes[0]->id;

Categories