laravel 5.4 pagination issue invalid argument supplied for foreach() - laravel-5

I have around 50 objects in an array.
$user = User::with('abc','def','ghi','jkl','mno','pqr')
->where([['id', '<>', Auth::user()->id],['role', '<>', 2]])->simplePaginate(15);
$users = json_decode($user,true);
Have I placed ->simplePaginate(15) this correctly? Can I do json_decode after that? like I am doing? I dont get any error in controller.
In Views
#section('content')
<div class="container-fluid">
<div></br></br></div>
#include('errors.list')
<div class="row">
#foreach($users as $user)
<a href="#" onclick=document.getElementById('{{$user->id}}').style.display='block'>
<div class="col-md-3" style="margin-bottom:50px;">
#if($user["search_status"] == 'matched')
<div class="w3-card-12-disabled" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#else
<div class="w3-card-12" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#endif
<img align="center" src="{{$user->photo}}" style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;" width="150px !important" height="150px !important" />
<h3 style="color:#000;
padding-top: 20px;" align="center">{{ $user->firstname}}</br>{{ $user->lastname}}</h3>
<div style="display:flex; justify-content: center;">
<div class="tooltip2">
<img align="middle" style="" src="{{$user->natives->flag}}"/>
<span class="tooltiptext">{{$user->natives->language}}</span>
</div>
</div>
</br>
</div>
</div>
</a>
#endforeach
<div class="row">
#foreach($users as $user)
<div id="{{$user->id}}" class="w3-modal">
<div class="w3-modal-content w3-animate-top w3-card-8">
<div id="div1" style="padding:40px 30px 30px 30px;">
<img style="border-radius:50%; border:5px solid #333; margin-left:18px" src="{{$user->photo}}" width="200px !important" height="200px !important">
<div style="text-align: center; padding-top:70px;">
{!! Form::open(['url' => '/users/'. $user->id.'/reportc']) !!}
{!! Form::hidden('_method', 'POST') !!}
{!! Form::hidden('friend', $user->id, null, ['class'=>'form-control']) !!}
{!! Form::submit('Report', ['class'=>'btn', 'style' => 'padding: 10px 10px 10px 10px; color:#000; border-color:#000;']) !!}
{!! Form::close() !!}
</div>
</div>
<div id="div2" style="padding:40px 0px 40px 0px;">
<span onclick=document.getElementById('{{$user->id}}').style.display='none'
class="w3-closebtn">×</span>
<h1>{{ $user->firstname}} {{ $user->lastname}}</h1>
</br>
</br>
<h5 style="padding-top:30px;"><b>About Me:</b> {{ $user->bio}} </h5>
<h5 style="padding-top:10px;"><b>Campus:</b> {{$user->campus->campus_name}}</h5>
<h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5>
<h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5>
<h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5>
<h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{ $user->snatives->language }} </h5>
<h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
#foreach($seeks as $seek)
#if($seek->user_id === $user->id)
{{ $seek->language}},
#endif
#endforeach </h5>
</div>
</div>
</div>
#endforeach
</div>
{!! $users->links() !!}
</div>
</div>
#endsection
Even if I dont put this code {{ $users->links() }} in view I still get same error. I guess issue is with the pagination code on controller.
Edited
I have this small issue still:
Before
<h5 style="padding-top:10px;"><b>Campus:</b> {{$user['campus']['campus_name']}}</h5>
<h5 style="padding-top:10px;"><b>School:</b> {{ $user['schools']['schools_name']}} </h5>
<h5 style="padding-top:10px;"><b>Year:</b> {{ $user['years']['years_name']}} </h5>
<h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user['natives']['language']}} </h5>
<h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user['snatives']['language']}} </h5>
<h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
#foreach($seeks as $seek)
#if($seek->user_id === $user['id'])
{{ $seek->language}},
#endif
#endforeach </h5>
After
<h5 style="padding-top:10px;"><b>Campus:</b> {{$user->campus->campus_name}}</h5>
<h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5>
<h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5>
<h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5>
<h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user->snatives->language}} </h5>
<h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
#foreach($seeks as $seek)
#if($seek->user_id === $user->id)
{{ $seek->language}},
#endif
#endforeach </h5>
This gives After code gives me error trying to get property of non-object which is why I was using json_decode() and Before code. How do I fix these?
result
{
"per_page":12,
"current_page":1,
"next_page_url":"http:\/\/localhost:8000\/users\/community?page=2",
"prev_page_url":null,
"from":1,
"to":12,
"data":[
{
"id":5,
"firstname":"Beth",
"lastname":"Hanley",
"email":"bah3#org.uk",
"role":1,
"photo":"uploads\/2.jpg",
"bio":"Hockey fan, foodie, gamer, Saul Bass fan and HTML5 Guru.",
"campus":{
"id":1,
"campus_name":"name"
},
"school":"1",
"year":"1",
"native_lang":"2",
"native_lang_flag":"2",
"search_status":"available",
"created_at":null,
"updated_at":null,
"lastlogin":null,
"verification_code":null,
"isverify":"0",
"native_lang_2":null,
"native_lang_flag_2":null,
"schools":{
"id":1,
"schools_name":"School of Energy, Geoscience, Infrastructure and Society"
},
"years":{
"id":1,
"years_name":"First"
},
"flags":{
"id":2,
"name":"\u00c5land Islands",
"flag":"\/flags\/AX-32.png"
},
"natives":{
"id":2,
"language":"English",
"flag":"\/flags\/GB-32.png"
},
"snatives":null
},
User Model
public function friends()
{
return $this->hasMany('App\Friend','friend_id','id');
}
public function seeks()
{
return $this->belongsToMany('App\Language');
}
public function natives()
{
return $this->hasOne('App\Language','id','native_lang');
}
public function snatives()
{
return $this->hasOne('App\Language','id','native_lang_2');
}
public function roles()
{
return $this->hasOne('App\Role','id','role');
}
public function campus()
{
return $this->hasOne('App\Campus','id','campus');
}
public function schools()
{
return $this->hasOne('App\School','id','school');
}
public function years()
{
return $this->hasOne('App\Year','id','year');
}
public function flags()
{
return $this->hasOne('App\Flag','id','native_lang_flag');
}
Campus Model
public function users()
{
return $this->belongsTo('App\User','campus','id');
}
Year Model
public function users()
{
return $this->belongsTo('App\User','year','id');
}

You don't need to json encode anything and access the object property directly.
Controller
$users = User::with('abc','def','ghi','jkl','mno','pqr')
->where([
['id', '<>', auth()->id()],
['role', '<>', 2]
])->simplePaginate(15);
return view('someview', compact('users'));
View
<div class="row">
#foreach($users as $user)
<a href="#" onclick="document.getElementById('{{ $user->id }}').style.display='block'">
<div class="col-md-3" style="margin-bottom:50px;">
#if($user->search_status == 'matched')
<div class="w3-card-12-disabled"
style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#else
<div class="w3-card-12"
style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#endif
<img align="center" src="{{ $user->photo }}"
style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;"
width="150px !important" height="150px !important"/>
<h3 style="color:#000;
padding-top: 20px;" align="center">{{ $user->firstname }}<br/>{{ $user->lastname}}</h3>
<br/>
</div>
</div>
</div>
</a>
#endforeach
</div>
{!! $users->links() !!}

Related

Display image with relations in Laravel

I want to display through the user's photo relationships but it does not show
And this photo also exists, but when I take dd(), it says null
this is Blade code And they all work except the user_image
#foreach($threads as $thread)
<div class="card my-3 rounded-lg mr- ">
<div class="card-body shadow ">
<blockquote class="blockquote mb-0">
<div class=" d-flex">
<p style="font-size: x-large; font-weight: bold; "><a class="" href="/threads/{{$thread->slug}}">{{\Illuminate\Support\Str::limit($thread->title,50)}}</a>
</p>
</div>
<div class=" d-flex">
<img class="img-profile rounded-circle mr-1 " style="width: 60px; height: 60px;"
src="{{$thread->user->user_image}}"> {{-- this is not working--}}
<p class="mt-3 text-secondary"
توسط {{$thread->user->name}} آپدیت شد</p>
</div>
<footer
class="blockquote-footer text-dark">{{\Illuminate\Support\Str::limit($thread->description,150)}}
</blockquote>
</div>
</div>
#endforeach
Maybe replace
public function user()
{
return $this->belongsTo(User::class);
}
by something like
public function images()
{
return $this->belongsTo(User::class);
}
and for users do
hasMany (images)
Also you have to do migrations to add foreign keys, did you do this ?

i am not able to find order through id in laravel

I am new to shopping cart. I am able to get all the data from order table on this page but i am not able to find single order by ID:
This is my view page where i am sending the $id to the controller of the order.
id)}}">Order# {{$order->id}}
The whole code:
'''
#foreach($orders as $order)
#if($order->status == "CompletedOrders")
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group" >
<li class="list-group-item" style="background-color: #5C94ED">
<h1 style="color: #FBEB58">Order# {{$order->id}}</h1>
<span class="badge" style="color: black; font-size: 16px">Name: {{$order->name}}</span>
<br>
<span class="badge" style="color: black; font-size: 16px">Phone: {{$order->phone}}</span>
<br>
#foreach($order->cart->items as $item)
<span class="badge" style="color: #FBEB58; font-size: 16px">{{$item['qty'] }} {{ $item['item']['name'] }}</span>
<br>
<span class="badge" style="color: #FBEB58; font-size: 16px">Price: ${{ $item['price'] }}</span>
<br>
#endforeach
<span class="badge" style="color: black; font-size: 16px">Instructions: {{$order->table}}</span>
<br>
<span class="badge" style="color: black; font-size: 16px">Time: {{$order->created_at}}</span>
<br>
</ul>
</div>
<div class="panel-footer">
<strong style="color: #222D32; font-size: 18px">Total Price: ${{ $order->cart->totalPrice }}</strong>
</div>
</div>
<input type="button" onclick="printDiv('printableArea')" value="print a div" />
#endif
#endforeach
'''
This is my route
'''
Route::get('showcompleteorder/{id}','OrderController#showCompleteOrder')->name('showcompleteorder');
'''
and this is my controller:
public function showCompleteOrder($id){
$orders = Order::find($id);
$orders->transform(function ($order, $key) {
$order->cart = unserialize($order->cart);
return $order;
});
return view('orders.showcompleteorders', ['orders' => $orders]);
}
but when i run this. it gives me a error of transform:
BadMethodCallException
Call to undefined method App\Order::transform()
http://localhost:8000/showcompleteorder/2
This is my view where i am trying to get the order by id:
#foreach($orders as $order)
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group" >
<li class="list-group-item" style="background-color: #5C94ED">
<h1 style="color: #FBEB58">Order# {{$order->id}}</h1>
<span class="badge" style="color: black; font-size: 16px">Name: {{$order->name}}</span>
<br>
<span class="badge" style="color: black; font-size: 16px">Phone: {{$order->phone}}</span>
<br>
#foreach($order->cart->items as $item)
<span class="badge" style="color: #FBEB58; font-size: 16px">{{$item['qty'] }} {{ $item['item']['name'] }}</span>
<br>
<span class="badge" style="color: #FBEB58; font-size: 16px">Price: ${{ $item['price'] }}</span>
<br>
#endforeach
<span class="badge" style="color: black; font-size: 16px">Instructions: {{$order->table}}</span>
<br>
<span class="badge" style="color: black; font-size: 16px">Time: {{$order->created_at}}</span>
<br>
</ul>
</div>
<div class="panel-footer">
<strong style="color: #222D32; font-size: 18px">Total Price: ${{ $order->cart->totalPrice }}</strong>
</div>
</div>
#endforeach
Can Someone please help me

