Laravel:Failed to update data - laravel-5

Dear team please help me
here is my function
public function destroy($id)
{
DB::table('ssrareqs')
->where('req_id',$id)
->update(['status_req'=>'reject']);
return back();
}
and below my blade
<td> <a style="color: red" href="{{action('ApproverController#destroy', $approvereqs->req_id)}}">Reject</a></td>

Related

How to get user name from another table in laravel

Hello I want to get user names on my reply table, but there its shows me only user id, when I type $reply->user_id->name it shows me that error: "Attempt to read property "name" on int".
How can I fix it ?
there are my codes:
Ticket Model
public function replies() {
return $this->hasMany(Reply::class);
}
Reply Model
public function tickets() {
return $this->belongsTo(Tickets::class);
}
Tickets Controller
public function show($id) {
$user = Auth::user();
$tickets = Tickets::with('companies')->get();
$ticketscomp = Companies::with('tickets')->get();
$severities = Severities::with('tickets')->get();
$users = User::with('tickets')->get();
//$replies = DB::table('replies')->where('ticket_id', $id)->get();
// $articles = Reply::where('user_id', $id)->with('User')->get();
$ticketspage = Tickets::with('severities')->with('companies')->with('user')->with('replies')->findOrFail($id);
return view('tickets.ticket', compact('ticketspage'))->
with(['tickets'=> $tickets])->
with(['ticketscomp'=>$ticketscomp])->
with(['severities'=>$severities])->
with(['ticketspage'=>$ticketspage])->
with(['replies'=>$replies]);
//dd($reply_author->toArray());
}
Blade
#foreach ($replies as $reply)
<div class="NjBSH">
<div id="AuthorAndDate-1">
<span class="author">{{ $reply->user_id->name }}</span>
<span class="date"><span style="color: rgb(32, 33, 36);">{{ $reply->created_at }}</span><br></span>
</div>
<div class="kmSodw">
<span>{{ $reply->text }}</span>
</div>
</div>
#endforeach
Text, Created at showing also $reply->user_id but not this $reply->user_id->name.
Here is a complete solution using ELOQUENT:
Ticket Model
public function replies() {
return $this->hasMany(Reply::class);
}
Reply Model
public function ticket() {
return $this->belongsTo(Ticket::class);
}
public function user() {
return $this->belongsTo(User::class);
}
Tickets Controller:
public function show(Ticket $ticket) {
//Load all of the ticket relationships that we will be using
$ticket->load('replies.articles', 'replies.user', 'companies', 'severities');
//Assign the loaded ticket companies
$companies = $ticket->companies;
//Assign the loaded ticket severities
$severities = $ticket->severities;
//Assign the loaded ticket replies
$replies = $ticket->replies;
/*
You can acess the replies -> articles && user in a foreach loop, in php or blade
foreach($replies->articles as $article){
//$article->id
}
*/
return view('tickets.ticket', compact('ticket', 'companies', 'severities', 'replies'));
}
Blade
#foreach ($replies as $reply)
<div class="NjBSH">
<div id="AuthorAndDate-1">
<span class="author">{{ $reply->user->name }}</span>
<span class="date"><span style="color: rgb(32, 33, 36);">{{ $reply->created_at }}</span><br></span>
</div>
<div class="kmSodw">
<span>{{ $reply->text }}</span>
</div>
</div>
#endforeach

Show category posts in Laravel is not working

