I want to print '$post' as an array from database.
<tbody>
#if (is_array($posts) || is_object($posts)){
#foreach ($posts as $post)
<tr>
<th>{{$post->id }}</th>
<td>{{$post->title }}</td>
<td>{{$post->body }}</td>
<td>{{$post->created_at }}</td>
<td>ViewEdit</td>
</tr>
#endforeach
#else
{{'no!'}}
#endif
</tbody>
it just prints no! this is the main function:
public function index()
{
$posts = Post::all();
return view('posts.index')->withPosts('$posts');
}
You should below way to pass the data from view file.
public function index()
{
$posts = Post::all();
return view('posts.index',compact('posts'));
}
You can use get method , get will return all of the results in the model's table. After that you can pass the result to your view.
public function index(){
$posts = Post::get();
return view('posts.index',compact('posts'));
}
Related
How can I paginate posts in single category page?
Code
public function show($slug) {
$category = Category::where('slug', $slug)->where('online', true)->with(['posts' => function ($q) {
$q->orderBy('id', 'desc');
//I need this query be paginate
}])->first();
//return....
}
my blade loop
<h1>{{$category->name}}</h1>
#foreach($category->posts as $post)
//post data here
#endforeach
The relationship of categories and posts is n:n;
Because you just need one category, you just need to get posts by one category.
Try to use another method:
public function show($slug) {
$cat = Category::where('slug', $slug)
->where('online', true)
->first();
$posts = Post::join('category_posts', 'category_posts.post_id', '=', 'posts.id')
->where('category_posts.category_id', $cat->id)
->selectRaw('posts.*')
->paginate(10);
//return....
}
And in your blade loop:
<h1>{{$cat->name}}</h1>
#foreach($posts as $post)
//post data here
#endforeach
here is the exact example which I always use in my several project.
Controller:
public function index(Request $request){
$parts = BikeParts::paginate(5); //you can change how many you want
return view('admin.bike_parts.index_excel', compact(['bike_parts']))->with('i', ($request->input('page', 1) - 1) * 5); // put exact number you are paginating , in here I used 5.
}
View:
<table class="table table-actions-bar m-b-0">
<tbody>
<thead>
<tr>
<th>id</th>
<th>PartNo.</th>
<th>Part Name</th>
<th>model</th>
<th>Retail Price</th>
</tr>
</thead>
#foreach($bike_parts as $bike_part)
<tr>
<td>{{$loop->index+1}}</td>
<td>{{$bike_part->part_number}}</td>
<td>{{$bike_part->description}}</td>
<td>{{$bike_part->bike_model}}</td>
<td>{{$bike_part->retail_price}}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $bike_parts->render() !!}
Instead of using first(), use paginate(count)
Here is an example according to your code,
public function show($slug) {
$category = Category::where('slug', $slug)
->where('online', true)
->with(['posts' => function ($q) {
$q->orderBy('id', 'desc');
}])->paginate(10);
//return....
}
$posts = Post::where('category_id', $categoryId)->paginate(10);
Heres a way that may be used to achieve paginated posts in multiple category
However, I suspect that this will make a N+1 query. Might need eager loading implemented with this. Can someone confirm this ?
// Category.php (Model)
public function posts()
{
return $this->hasMany('App\posts', 'post_id', 'id');
}
In the blade file
#foreach($categories as $category)
<h1>{{$category->name}}</h1>
#foreach($category->posts()->paginate() as $post)
//post data here
#endforeach
#endforeach
I am trying to build a shopping cart but it doesn't show what the cart contains.
Something is wrong here is my code in the controller and blade:
public function index()
{
$id = Auth::id();
$results = DB::table('carts')
->select('book_id')
->where('user_id', '=', $id)
->get();
$data = (array) $results;
$books = Book::all()->whereIn('id', $data);
return view('carts.index')->withBooks($books);
}
#foreach($books as $book)
<tr>
<td> {{ $book->autori }} </td>
<td> {{ $book->titulli }} </td>
<td> {{ $book->cmimi }}</td>
</tr>
#endforeach
public function index()
{
$booksIds = DB::table('carts')
->select('book_id')
->where('user_id','=', Auth::id())
->get()
->pluck('book_id')
->toArray();
$books = Book::whereIn('id', $booksIds)->get();
return view('carts.index')->withBooks($books);
}
or using a single query:
public function index()
{
$books = Book::join('carts', static function($join) {
$join->on('books.id', '=', 'carts.book_id')
->where('carts.user_id', '=', Auth::id());
})->get();
return view('carts.index')->withBooks($books);
}
You should read about and use eloquent relationships.
So I keep getting this error when trying to get the name of the user who created a post.
My models look like this:
class User extends Authenticatable {
use Notifiable, CanResetPassword;
public function posts() {
return $this->hasMany('App\Post');
}
}
&
class Post extends Model {
public function user() {
return $this->belongsTo('App\User', 'user_id');
}
}
I try to display the name by doing this in my view:
#foreach($posts as $post)
<tr>
<td>{{ $post->user->name }}</td>
</tr>
#endforeach
And my controller looks like this:
public function getAdminIndex() {
$posts = Post::orderBy('id', 'desc')->get();
$comments = Comment::all();
$likes = Like::all();
$users = User::all();
return view('admin.index', ['posts' => $posts, 'comments' => $comments, 'likes' => $likes, 'users' => $users]);
}
Can anyone please tell me what I'm doing wrong?
Thank you very much!
It means not all posts have user, so do something like this:
<td>{{ optional($post->user)->name }}</td>
Or:
<td>{{ empty($post->user) ? 'No user' : $post->user->name }}</td>
I got a problem when I want to show my data in view from same table, but I just need one of field in that table..
through controller, there were function index() and setID()
This is my controller
public function index(){
$wo = DB::select('select max(nowo)+1 as nowo, CURRENT_DATE as tgl from workorder');
return view('entri_wo')->with(['workorder'=>$wo]);
}
public function setID()
{
$wo = DB::select('select idmontir from workorder');
return view('entri_wo',['wo'=>$wo]);
}
This is my view
Also, I want to show this
And this is my route
Route::get('/entri_wo', 'WoController#index')->name('entri_wo');
in your controller:
public function index(){
$wo = DB::select('select idmontir from workorder');
$wo2 = DB::select('select max(nowo)+1 as nowo, CURRENT_DATE as tgl from workorder');
return view('entri_wo',['wo'=>$wo,'workorder'=>$wo2]);
}
and in your view:
#foreach($wo as $item)
<option value="{{ $item->idmontir }}" >{{ $item->idmontir }}</option>
#endforeach
and :
#foreach($workorder as $item)
<tr>
<td>{{ $item->nowo }}</td>
<td>{{ $item->tgl }}</td>
</tr>
#endforeach
The problem is undefined variable in view.
My controller:
use App\Book;
class bookController extends Controller
{
public function index()
{
$books = Book::all();
return view('frontend.index')->with('books', $books);
}
}
I get undefined variable books;
I was searching here and on the web and tried many things and I still get that error.
Not sure why. Can someone help me?
Thank you
EDIT:
View:
#foreach ($books as $key => $book)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $book->title }}</td>
<td>{{ $book->description }}</td>
</tr>
#endforeach
First of all your class name must start with capital letter...
Try this:
return view('frontend.index', ['books' => $books]);
instead of
return view('frontend.index')->with('books', $books);
Try this:
return view('frontend.index')->with(compact($books));
Try this
use App\Book;
class bookController extends Controller
{
public function index()
{
$books = Book::all();
$data['books'] = $books; //You may put other values too in $data array
return view('frontend.index', $data);
}
}