Nested replies almost done, only need to arrange them in view

I'm currently doing a small forum in Laravel. This will include(ofcourse) commenting on posts. I also done replies on top of comments. I've done this by adding post_id, commenter_id and parent_id field in Comment model. And that's all alright.And i also done small margin that each reply have depending on their parent .What i have trouble with is displaying appropriate reply, for example: Comment1, then reply to comm.1, then another reply to that reply and so on, then another direct reply on Comment1, every subsequent reply to reply after second direct comment on Comment1 isn't grouped with other replies. How to achieve that?
Here is image of what i'm trying to achieve:
Here is Comment model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Post;
use App\User;
class Comment extends Model
{
protected $fillable = [
'user_id',
'post_id',
'parent_id',
'body'
];
public function commentPost(){
return $this->belongsTo("App\Post","post_id");
}
public function commentAuthor(){
return $this->belongsTo("App\User","commenter_id");
}
public function replies() {
return $this->hasMany('App\Comment', 'parent_id');
}
}
Here is show view:
#extends("layouts.app")
#section("content")
<?php
/*Pregleda zasebno za title i description da li ima gresaka.
Ukoliko ih ima, dodeljume im klasu(crveni border).*/
$errBody = $errors->has('body') ? 'shake' : '';
?>
Go Back
<div style="float:right;">
<i class="fas fa-arrow-left"></i>
<i class="fas fa-arrow-right"></i>
</div>
<h1>{{$post->title}}</h1>
<div class="postContainer">
<img class="postCover" src="/storage/cover_images/{{$post->cover_image}}">
<div>
<!--Ovako prikazuje html kod.-->
<!--{{$post->body}}-->
{!!$post->body!!}
</div>
<small class="timestamp">Written on {{$post->created_at}}</small>
</div>
#if(!Auth::guest())
#if(Auth::check() && Auth::user()->role_id<3 && Auth::user()->role_id>0)
<hr>
Edit
<form action="/posts/{{$post->id}}" method="post" class="float-right">
#csrf
{{method_field("DELETE")}}
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Are yoy sure you want to delete this post?</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<button type="submit" class="btn btn-outline-danger btn-sm">Delete</button>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</form>
<button class="btn btn-outline-danger btn-sm float-right" data-toggle="modal" data-target="#myModal">Delete</button>
#endif
#endif
#auth
<hr>
#if(Auth::check() && Auth::user()->role_id<3 && Auth::user()->role_id>0)
<form method="POST" action="/posts/{{$post->id}}/comments">
#csrf
<!-- https://laravel.com/docs/5.7/validation#available-validation-rules -->
<div class="form-group animated {{$errBody}}">
<textarea id="ckeditor" class="form-control" name="body" placeholder="Post body" required value=""></textarea>
</div>
<div>
<button type="submit" class="btn btn-outline-success btn-sm">Post comment</button>
</div>
</form>
#endif
<hr>
#endauth
<h6 style="border-bottom: 1px solid whitesmoke;">Comments <span class="badge" style="background-color: whitesmoke; border: 1px solid silver;vertical-align: top;">{{count($comments)}}</span></h6>
<div class="container commContainer">
<ul class="list-group" style="list-style-type:none">
#if(count($comments) > 0)
#foreach ($comments as $index => $comment)
<li>
#if($index>0)
<div class="horDiv"></div>
#endif
{{$comment->commentAuthor->name}} has said parent_id: {{$comment->parent_id}}
</li>
<li class="list-group-item py-2 commBody" style="margin-left: calc({{$comment->parent_id}}*10px) !important" parent_id="{{$comment->parent_id}}" id="{{$comment->id}}">My id: {{$comment->id}} {!!$comment->body!!}
#if(Auth::check() && Auth::user()->role_id<3 && Auth::user()->role_id>0)
<br><span class="btn btn-outline-primary btn-sm" style="width: 40px;height: 20px;line-height: 5px;padding: 5px;" onclick="reply('reply{{ $comment->id }}')";>reply</span>
#include('inc.replyForm')
#endif
</li>
#endforeach
#endif
</ul>
</div>
#endsection
I'm just guessing, but can it be done by in view itself?I tried grouping and ordering before controller passes it to view, but nothing had desired effect. I appreciate every point in right direction.
Edit:
Here is store method in CommentsController.
public function store(Request $request, $id)
{
$this->validate($request, [
"body" => "required"
]);
$data = array(
"body" => $request->input("body"),
"post_id" => intval($id),
"commenter_id" => Auth::user()->id,
"imParent_id" => $request->input("comment_id")
);
//dd($data);
$comment = new Comment;
$comment->body = $data["body"];
$comment->post_id = $data["post_id"];
$comment->commenter_id = $data["commenter_id"];
$comment->parent_id = $data["imParent_id"];
$comment->save();
$post = Post::find($id);
$comments = $post->comments;
//return redirect("/posts/{{$id}}")->with("success", "Post Created")->with('comments', $comments);
//return view("posts.show")->with(compact('post', 'comments'));
//with(['replies' => function($comments){ $comments->orderBy('parent_id') } ])
return back()->with('comments', $comments);
}
Edit2:
And here is show method in PostController:
public function show($id)
{
$post = Post::find($id);
$comments = $post->comments;
$prev = $post->prev($post);
$next = $post->next($post);
return view("posts.show")->with(compact("post", "prev", "next", "comments"));
}
Edit3:
Here is rather flawed attempt(in show view):
#if(count($comments) > 0)
#for($i=0;$i<count($comments);$i++)
#if(!$comments[$i]->parent_id)
<li class="list-group-item py-2 commBody" parent_id="{{$comments[$i]->parent_id}}" id="{{$comments[$i]->id}}">
{!!$comments[$i]->body!!}
</li>
#for($k=0;$k<count($comments);$k++)
#if($comments[$i]->id==$comments[$k]->parent_id)
<?php array_push($myArray,$comments[$i]->id); ?>
<li class="list-group-item py-2 commBody" style="margin-left: calc({{$comments[$k]->parent_id}}*10px) !important" parent_id="{{$comments[$k]->parent_id}}" id="{{$comments[$k]->id}}">
{!!$comments[$k]->body!!}
</li>
#endif
#endfor
{{"///////////////////////////////////////////////"}}
#endif
#if(array_search($comments[$i]->parent_id,$myArray)===false)
#for($k=0;$k<count($comments);$k++)
#if($comments[$i]->id==$comments[$k]->parent_id)
<li class="list-group-item py-2 commBody" style="margin-left: calc({{$comments[$k]->parent_id}}*10px) !important" parent_id="{{$comments[$k]->parent_id}}" id="{{$comments[$k]->id}}">
{!!$comments[$k]->body!!}
</li>
#endif
#endfor
#endif
#endfor
#endif
And how it looks along with duplicates:
Edit4:
Followed Tim Lewis advice of separating comments(parentless) from replies(with parents), here's how to displayed them and sending additional parameter to reply form partial:
<ul class="list-group" style="list-style-type:none">
#if(count($comms) > 0)
#for($i=0;$i<count($comms);$i++)
<li class="list-group-item py-2 commBody" style="margin-left: calc({{"0"}}*10px) !important" parent_id="{{$comms[$i]->parent_id}}" id="{{$comms[$i]->id}}">
{{$comms[$i]->body}}
#if(Auth::check() && Auth::user()->role_id<3 && Auth::user()->role_id>0)
<br><span class="btn btn-outline-primary btn-sm" style="width: 40px;height: 20px;line-height: 5px;padding: 5px;" onclick="reply('reply{{ $comms[$i]->id }}')";>reply</span>
#include('inc.replyForm', array('par' => $comms[$i]))
#endif
</li>
<li>#if($i>0) <div class="horDiv"></div> #endif </li>
#if(count($replies) > 0)
#for($j=0;$j<count($replies);$j++)
#if($comms[$i]->id==$replies[$j]->parent_id)
<li class="list-group-item py-2 commBody" style="margin-left: calc({{$comms[$i]->id}}*10px) !important" parent_id="{{$replies[$j]->parent_id}}" id="{{$replies[$j]->id}}">
{{$replies[$j]->body}}
#if(Auth::check() && Auth::user()->role_id<3 && Auth::user()->role_id>0)
<br><span class="btn btn-outline-primary btn-sm" style="width: 40px;height: 20px;line-height: 5px;padding: 5px;" onclick="reply('reply{{ $replies[$j]->id }}')";>reply</span>
#include('inc.replyForm', array('par' => $replies[$j]))
#endif
</li>
<li>#if($j>0) <div class="horDiv"></div> #endif </li>
#endif
#for($k=$j;$k<count($replies);$k++)
#if($replies[$k]->parent_id==$replies[$j]->id)
<li class="list-group-item py-2 commBody" style="margin-left: calc({{$replies[$j]->id}}*10px) !important" parent_id="{{$replies[$k]->parent_id}}" id="{{$replies[$k]->id}}">
{{$replies[$k]->body}}
#if(Auth::check() && Auth::user()->role_id<3 && Auth::user()->role_id>0)
<br><span class="btn btn-outline-primary btn-sm" style="width: 40px;height: 20px;line-height: 5px;padding: 5px;" onclick="reply('reply{{ $replies[$k]->id }}')";>reply</span>
#include('inc.replyForm', array('par' => $replies[$k]))
#endif
</li>
<li>#if($k>0) <div class="horDiv"></div> #endif </li>
#endif
#endfor
#endfor
#endif
#endfor
#endif
</ul>
I did it before for making categories. Here is a tip:
<select name="category_id" class="form-control chosen-select" required value="{{ old('category_id') }}">
#foreach(App\Category::where('parent_id',0)->get() as $c)
<option value="{{$c->id}}">*{{$c->name}}</option>
#foreach(App\Category::where('parent_id',$c->id)->get() as $d1)
<option value="{{$d1->id}}"> - {{$d1->name}}</option>
#endforeach
#endforeach
</select>