I want to show posts that are in a category by clicking on them, but when I click on a category,$articles in category.blade.php returns null.
$articles should return articles that have a specific category
this is my index.blade.php:
<div class="tab-pane fade" id="show-categories" role="tabpanel">
<h6 class="sidebar-title">Categories</h6>
<div class="row link-color-default fs-14 lh-24">
#foreach($categories as $category)
<div class="col-6">
<a href="{{ route('cms.category', $category->id) }}">
{{ $category->name }}
</a>
</div>
#endforeach
</div>
</div>
category.blade.php:
#extends('layouts.app')
#section('content')
#forelse ($articles as $article)
<h1>{{$article->title}}</h1>
#empty
<span>array is empty</span>
#endforelse
#endsection
router:
Route::get('cms/categories/{category}', [articlesController::class, 'category'])->name('cms.category');
and articlesController:
public function category(Category $category)
{
return view('cms.category')
->with('category', $category)
->with('articles', $category->articles()->searched()->simplePaginate(3))
->with('categories', Category::all())
->with('tags', Tag::all());
}
Please, don't forget to define the relationship from Category to Article on your model. Like: one category has many articles. For example:
On your Category.php model:
public function articles()
{
return $this->hasMany(Article::class);
}
Then on your, controller, you may call the relationship like this way:
public function category(Category $category)
{
$articles = Category::with("articles")->where("id", $category->id)->simplePaginate(3);
return view('cms.category')
->with('category', $category)
->with('articles', $articles)
->with('categories', Category::all())
->with('tags', Tag::all());
}
Please share the model file and second I prefer to write the category method in a different way.
public function category($category_id)
{
try {
$articles= Article::where('id', $category_id)->get();
return view('cms.category', $articles);
} catch (\Throwable $th) {
Log::error($th->getMessage());
}
}
Let me know if you still face any issue. Try to use dd $articles ..

Trying to get property 'image_url' of non-object (View: /app/resources/views/library.blade.php)

My code keeps returning an error when I deploy to google cloud, but this works locally
Trying to get property 'image_url' of non-object (View: /app/resources/views/library.blade.php)
In my view, I have
#forelse($favorites as $fav)
<div class="col-6 col-md-3">
<a href="{{ route('book', $fav->id) }}">
<img src="{{ $fav->book->image_url }}" alt="trending image" />
</a>
<p class="authorone">{{ $fav->book->author->name }}</p>
<h1 class="book-title">{{ $fav->book->name }}</h1>
#empty
<h2>You have no favourites</h2>
<br/><br/>
Return To Discover
</div>
#endforelse
Here is my controller
public function mylibrary(){
// $mybooks = MyBook::where('user_id', Auth::id())->get();
$favorites = Favorite::where('user_id', Auth::id())->get();
return view('library')->with('favorites', $favorites);
}
In my Favorite Model, I have this
public function book ()
{
return $this->belongsTo(Book::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
In my Book Model, I have this
public function favorites()
{
return $this->hasMany(Favorite::class);
}
In my User Model, I have this
public function favorites(){
return $this->hasMany(Favorite::class);
}
My favorites table
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('book_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
});
}
I don't know it's not working when I deploy to google cloud, but it works locally. I can't seems to figure out what I am doing wrong.
I also tried this
public function mylibrary(Book $book){
// $mybooks = MyBook::where('user_id', Auth::id())->get();
$favorites = Favorite::where('user_id', Auth::id())->get();
return view('library')->with('favorites', $favorites)->with('book', $book);
}
My Books schema
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('about');
$table->string('image');
$table->string('image_url');
$table->string('epub_url');
$table->integer('author_id');
$table->string('publisher');
$table->year('year');
$table->boolean('recommended')->default(0);
$table->timestamps();
});
When querying, you may specify which relationships should be eager
loaded using the with method:
Blockquote
public function mylibrary() {
$data = User::with('favorites', 'favorites.book')->find(Auth::id());
return view('library')->with('userFavorites', $data);
}
You may check for null in chained statements using the Null Coalescing Operator ?? ( introduced in PHP 7) :
//...
<img src="{{ $fav->book->image_url ?? '' }}" alt="trending image" />
//...
Edit
#forelse($userFavorites->favorites as $fav)
<div class="col-6 col-md-3">
<a href="{{ route('book', $fav->id) }}">
<img src="{{ $fav->book->image_url ?? '' }}" alt="trending image"/>
</a>
<p class="authorone">{{ $fav->book->author->name ?? '' }}</p>
<h1 class="book-title">{{ $fav->book->name ?? '' }}</h1>
#empty
<h2>You have no favourites</h2>
<br/><br/>
Return To Discover
</div>
#endforelse

Display tests for lessons who belong to Courses

