passing data to blade with nest() - php

I have a post table and a comment table and I want to show a list of my comments. But for showing the title of post for each comment I get thrown this error:
Trying to get property of non-object (View:\myBlog\app\views\comments\list.blade.php)
I am really confused about it.
This is my controller:
public function listComment()
{
$comments = Comment::orderBy('id', 'desc')->paginate(20);
$this->layout->title = 'Comment Listings';
$this->layout->main = View::make('admin.index')->nest('content', 'comments.list', compact('comments'));
}
This is my comments/list.blade.php
<tbody>
#foreach($comments as $comment)
<tr>
<td>{{{$comment->commenter}}}</td>
<td>{{{$comment->email}}}</td>
<!--<td>{{$comment->post->title}}</td>-->
<td>
{{Form::open(['route'=>['comment.update',$comment->id]])}}
{{Form::select('status',['yes'=>'Yes','no'=>'No'],$comment->approved,['onchange'=>'submit()'])}}
{{Form::close()}}
</td>
<td>{{HTML::linkRoute('comment.delete','Delete',$comment->id)}}</td>
<td>{{HTML::linkRoute('comment.show','Quick View',$comment->id,['data-reveal-id'=>'comment-show','data-reveal-ajax'=>'true'])}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$comments->links()}}
What is my wrong with passing data to comments/list.blade.php?

Related

Attempt to read property "Nombre" on bool

I'm trying to make a show view with Laravel 8 but i can't show the detail, this is the code from the controller:
public function show($id)
{
$accesorios=DB::table('accesorio as acc')
->join('detalle_aparato as da','acc.idAccesorio','=','da.idAccesorio')
->select('acc.Nombre')
->where('da.idAparato','=',$id);
return view("almacen.aparato.show",["accesorio"=>Accesorio::findOrFail($id)]);
}
And this is the code from the view:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
Error message
When I use:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc}}</td>
</tr>
#endforeach
It prints: 1 for each record
Hope you can help me
In you're controller you're using DB::Table to set the $accesorios variable, but never using it.
You then are setting accesorio in your view to Accesorio::findOrFail($id) which will only return one instance of the object.
Either pass $accessorios into your view
return view("almacen.aparato.show",["accesorios"=>$accessorios]);
then loop through it
#foreach ($accesorios as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
or since you're just sending one instance of the object to the view, remove the loop and you can render it like this.
<tr>
<td>{{ $accesorio->Nombre }}</td>
</tr>

Laravel route problem redirects 404 error

I have a problem, I made a support system, and when I want to enter the page where I watch the ticket, I will get 404 not found. Basically the route is that of the id in the database.
Routes:
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
Controller:
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate() {
$tickets = Ticket::latest()->get();
return view('updateTicket', compact('tickets'));
}
View:
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<form>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</form>
</tr>
#endforeach
</tbody>
I really don't understand the problem, a way to solve and did not receive error 404? Btw, I read the other topics and I couldn't find a solution.
The link you are setting in the href attribute is only the ticket id, but it should be, for example, "/viewTickets/1"
Try to put this:
href="{{route('updateTicket', ['ticket' => $ticket->id])}}"
In your route :
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
In your Controller :
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate(Request $request, $ticket) {
$tickets = Ticket::find($ticket);
return view('updateTicket', compact('tickets'));
}
In your Blade :
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</tr>
#endforeach
</tbody>

Paginating in Laravel

