I'm trying to fetch values at view passed from controller.
In my controller my syntax are:
public function index()
{
$vehicles=vehicles::orderBy('created_at', 'desc')->get();
// return $vehicles;
$ad=ads::orderBy('views','desc')->get();
// return $ad;
foreach ($ad as $ads) {
# code...
$popularvehicle[]=vehicles::where('id',$ads->id)->get();
// echo $popularvehicle;
}
return view('index',compact('vehicles','popularvehicle'));
}
In my views i've tried following:
#foreach($popularvehicle as $popularvehicle)
{{$popularvehicle->vname}}
#endforeach
It gives an error of Undefined property: Illuminate\Database\Eloquent\Collection::$vname
I've also tried {!!$popularvehicle['vname']!!}. But it throws error like undefined index.
When i echo {!!$popularvehicle!!} it gives all the values required like [{"id":3,"vname":"Pulsar","lotno":"Ba 25 Pa","engine":"150","mileage":"35","kilometers":25000,"price":"120000","negotiable":"Yes","vcondition":"New","used":"3 month","manufacture_year":"2015","description":"Almost New","Company_cid":1,"Users_id":1,"Vehicle_Type_id":1,"created_at":"2017-01-12 15:08:41","updated_at":"2017-01-12 15:08:41"}].
How can i fetch the values of $popularvehicle? Can anyone help me? Will converting array to object help solve this problem. If yes, how can i do so?
The error is because -> is trying to point to the property of object $popularvehicles[] is an array:
$ads=ads::orderBy('views','desc')->get();
foreach ($ads as $ad) {
$popularvehicles[]=vehicles::where('id',$ad->id)->get()->toArray();
}
and then,
#foreach($popularvehicles as $popularvehicle)
#foreach($popularvehicle as $vehicle)
{{$vehicle['vname']}}
#endforeach
#endforeach
Note the changes made for naming conventions. Also, Model name is good to be singular.
Consider plural name
$popularvehicles = array();
foreach ($ad as $ads) {
$popularvehicles[]=vehicles::where('id',$ads->id)->get();
}
return view('index',compact('vehicles','popularvehicles'));
And use in view
#foreach($popularvehicles as $popularvehicle)
{{$popularvehicle->vname}}
#endforeach
Try this:
Controller:
$popularvehicle = vehicles::where('id',$ads->id)->get();
// If you use $popularvehicles[] then you have to use one extra foreach() to retrieve the columns
View:
#foreach($popularvehicle as $vehicle)
{{$vehicle->id}} // for id
{{$vehicle->vname}} // for name
#endforeach
Try this:
public function index()
{
$vehicles = vehicles::orderBy('created_at', 'desc')->get();
// return $vehicles;
$ads = ads::orderBy('views','desc')->get();
// return $ad;
foreach ($ads as $ad) {
# code...
$popularvehicles[]=vehicles::find($ad->id);
}
return view('index',compact('vehicles','popularvehicles'));
}
And in your view:
#foreach($popularvehicles as $popularvehicle)
{{$popularvehicle->vname}}
#endforeach
Related
Controller:
public function dashboard()
{
$course = Course::all();
return view('content.dashboard',compact('course'));
}
View:
#foreach($course as $year)
$sub = \App\Models\SubCourse::where('course_id',$year->id)->first();
#dd($sub->id);
#endforeach
In the above code I am trying to get value of SubCourse but it throw an error i.e. Undefined variable: sub (View: C:\xampp\htdocs\example\resources\views\content\dashboard.blade.php). I don't know why? Please help me.
Thank you
You Can Accessing Model Like this in PHP Tag
#foreach($course as $year)
<?php
$sub = \App\Models\SubCourse::where('course_id',$year->id)->first();
#dd($sub->id);
?>
#endforeach
public function index2(){
$s_books = raws::orderBy('sequence', 'ASC')->where('type',"book")->get();
foreach ($s_books as $data) {
$menu="{$data->menu}";
$main_books = books::orderBy('id', 'DESC')->where("menu",$menu)->get();
}
return view('index')->with(['main_books'=>$main_books]);}
And this is Blade
#foreach($main_books as $data)
{{$data->id}}
#endforeach
But this is showing only the last data.
How can I show all tha data in $main_book in laravel blade???
The code you have, you will get only the last item, you need to put all in an array. Try this code in you controller.
$main_books = [];
foreach ($s_books as $data) {
$menu="{$data->menu}";
array_push($main_books, books::orderBy('id', 'DESC')->where("menu",$menu)->get());
}
how can I fetch one particular index from the array to delete? e.g. if I have array[a, b, c] in the array list and want to delete b which is index array[1] from it? Any help would be great.
I need to pass index to the controller so I can delete it
View
#foreach (json_decode($p->filename) as $picture)
<ul>
Delete
</ul>
#endforeach
Controller
public function deleteProductImageName($id) {
if(Auth::check()) {
$products = Product::where('id', $id)->first();
foreach($products as $p) {
if(($products->user_id == Auth::user()->id) && ($products->id == $id)) {
$product = Product::where('user_id', Auth::user()->id)
->where('id', $id)->first();
$filename_index = $product->filename;
echo $filename_index; '<br/>';
echo $filename_index . '[' . $index . ']';
}
}
} else {
Session::flash("message", "OOPS! You dont have permission to delete the items. Please login first.");
return redirect("/register-user");
}
}
UPDATED
Have a look at the Loop Variables.
To get the current index, you can use:
$name = $request->query('name');
EDIT
Ok I understand the question better. You want to add additional parameters to the request.
View Example
#foreach($items as $item)
Delete
#endforeach
Controller Example
public function delete($id) {
// get the query parameter
$index = $request->query('index');
}
what i am trying to do is getting my tvserious and movies from categories class
this is my categories class :
class Category extends Model
{
public function movies()
{
return $this->hasMany(Movie::class);
}
public function tvserious()
{
return $this->hasMany(Tvserious::class);
}
what i tried and it's working
public function CategoryClick($slug){
$media = Category::where('slugid',$slug)->with(['movies' => function($query) {
$query->whereNotNull('title');
},'tvserious' => function($query) {
$query->whereNotNull('title');
}])->inRandomOrder()->paginate(8);
return view('test')->with([
'catclick'=>$media,
'title'=>$slug,
]);
}
the problem with this way is in my blade i have to create a loop for movies and tvserious and the tvserious data will always stay at the end at it will show after the movies loop ends
#foreach($catclick as $media)
#foreach($media->movies as $movie )
{{ $movie->title }}
#endforeach
#foreach($media->tvserious as $tvserious )
{{ $tvserious->title }}
#endforeach
#endforeach
so how can i get both of my movies and serious in my blade mixed together
i don't want all the movies to be at first so where is the problem and how can i fix this ?
this worked pretty well for me ... #first
i made an accessor in my category class
public function getMediaAttribute()
{
return $this->movies->values()->toBase()
->merge($this->tvserious->values())
->sortByDesc(function ($media, $key) {
return $media->updated_at;
});
}
then in my controller
this is my controller function
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
public function CategoryClick($slug){
$all = Category::where('slugid',$slug)->first()->getMediaAttribute(); // calling the accessor
// Then use following code to paginate the results:
$perPage = 10;
$currentPage = app('request')->get('page') ?: 1; // or $request->get('page') if available
$paginator = new Paginator($all, $all->count(), $perPage, $currentPage);
return (dd($paginator));
everything is great now thx for everyone who helped me to solve thix issue :D
Add an accessor to your Category model:
class Category
{
public function getMediaAttribute()
{
return $this->movies->values()->toBase()
->merge($this->tvserious->values())
->sortByDesc(function ($media, $key) {
return $media->updated_at;
});
}
}
Here we used a base collection instead of the Eloquent collection due to merge issues with the Eloquent collection (it does not allow for duplicate keys, thanks #lagbox for pointing it out). After merging both media types together, we also sort the resulting list based on their last update. This means recently touched entries will be listed first.
The newly created list can be accessed with $category->media. Instead of sortByDesc($func) also a simple sortBy('column') or shuffle() (to get a random order) would work.
You can use this code
#php
$moviesCount = $media->movies->count();
$tvseriousCount = $media->tvserious->count();
$maxCount = ($tvseriousCount > $moviesCount) ? $tvseriousCount : $moviesCount;
#endphp
#for ($index = 0; $index < $maxCount; $index++)
#isset($media->movies[$index])
{{ $media->movies[$index]->title }}
#endisset
#isset($media->tvserious[$index])
{{ $media->tvserious[$index]->title }}
#endisset
#endfor
I have a function in my helper
function somethingOrOther($id)
{
$posts = Posts::where('author_id', $id);
$posts_count = [];
$posts_count['total'] = $posts->count();
$posts_count['published'] = $posts->where('status', 'published')->count();
$posts_count['draft'] = $posts->where('status', 'draft')->count();
return $posts_count;
}
and this function works perfectly fine. But now I'm trying use it in this function in a controller to return it to a view
public function profile($id)
{
$posts_count[]= somethingOrOther($id);
return view ('display.profile')->withPosts_count($posts_count['total'])->withPosts_published_count($posts_count['published'])->withPosts_draft_count($posts_count['draft']);
}
But I get an error of 'Undefined index: total'. What do I need to do to separately return the array values of $posts_count?
I belive you can just do
$posts_count= somethingOrOther($id);
and then return it like
return view ('display.profile',["posts_count" => $posts_count]);
and in blade get it like
{{$posts_count["total"]}}
Try with
$post_count = $this->somethingOrOther($id);
if ( !$post_count ) {
return "no posts for this author";
}
...
return view('display.profile', compact('post_count'));
In view
{{ $post_count['total'] }}