I have Courses which has lessons, and each lesson has a test. I'm trying to display the test when a lesson is clicked.
I've created the models, controller and view and it doesn't seem to work.
Here is the model for the Lesson
public function course()
{
return $this->belongsTo(Course::class, 'course_id')->withTrashed();
}
public function test() {
return $this->hasOne('App\Test');
}
Here is the controller
public function show($id)
{
$course = Course::with( 'lessons')->with('activeLessons')->findOrFail($id);
$created_bies = \App\User::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$trainers = \App\User::get()->pluck('name', 'id');
// $test = \App\Test::where('course_id', $id)->get();
$lesson = \App\Lesson::where('course_id', $id)->get();
// $course_test = Course::with('tests')->findOrFail($id);
$user = User::find(1);
$user->name;
return view('admin.courses.showCourse', compact('course', 'test', 'lesson','course_test', 'previous_lesson', 'next_lesson','date', 'user'));
}
function view_tests($id)
{
$lessons = Lesson::findOrFail($id);
$lessons->test;
return view('admin.courses.test', compact('lessons'));
Here is the Route
Route::get('/test/{id}', 'EmployeeCoursesController#view_tests')->name('test.show');
And here is the Blade with the link to display the test
#foreach($course->activeLessons as $lesson)
<article class="lesson" >
<p></p>
<p></p>
{!! $loop->iteration!!}.
<div class="body" id="title"> {!!$loop->iteration!!}. <h4>{{ $lesson->title }}</div>
<p> {!! $lesson->short_description !!}</p>
<iframe width="420" height="315" src="{{ $lesson->video_link}}" frameborder="0" allowfullscreen></iframe>
</article>
#endforeach
The issue was on the test blade. The code works well.

Get the user name and comment with article id in Laravel

I have 3 tables users, comments, and articles. I have a route like this:
Route::get('/article/{id}', 'ArticlesController#view');
What I want is when I accessing that route I will get User Name and their comment in this article id.
so here's my view function in ArticlesController:
public function view($id){
$article = Article::with('comments')->find($id);
return view('front.single',compact('article'));
}
and here's my single.blade.php code:
<div class="single-grid">
#section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
#show
</div>
<div class="comment-list">
#foreach($article->comments as $comment)
<ul class="comment-list">
<div class="comment-grid">
<img src="/images/avatar.png" alt=""/>
<div class="comment-info">
<h4>{{ $comment->user->name }}</h4>
<p>{{ $comment->comment }}</p>
<h5>{{ $comment->created_at->diffForHumans() }}</h5>
Reply
</div>
<div class="clearfix"></div>
</div>
</ul>
#endforeach
</div>
I'm not sure what how to do it since it gives me error like this:
"Call to undefined relationship [comment] on model [App\User]."
I already define the relation in each model. here's my articles model:
public function comments(){
return $this->hasMany(Comment::class);
}
public function user(){
return $this->belongsTo(User::class);
}
My comment model:
public function article(){
$this->belongsTo(Article::class);
}
public function user(){
$this->belongsTo(User::class);
}
and here's my User model:
public function articles(){
return $this->hasMany(Article::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function publish(Article $article){
$this->articles()->save($article);
}
here's my table structure:
-users(id,name,email,password,remember_token,created_at,updated_at)
-comments(id,user_id,article_id,comment,created_at,updated_at)
-articles(id,user_id,title,content,created_at,updated_at)
so how can I can User Name just by using this route? thanks.
on Your Comment Model, You need to replace articles to article
public function article(){
$this->belongsTo(Article::class);
}
also, if you want to get user specific comments, than you need to change your controller action code from
$article = Article::with('user.comment')->find($id) to
$article = Article::with('user.comments')->find($id);
I think that your issue comes from the use of compact function : an array is passed to the view instead of an object.
Can you try it like this :
// Controller
public function view($id) {
$article = Article::findOrFail($id);
return view('front.single', $article);
}
<!-- View -->
#foreach($article->comments as $comment)
{{ $comment->user->name }}
{{ $comment->comment }}
{{ $comment->created_at->diffForHumans() }}
#endforeach

Resources