I am working on an assignment for Uni however I've ran into a snag paginating my comments field. I have put my Controller, Model and View bellow(in that order)
<?php
namespace App\Http\Controllers;
use App\Comment;
use Illuminate\Http\Request;
class CommentController extends Controller
{
const COMMENTS_PER_PAGE = 5;
public function index () {
$comments = Comment::paginate (self::COMMENTS_PER_PAGE);
return view ('index') -> with (['comments' => $comments]);
}
public function create()
{
//
}
public function store(Request $request)
{
//
}
public function show(Comment $comment)
{
//
}
public function edit(Comment $comment)
{
//
}
public function update(Request $request, Comment $comment)
{
//
}
public function destroy(Comment $comment)
{
//
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public static function paginate(int $COMMENTS_PER_PAGE)
{
}
}
{{--#extends('layout.master)--}}
{{--#section('content')--}}
<div class="container main-table">
<div class="box">
<h1 class="title">Guestbook Comments</h1>
#if (count ($comments) > 0)
<table class="table is-striped is-hoverable">
<thead>
<tr>
<th>User</th>
<th>Comment</th>
<th>Date</th>
<th>Likes</th>
</tr>
</thead>
<tbody>
#foreach ($comments as $c)
{{--declaring the comments--}}
<tr>
<td>{{ $c -> name }}</td>
<td>{{ $c -> comment }}</td>
<td>{{ $c -> created_at -> format ('D jS F') }}</td>
<td>{{ $c -> likes }}</td>
</tr>
#endforeach
</tbody>
</table>
{{--looking at pagination--}}
{{ $comments -> links() }}
#else
<div class="notification is-info">
<p>
The Guestbook is empty. Why not add a comment?
</p>
</div>
#endif
</div>
</div>
{{--#endsection--}}
I managed to get it to display the comments before changing to pagination. After doing this I ran into issues getting it to display. I don't get an error I just get a blank page as if nothing is wrong. It doesn't show any of my headings or text that is not from my comments table.
Has anyone got any ideas about why this is?
Any help would be greatly appreciated :D
Update 17:44 13/11/19
Just realised that the link was going to the wrong page but just now figured out that the count function is having and issue as it says that there is no relevant ARRAY ("Facade\Ignition\Exceptions\ViewException
count(): Parameter must be an array or an object that implements Countable (View: C:\XAMPP\htdocs\Assignment\resources\views\comment\comments.blade.php) ")
Does anyone know why this is?
I thought it would just list comments 1 by 1 up until it reads out 5 comments.
Any ideas on how to fix this? :)
change {{ $comments -> links() }} & try this
{!! $comments->render() !!}
I would recommend to paginate like this.
public function index () {
const COMMENTS_PER_PAGE = 5;
$comments = Comment::paginate(self::COMMENTS_PER_PAGE);
return view('index', compact('comments'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
and also, change
{!! $comments->links() !!}
to
{!! $comments->render() !!}
{!! $comments->appends(Request::except('page'))->render() !!}

Syntax Error access violation in query Laravel 5.1

I am having trouble in finding and understanding my mistake. I have table payments and i want to show this data on my students page, (by getting student_id). It should get the data by student_id.
payments{id, student_id, course_id, transfer_amount, payment_type_id, is_paid, date}
But this mistake keeps showing. Where is my mistake, guys please help.Any help is appreciated.
Here is StudentsController.php
public function payments($id, Request $request) {
$student = Student::findOrFail($id);
$payment = Payment::where('student_id', $id)-> get();
$student->has('payments')->where('student_id', $request->id)->get();
return view('students.groups', compact('student', 'payment'));
}
Here is my model Student.php
public function payments()
{
return $this->belongsToMany('App\Payment','payments', 'student_id');
}
here is view students> payments.blade.php
#forelse ($student->payments as $key => $payment)
<tr class="center aligned">
<td>{{++$key}}</td>
<td>{{$payment->course_id}}</td>
<td>{{$payment->payment_type_id</td>
<td>{{$payment->transfer_amount}}</td>
<td>
#if($payment->is_paid)
<p style="color: green;">Paid</p>
#else <p style="color: red">Not Paid</p>
#endif
</td>
#empty
<tr class="center aligned">
<td colspan="12" class="ui red header">No data</td>
</tr>
#endforelse
error Not unique table/alias showing up.
Do you really need to have the variable $payment? Did you use this somewhere or anywhere in the view? If not, then this will probably the right eloquent syntax:
public function payments($id, Request $request) {
$student = Student::with('payments')->where('student_id', $id)->firstOrFail();
return view('students.groups', compact('student'));
}
In the above example, it is searching for a user with id of $id and include its payments details using relationships that's defined in the model.
If you need to validate the student MUST always has at least 1 payment, then you could add has('payments') to the syntax like so:
public function payments($id, Request $request) {
$student = Student::has('payments')->with('payments')->where('student_id', $id)->firstOrFail();
return view('students.groups', compact('student'));
}
This will return ONLY if the student has at least 1 payment stored in the database.
Hope it helps!
I think you can update your query like:
public function payments($id, Request $request) {
$student = Student::findOrFail($id);
$payment = Student::with('payments')->where('id', $id)->get();
return view('students.groups', compact('student', 'payment'));
}
Hope this helps you.
UPDATE 5/5/2017
I think you Should Update payments.blade.php
#forelse ($student->payments as $key => $payment)
<tr class="center aligned">
<td>{{++$key}}</td>
<td>{{$payment->course_id}}</td>
<td>{{$payment->payment_type_id}}</td>
<td>{{$payment->transfer_amount}}</td>
<td>
#if($payment->is_paid)
<p style="color: green;">Paid</p>
#else <p style="color: red">Not Paid</p>
#endif
</td>
#empty
<tr class="center aligned">
<td colspan="12" class="ui red header">No data</td>
</tr>
#endforelse

Fecthing the data from database in Laravel 5.1

Good day
I'm facing some errors while trying to fetch data from database
here are my codes
my table name is meeting and I used the formal documentation to write the codes
controllers.php
public function meetingdet()
{
$mretive = \DB::table('meeting')-> $name = $request->input('meetingname');}
view
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td><li>foreach ($name as $retrive) {
echo $retrive-> meeting;}
#endforeach
</li>
</td>
</tr>
</table>
but this what is showing for me
syntax error, unexpected 'endforeach' (T_ENDFOREACH) and whan I remove
endforeach it display for me the for each without fetching the data
By the way I'm working on laravel 5.1
Regards
You can try with the following sample code:
controllers.php
//include your model file
use App\model;
public function meetingdet()
{
$meetingData = model::getMeetingData();
$view = array(
'name'=>$meetingData
);
return view('test', compact('meetingData'));
}
Model.php
public static function getMeetingData()
{
$value=DB::table('meeting')->orderBy('meeting_id', 'asc')->get();
return $value;
}
view.blade.php
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td>
<ul>
<li>
#foreach ($meetingData as $retrive)
{{{ $retrive->meeting_id }}}
#endforeach
</li>
</ul>
</td>
</tr>
</table>
<li>
#foreach ($name as $retrive)
{{ $retrive->meeting }}
#endforeach
</li>
you didn't add # to foreach..

Categories