Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why looping to get data after using WHERE in Laravel?

So, here is my controller:

$topics = Topic::where('board_id', $id)->with('user')->get();
$topic = Topic::find($id);
$board = Boards::where('id', $id)->get();
return view('boards.show')->with('topics', $topics)->with('topic', $topic)->with('board', $board);

And here is the code for generating URL's:

@foreach($board as $boards)
<a href="/topics/create/{{$boards->id}}">Create New Post</a>
<p>No Posts Found</p>
@endforeach

But if i'm removing foreach loop, it is giving error:

Property [id] does not exist on this collection instance.

But why do, i have to loop, if it is only getting one row from the boards table?? Any solution of doing it without running for each loop???

like image 933
Deepak Rawat Avatar asked Dec 27 '17 18:12

Deepak Rawat


People also ask

What is eloquent in Laravel?

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table.

How do I write a global scope in Laravel?

Writing a global scope is simple. First, define a class that implements the Illuminate\Database\Eloquent\Scope interface. Laravel does not have a conventional location where you should place scope classes, so you are free to place this class in any directory that you wish.

What version of PHP is required for Laravel?

: laravel is for? laravel if condition with ?? laravel 8 if syntax ?? laravel/ui v3.0.0 requires php ^7.3 -> your php version (8.0.2) does not satisfy that requirement. Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 7.3.0". it is missing from your system. Install or enable PHP's pcntl

How do I find the ID of a komenmodel in Laravel?

This is by far the most correct answer to the question because this is the 'Laravel' way of doing it. – Eric Tucker Jan 3 '17 at 18:09 Add a comment | 0 First create the UserModel and KomenModel. then use this $queryData = UserModel::find($id); foreach ($queryData as $data) { $query2 = KomenModel::find($data->id); }


Video Answer


2 Answers

Since you want to get just one object, you do not need to use get() to get a collection. Use find() to get an object by it's primary key:

$board = Boards::find($id);

In the view you don't need to use @foreach loop:

<a href="/topics/create/{{ $board->id }}">Create New Post</a>
like image 168
Alexey Mezenin Avatar answered Oct 14 '22 09:10

Alexey Mezenin


You can use Boards::find($id); or Boards::findOrFail($id); insted of Boards::where('id', $id)->get(); for getting a single row.Also use

return view('boards.show')->with('topics', $topics)->with('topic', $topic)->with('board', $board);

to

return view('boards.show',[
   'topics'=> $topics,
   'topic'=> $topic,
   'board'=> $board
 ]);

becouse passing normal values to view using session is not a good practice

like image 34
Rahul Reghunath Avatar answered Oct 14 '22 11:10

Rahul Reghunath