"Undefined offset: 1" error in Laravel. What is going on?

I´m trying to pass in my view all posts I have in my database with a foreach, but this error pops up.
Here is my view:
<ul>
{{ <li>#foreach ($posts => $posts) }}
<div class="row section scrollspy" style="padding:10px; margin-top:10px;">
<div class="content-box col s12" style="padding:20px; margin-top:40px; background: #f2f2f2; border-radius:10px;">
<div class="i_title" style="text-align:center; margin: -57px 0 0 0;">
<img class="responsive-img circle" src="images/profile_picture.jpg" style="width:60px; box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);"><span class="tooltiptext">#Username{{ $user->username }}</span>
</div>
<p class="col s12"{{ $post->post }}</p>
</div>
</div>
{{ #endforeach</li> }}
</ul>
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function getUsers()
{
$user = DB::table('users')->get();
return view('layouts/welcomeView', ['users' => $user]);
}
public function getPosts()
{
$posts = DB::table('posts')->get();
return view('layouts/welcomeView', ['posts' => $posts]);
}
}
My database have some posts, so thats not the problem. I think that the trouble is that I´m not passing well the variable with the posts. What can I do??
A couple of things wrong with this.
Firstly, you're mixing blade directives up. {{ }} is used to echo something out. Whatever is inside {{ and }} is interpreted as PHP and is passed into a function to do some escaping and it is echoed out. So when you're doing {{ <li>#foreach ($posts => $posts) }}, the Blade compiler tries to treat <li>#foreach ($posts => $posts) as PHP code which will result in a syntax error. The #foreach doesn't belong inside {{ }}.
Secondly, I noticed inside your #foreach that you have $posts => $posts. Notice that the variables on each side of the arrow is the same? That is no doubt a typo but it would result in the first iteration of the loop overwriting the $posts variable with the first entry and the loop would try to continue iterating over this new, single value. It's likely that this is causing the error you're seeing.
This is probably closer to what you're looking for:
<ul>
#foreach ($posts => $post)
<li>
<div class="row section scrollspy" style="padding:10px; margin-top:10px;">
<div class="content-box col s12" style="padding:20px; margin-top:40px; background: #f2f2f2; border-radius:10px;">
<div class="i_title" style="text-align:center; margin: -57px 0 0 0;">
<img class="responsive-img circle" src="images/profile_picture.jpg" style="width:60px; box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);"><span class="tooltiptext">#Username{{ $user->username }}</span>
</div>
<p class="col s12"{{ $post->post }}</p>
</div>
</div>
</li>
#endforeach
</ul>
Change your view to;
<ul>
<li>
#foreach ($posts => $post)
<div class="row section scrollspy" style="padding:10px; margin-top:10px;">
<div class="content-box col s12" style="padding:20px; margin-top:40px; background: #f2f2f2; border-radius:10px;">
<div class="i_title" style="text-align:center; margin: -57px 0 0 0;">
<img class="responsive-img circle" src="images/profile_picture.jpg" style="width:60px; box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);"><span class="tooltiptext">#Username{{ $user->username }}</span>
</div>
<p class="col s12"{{ $post->post }}</p>
</div>
</div>
#endforeach
</li>
</ul>
You might need to move #Username if that isn't defined somewhere.
It is generally good practice to check there is something in an array before you foreach it.
You can achieve this by doing;
#if(! $posts->isEmpty())
#foreach ($posts => $post)
....
#endforeach
#endif

"Property does not exist on this collection instance" error while joining in laravel 5.4

UserAcademyController.php
public function view_video()
{
$materialList = DB::table('material')
->join('material_group', 'material.group_id', '=', 'material_group.id')
->select('material.*', 'material_group.group_thumbnail_url')
->get();
return view('user-academy.view_video', ['materialList' => $materialList]);
}
view_video.blade.php
#foreach ($materialList as $material)
<div class="col-sm-6 col-lg-4 col-md-4">
<div class="thumbnail">
<a href="{!! url('user-academy/view/' . $material->id)!!}">
#if($material->thumbnail_url !== null)
<img src="{{$material->thumbnail_url}}"
style="width: 700px; height: 300px">
#elseif($materialList->group_thumbnail_url !== null)
<img src="{{$materialList->group_thumbnail_url}}"
style="width: 700px; height: 300px">
#elseif($material->thumbnail_url !== null && $materialList->group_thumbnail_url !== null)
<img src="http://www.independentmediators.co.uk/wp-content/uploads/2016/02/placeholder-image.jpg"
style="width: 700px; height: 300px">
#endif
</a>
<div class="caption">
<h4 class="pull-right text-success">${{$material->price}} </h4>
<h4>
{{ $material->title }}
</h4>
<hr>
<p class="description">{!! $material->description !!} </p>
</div>
</div>
</div>
#endforeach
This is the "material" table
This is the "material_group" table
I get this error even though I copied everything exactly from laravel documentations and followed many videos on this topic I am a new programmer so this may be a very obvious mistake but I have spent a fair share of time looking at the code and just cannot find where the error is!
You are trying to access group_thumbnail_url property on Collection class which is for sure not exist, that's why you got that error.
the solution is to replace $materialList->group_thumbnail_url by material->group_thumbnail_url

Resources