I have table Authors and table Uploads and I am simply trying to get all uploads for a certain author by author_id in the Uploads table.
For that purpose I have a method in my controller looking like this :
public function author_works()
{
$uploads= Upload::where('author_id', 3)->first();
return view('author-works')->with('uploads',$uploads);
}
My view is :
#extends('layouts.app')
#section('content')
<h1>Uploads</h1>
#if(count($uploads)>0)
#foreach($uploads as $upload)
<div class="well">
<h3>{{$upload->name}} </h3>
<small>Written on {{$upload->created_at}}</small>
</div>
#endforeach
{{-- {{$uploads->links()}}--}}
#else
<p>No uploads found</p>
#endif
#endsection
When I debug with dd($uploads); in the controller I am seeing the following, which is actually the right record :
#attributes: array:9 [▼
"id" => 7
"name" => "Fsdf"
"description" => "fsdfsdffds"
"created_at" => "2017-12-07 21:29:53"
"updated_at" => "2017-12-07 21:29:53"
"user_id" => 1
"avtor_id" => 3
]
#original: array:9 [▼
"id" => 7
"name" => "Fsdf"
"description" => "fsdfsdffds"
"created_at" => "2017-12-07 21:29:53"
"updated_at" => "2017-12-07 21:29:53"
"user_id" => 1
"avtor_id" => 3
]
Can someone explain why am I getting the error?
When you're using first() method, you're getting an object instead of collection. Use get():
$uploads = Upload::where('author_id', 3)->get();
Related
I'm trying to display the users name using blade, but Its says null.
This is the controller
public function edit(Project $project)
{
//
$project = Project::findOrFail($project->id);
$assignees = User::with('roles')->get();
return view('projects.edit',['project'=>$project,'assignees'=>$assignees]);
}
When I dd($assignees) all the columns appears including the name column.
#attributes: array:10 [▼
"id" => 4
"name" => "Kitten Moose"
"email" => "kitten#karata.com"
"email_verified_at" => null
"password" => "$2y$10$iG4EZBq13ExGOVIrGNhEQeVvordWc3ibWfYgdaq6x0wqefetqXixe"
"remember_token" => null
"created_at" => "2020-07-03 05:11:56"
"updated_at" => "2020-07-11 09:37:49"
"team_id" => 3
"deleted_at" => null
]
On blade:
<div class="form-group">
<label for="exampleFormControlSelect1">Assignee</label>
<select name="assignee_id" class="js-example-basic-single">
<option value=" ">Select</option>
#foreach($assignees as $assignee)
<option value="{{$assignee->id}}">{{$assignee->name}}</option>
#endforeach
</select>
</div>
Using the blade above, I get {"id":1,"name":null} everything else displays correctly
I'm not sure if this could be caused by the model, but I have this in the user model:
public function assignee()
{
return $this->belongsTo(User::class);
}
I'm trying to use old() in Laravel but it doesn't work.
In my controller:
dd(back()->withInput());
this is the session result
#session: Store {#364 ▼
#id: "dyNmtFJVQCQmcCob4SPYEBzWHJ6TJeM9X0mzWWu7"
#name: "laravel_session"
#attributes: array:6 [▼
"_token" => "NVJATvxuHRlGbAC1jUEIjS6gbLQQEWPIIV41fLYd"
"url" => []
"_previous" => array:1 [▼
"url" => "http://localhost:8000/units/23/rents/create"
]
"_flash" => array:2 [▼
"old" => []
"new" => array:1 [▼
0 => "_old_input"
]
]
"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 1
"_old_input" => array:5 [▼
"_token" => "NVJATvxuHRlGbAC1jUEIjS6gbLQQEWPIIV41fLYd"
"contract_id" => "20"
"unit_id" => "23"
"amount_paid" => "1"
"submit" => "submit"
]
]
#handler: FileSessionHandler {#363 ▶}
#started: true
value="{{ old('amount_paid')}}" I would get empty field
value="{{ old('amount_paid','test') }}" I would get 'test' in the field
I'm using Laravel 5.6
EDIT
to clarify the above code it to debug
this is my original code
controller
return back()->withInput();
view
<div class="form-group">
<label> amount paid </label>
<input type="float" class="form-control" name="amount_paid" value="{{ old('amount_paid')}}" required>
</div>
adding the method that handle the request
public function store(Request $request)
{
// updating the amount paid
$rent = new Rent;
$rent->amount_paid = $request->amount_paid;
try {
$rent->save();
} catch (\Illuminate\Database\QueryException $e) {
//return to the form and populate it
return back()->withInput();
}
return redirect('units/'.$request->unit_id);
** update **
It appear that session->flash is not working.
I tried a new installation of laravel and it is working fine
The problem was related to session it self.
I have 2
\Illuminate\Session\Middleware\StartSession::class,
one in middleware and another in $middlewareGroups[web].
this was a result of following a tutorial in youtube months ago to implement localisation :(
I'm trying to get the ID of the share, but, I'm do a foreach in links, look:
#foreach($user->getRelation('shares')->pluck('links') as $link)
{{ $link->first()->title }} //works
{{ //get the ID, of the current share }}
#endforeach
How can I do it?
dd($user->shares);
Collection {#338 ▼
#items: array:3 [▼
0 => Share {#343 ▼
#attributes: array:6 [▼
"id" => 7
"link_id" => 3
"user_id" => 1
"shared_in" => null
"content" => "testetste"
"created_at" => "2018-02-02 15:46:13"
]
#relations: array:1 [▼
"links" => Collection {#349 ▶}
]
}
1 => Share {#344 ▶}
2 => Share {#345 ▶}
]
}
You can specify id as the second parameter for pluck() method:
#foreach($user->getRelation('shares')->pluck('links', 'id') as $id => $link)
{{ $link->first()->title }}
{{ $id }}
#endforeach
In this instance, your shares? are already shared with the view in their entirety and so plucking the links and ids doesn't really save you much performance meaning this loop is unnecessarily complicated.
#foreach($user->getRelation('shares') as $share)
{{ $share->links->first()->title }}
{{ $share->id }}
#endforeach
If however, all you want to use in this view instance is the link and id, then I would recommend modifying your query in the controller to reduce the size of the overall collection being sent to your view instance.
Hi im trying to pass all the products in the same category to a view
my products table has cat_id which related to cat_id in the categories table,
i got all the data from a product id to a single product view and i want to show related products to that product too
here my passing to product page's controller
public function product($id){
$product = Products::where('pro_id',$id)->Paginate(1);
$products=Products::where('cat_id',$product->cat_id)->get();
return view('front.product.product', compact('product','products'));
}
the $product variable works
but not the $products variable
i get the following error when try to uses $products variable
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$cat_id
and my variables when trying to pass to view
#foreach($product as $show)
<div class="col-md-9">
<div class="col-md-5 grid">
<div class="thumb-image"> <img src="{!! url('public/backend/images/products/'.$show->pro_img) !!}" data-imagezoom="true" class="img-responsive" style="width: 305px; height: 400px;"> </div>
</div>
<div class="col-md-7 single-top-in">
<div class="single-para simpleCart_shelfItem">
<h1>{{$show->pro_title}}</h1>
<p>{{$show->pro_descript}}</p>
<label class="add-to item_price">{{$show->pro_price}}</label>
<div class="available">
<h6>Description</h6>
<h4>{{$show->pro_detail}}</h4>
</div>
Add to cart
</div>
</div>
<div class="clearfix"> </div>
<div class="content-top1">
<div class="clearfix"> </div>
</div>
#foreach($products as $related)
<div class="col-md-4 col-md3">
<div class="col-md1 simpleCart_shelfItem">
<a href="single.html">
<img class="img-responsive" src="{!! url('public/backend/images/products/'.$related->pro_img) !!}" alt="" />
</a>
<h3>{{$related->pro_detail}}</h3>
<div class="price">
<h5 class="item_price">{{$related->pro_price}}</h5>
Add To Cart
<div class="clearfix"> </div>
</div>
</div>
</div>
#endforeach
</div>
#endforeach
when i dd($product) with paginate(1)
LengthAwarePaginator {#237 ▼
#total: 1
#lastPage: 1
#items: Collection {#226 ▼
#items: array:1 [▼
0 => Products {#232 ▼
#table: "tbl_products"
#primaryKey: "pro_id"
+timestamp: false
#filltable: array:9 [▼
0 => "cat_id"
1 => "pro_title"
2 => "pro_descript"
3 => "pro_detail"
4 => "pro_img"
5 => "quantity"
6 => "pro_date"
7 => "pro_price"
8 => "pro_status"
]
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▼
"pro_id" => 2
"cat_id" => 9
"pro_title" => "veston "
"pro_descript" => "look good"
"pro_detail" => "blue jean"
"pro_img" => "1516593265.jpg"
"quantity" => 8
"pro_date" => "2018-01-22 10:54:25"
"pro_price" => 400.0
"pro_status" => 0
"created_at" => "2018-01-22 03:54:25"
"updated_at" => "2018-01-22 03:54:25"
]
#original: array:12 [▼
"pro_id" => 2
"cat_id" => 9
"pro_title" => "veston "
"pro_descript" => "look good"
"pro_detail" => "blue jean"
"pro_img" => "1516593265.jpg"
"quantity" => 8
"pro_date" => "2018-01-22 10:54:25"
"pro_price" => 400.0
"pro_status" => 0
"created_at" => "2018-01-22 03:54:25"
"updated_at" => "2018-01-22 03:54:25"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
}
]
}
#perPage: 1
#currentPage: 1
#path: "http://localhost:81/shop/product/2"
#query: []
#fragment: null
#pageName: "page"
}
$product = Products::where('pro_id',$id)->Paginate(1);
this code returns array
cahnge code to like below:
$product = Products::where('pro_id',$id)->first();
Here you are calling a paginator:
$product = Products::where('pro_id',$id)->Paginate(1);
Try this instead:
$product = Products::where('pro_id',$id)->first();
-or-
$product = Products::where('pro_id',$id)->paginate(1)[0];
-or finally-
$product = Product::findOrFail($id);
The latter would only work if you've set protected $primaryKey = 'pro_id'; within your class.
The details:
When you called $product = Products::where('pro_id',$id)->Paginate(1);' you are getting aPaginatorinstance back from the data layer. ThePaginatordoesn't have the properties that aProduct` does in your example, which is where the error is coming from.
A paginator is a different type of collection, but it is a type of collection, also. Think about it like an array or like a Java style ArrayList which is a complex wrapper class for an array that provides additional funcitonality. A paginator is similar, but with an extra level of abstraction to provide view based pagination. You can access objects on the 'page' within the paginator similarly to how you can get array-like access to objects in a collection.
If you just return a collection, then doing
$product = Products::where('pro_id',$id)->get();
Gives you the the matching id as a collection. In order to actually see the object you want you would need to do this:
$product = Products::where('pro_id',$id)->paginate(1)[0];
to get the first item in that collection.
I am using Laravel and Vue js, and I can not figure out how to validate this field using Laravel.
Vue-component:
<div v-for="problem in problems">
<div>
<label :for="problem.id">{{problem.title}}</label>
<input type="text" class="form-control" :id="problem.id"
v-model="problem.rating">
</div>
</div>
This returns an array
array:2 [
"problems" => array:2 [
0 => array:2 [
"id" => 33
"rating" => "12"
]
1 => array:2 [
"id" => 38
"rating" => "3"
]
]
]
Now I do not know how to validate this using Laravels validation. I have tried a lot of things like
$this->validate($request, [
'rating' => 'required|integer',
]);
But this does of course not work here. Can anyone please help me?
EDIT:
I figured it out. Needed to use
$this->validate($request, [
'problems.*.rating' => 'required|integer',
]);