Defining route in laravel corrctly - php

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

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.

get one unique item from a laravel relationship

i have a 1 to n relation ship between product and images and on images i have a field called color_id which has a 1 to n with images . now what i want to do is to call the product with images relationship like below :
$relation = Product::with('images')->where('id',$product->id)->get();
dd($relation);
now what want is to get 1 images from each color_id so for example if this product has 3 images with color_id of 1 , and 4 images with color_id of 2 .i want to get 1 images from each color . and here is the dd result of $relation in case needed .
^ Illuminate\Database\Eloquent\Collection {#945 ▼
#items: array:1 [▼
0 => Webkul\Product\Models\Product {#912 ▼
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▶]
#original: array:7 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"images" => Illuminate\Database\Eloquent\Collection {#968 ▼
#items: array:3 [▼
0 => Webkul\Product\Models\ProductImage {#967 ▼
+timestamps: false
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▼
"id" => 17
"type" => null
"path" => "product/39/y0ueAFEgscD9ZsVFg7nT2WPNsU6vDyXWqLVONa8L.jpeg"
"product_id" => 39
"product_color_id" => 3
"product_size_id" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Webkul\Product\Models\ProductImage {#970 ▼
+timestamps: false
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▼
"id" => 23
"type" => null
"path" => "product/39/M9JTVsy2BtNp8LuUQh7cp17S5jN1ifOZ1P9WZk3h.jpeg"
"product_id" => 39
"product_color_id" => 2
"product_size_id" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
2 => Webkul\Product\Models\ProductImage {#969 ▼
+timestamps: false
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▼
"id" => 24
"type" => null
"path" => "product/39/STA9sGKjWHWDn0wkmnVRqZYdI2adLE5qaIn7vyFQ.jpeg"
"product_id" => 39
"product_color_id" => 1
"product_size_id" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
Try this
$relation =Product::with(array('images' => function($query) {
$query->groupBy('product_color_id');
})->where('id',$product->id)->get();

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 how to convert data into an object

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?

Using select on eloquent model causes "Undefined offset:0"

I am new to laravel. I have written code like below. And when I add select, it causes an error saying undefined offset: 0.
It also causes same error when I have more than two records even though I commented out select part.
When I check the query using toSql(), it is perfectly fine.
So when I use dd() on $clientDrivers before return, the output is below. (Seems fine as well)
What would be the problem with my code?
Any suggestion or advice would be appreicated.
EDIT: I have found the problem which was the $appends in Driver Model. Why does $appends cause the problem?
Solution: I have fixed my Driver Model getBankAttribute which is used for $appends and it works fine.
Here is my code:
Model
public function drivers() {
return $this->belongsToMany('App\Model\User\Driver', 'ClientDriver', 'client_id', 'userdriver_id');
}
Controller
$client = ClientModel::findOrFail($id);
$select = ['UserDriver.name as userdriver_name', 'UserDriver.phone_number as userdriver_phone_number'];
$clientDrivers = $client->drivers()
->select($select) // this does not work. If this is commented out it works perfectly fine.
->get();
return response($clientDrivers, 200);
Result of dd($clientDrivers)
Collection {#805
#items: array:2 [
0 => Driver {#798
#dates: array:1 [
0 => "deleted_time"
]
#table: "UserDriver"
+timestamps: false
+appends: array:1 [
0 => "bank"
]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"userdriver_name" => "driver1"
"userdriver_phone_number" => "140412351235"
]
#original: array:4 [
"userdriver_name" => "driver1"
"userdriver_phone_number" => "140412351235"
"pivot_client_id" => 1
"pivot_userdriver_id" => 1
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#801
+pivotParent: Client {#713
#table: "Client"
#guarded: array:2 [
0 => "id"
1 => "created_time"
]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [
"id" => 1
"created_time" => "2017-12-28 05:23:50"
"invoice_email" => ""
"biz_number" => "1234512345"
]
#original: array:20 [
"id" => 1
"created_time" => "2017-12-28 05:23:50"
"invoice_email" => ""
"biz_number" => "1234512345"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#forceDeleting: false
}
#foreignKey: "client_id"
#relatedKey: "userdriver_id"
#guarded: []
#connection: "mysql"
#table: "ClientDriver"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"client_id" => 1
"userdriver_id" => 1
]
#original: array:2 [
"client_id" => 1
"userdriver_id" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
1 => Driver {#799
#dates: array:1 [
0 => "deleted_time"
]
#table: "UserDriver"
+timestamps: false
+appends: array:1 [
0 => "bank"
]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"userdriver_name" => "driver2"
"userdriver_phone_number" => "140412351236"
]
#original: array:4 [
"userdriver_name" => "driver2"
"userdriver_phone_number" => "140412351236"
"pivot_client_id" => 1
"pivot_userdriver_id" => 2
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#803
+pivotParent: Client {#713}
#foreignKey: "client_id"
#relatedKey: "userdriver_id"
#guarded: []
#connection: "mysql"
#table: "ClientDriver"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"client_id" => 1
"userdriver_id" => 2
]
#original: array:2 [
"client_id" => 1
"userdriver_id" => 2
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
]
}
You need to pass string in $select. You are currently passing array in it.
Change your $select as below:
$select = "'UserDriver.name as userdriver_name', 'UserDriver.phone_number as userdriver_phone_number'";
You could use with() to get associated drivers for a client
$client = ClientModel::where('id',$id)
->with('drivers:name,phone_number')
->get();
$clientDrivers = $client->drivers;
return response($clientDrivers, 200);

Categories