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???
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.
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.
: 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
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); }
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With