Trying to get property 'product_name' of non-object - laravel

ProductDetail Model:
public function getProduct(){
return $this->belongsTo('App\Models\Products');
}
Product model:
public function detail(){
return $this->hasOne('App\Models\ProductDetails');
}
homepage blade:
#foreach ($product_slider as $index=>$product_details)
<div class="ps-banner"><img class="mobile-only" src="{{asset('farmart/')}}/img/slider/home-1/slide_03_mobile.png" alt="alt"><img class="desktop-only" src="{{asset('farmart/')}}/img/slider/home-1/slide_03.png" alt="alt">
<div class="ps-content {{ $index==0 ? 'active':'' }}">
<div class="container">
<div class="ps-content-box">
<div class="ps-node"><span class='text-danger'>SALE UP TO 30%</span></div>
<div class="ps-title">{{ $product_details->getProduct->product_name}}</div>
<div class="ps-subtitle">Only from <br><span class='price'>{{ $product_details->getProduct->price }} TL</span></div>
<div class="ps-shopnow"> Shop Now<i class="icon-chevron-right"></i></div>
</div>
</div>
</div>
</div>
#endforeach
HomeController :
$product_slider=ProductDetails::with('getProduct')->where('show_slider', 1)->take(3)->get();
return view('homepage' ,compact('product_slider'));
error-->Trying to get property 'product_name' of non-object

productDetails model:
public function getProduct(){
return $this->belongsTo('App\Models\Products','products_id');
}
fix as above

your getting this error because in $product_details->getProduct->product_name here getProduct is undefined
so it will be $product_details->undefined->product_name so here you cannot get product_name of undefined
solution 1 : -
you can skip the $product_slider which doesnot have $product_details
#foreach ($product_slider as $index=>$product_details)
#if (!$product_details->getProduct)
#continue
#endif
<div class="ps-banner"><img class="mobile-only" src="{{asset('farmart/')}}/img/slider/home-1/slide_03_mobile.png" alt="alt"><img class="desktop-only" src="{{asset('farmart/')}}/img/slider/home-1/slide_03.png" alt="alt">
<div class="ps-content {{ $index==0 ? 'active':'' }}">
<div class="container">
<div class="ps-content-box">
<div class="ps-node"><span class='text-danger'>SALE UP TO 30%</span></div>
<div class="ps-title">{{ $product_details->getProduct->product_name}}</div>
<div class="ps-subtitle">Only from <br><span class='price'>{{ $product_details->getProduct->price }} TL</span></div>
<div class="ps-shopnow"> Shop Now<i class="icon-chevron-right"></i></div>
</div>
</div>
</div>
</div>
#endforeach
ref link https://laravel.com/docs/8.x/blade#loops
Solution 2 : -
incontroller only retrun those which have products
$product_slider=ProductDetails::has('getProduct')->with('getProduct')->where('show_slider', 1)->take(3)->get();
return view('homepage' ,compact('product_slider'));
ref link https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence

Related

i want to get a video id so that it can help me get comments related to that video in my site, it only displays a video but not comments

The biggest issue here is how to get these comments using the videoid in my bladeview -- my blade
<div id="videoid">{{$id->id}}</div>
<div id="videotitle">{{$id->title}}</div>
#php($comments = \App\comments::where('video_id','{{$id->id}}')->get() )
<div id="displaycomment">
#foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
#endforeach
</div>
My controller works well --
mycontroller
public function watch($id)
{
return view('video/watch', compact('id'));
}
This is your blade. Thought there's no need to query from the view but rather from the controller.
<div id="videoid">{{ $video->id }}</div>
<div id="videotitle">{{ $video->title }}</div>
<div id="displaycomment">
#foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
#endforeach
</div>
Then from your controller you can fetch your data and pass them to the view using Laravel's magic method:
public function watch($video_id)
{
$video = Video::whereId($video_id)->first();
$comments = \App\comments::where('video_id',$video_id)->get()
return view('video/watch',[
'video'=>$video,
'comments'=>$comments
]);
}
in controller
public function watch($id)
{
$video = Video::with('comments')->find($id);
$comments = \App\comments::where('video_id','{{$video->id}}')->get()
return view('video/watch', compact('video','comments'));
}
in view
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
#foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
#endforeach
</div>
You seem to be missing a key part of using Eloquent.
Relationships.
// Video model:
public function comments()
{
return $this->hasMany(Comment::class);
}
// Comment model:
public function video()
{
return $this->belongsTo(Video::class);
}
// Controller code: (Switched to [Route-model binding][2])
public function watch(Video $video)
{
return view('video.watch', [
'video' => $video
]);
}
// Update routes for Route-model-binding
Route::get('/watch/{video}', 'VideoController#watch')->name('video.watch');
// View:
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
#foreach ($video->comments as $comment)
<div id="username">
<div id="con">
<h6>{{$comment->id }}</h6>
</div>
<div id="con">
<h6>{{ $comment->user_id }}</h6>
</div>
<div id="con">$comment->created_at</div>
</div>
<div id="comment">$comment->comment</div>
#endforeach
</div>
Route-model binding

How to compare a value from the search bar to the one from the database in laravel

I am trying to use the search box in my Laravel project but I keep getting the same error since morning
Here is my Search controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function index()
{
return view('search.index');
}
public function Search(Request $request)
{
$serial_number= $request->input('search');
$results =DB::table('animals')->where(function ($query) use ($serial_number) {
$query->where('serial_number','LIKE',"%$serial_number%");
})->latest()->get();
return view('search',compact('results'));
}
}
My routes
Route::get('/search','SearchController#index')->name('search');
Route::post('/search','SearchController#search')->name('search');
and finally my view
#extends('layouts.app')
#section('content')
<form action="{{ route('search') }}" method="POST">
<div class="p-1 bg-light rounded rounded-pill shadow-sm mb-4">
<div class="input-group">
<input type="search" name="search" placeholder="Here the animal serial number..." aria-describedby="button-addon1" class="form-control border-0 bg-light">
<div class="input-group-append">
<button id="button-addon1" type="submit" class="btn btn-link text-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
</form>
#if($results)
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h3>Details for the animal</h3>
</div>
<div class="card-body">
<div class="col-12">
<p><strong>Id: </strong>{{ $results->id }}</p>
<p><strong>Animal: </strong>{{ $results->type->category }}</p>
<p><strong>Gender: </strong>{{ $results->gender }}</p>
<p><strong>Place Of Birth: </strong>{{ $results->user->address->city }}</p>
<p><strong>Farm: </strong>{{ $results->user->name }}</p>
<p><strong>Date: </strong>{{ $results->created_at }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
#endif
#endsection
For me, I think that the problem is in the controller on that line $result which makes the view give the error
419|Page Expired or "Facade\Ignition\Exceptions\ViewException
Undefined variable: results (View:
/Users/macair13/MeatracProject/resources/views/search/index.blade.php)"
You need to include the CSRF token or exclude that URL from the CSRF token check.
<form ...>
#csrf
...
</form>
Laravel 6.x Docs - CSRF
Also you are not passing a results variable to your 'search' view from the Controller's index method. You will need to check if $results isset in your view or pass it to your view.
public function view (Request $request)
{
$search=$request['search'] ?? "";
if($search !=""){
//where
$data=File::where('id','like','%'.$request->search.'%')
->orwhere('name','like','%'.$request->search.'%')
->orwhere('file','like','%'.$request->search.'%')->get();
}
else{
$data = File::all();
}
$p = compact('data','search');
return view('upload_data')->with($p);
// $data = File::all();
// return view('upload_data',compact('data'));
}

How to increment the count value in DB when button is clicked in laravel?

I have Home.blade.php when I click the vote button in that page, the count value should increment in the candidate table.
Here my Home.blade.php
#foreach ($file as $show)
<div class="col-md-3">
<div class="card">
<img src="p3.png" alt="John" style="width:100%">
<div class="card-body">
<h5 class="title">President</h5>
<p class="card-text">Name : {{$show->name}}</p>
<!-- <p><button>VOTE</button></p> -->
<button type="button" class="vote">VOTE</button>
</div>
</div>
</div>
#endforeach
I have table name called the candidate, when vote button is clicked, the count should be incremented in database. And i have controller called HomeController pls help me.
Thank you
class Homecontroller extends Controller
{
public function pre()
{
$file = DB::table('candidate')
->select('name','branch')
->where('candidate.post_id', '=', 1)
->get();
return view('/home')->with('file',$file);
}
}
#foreach ($file as $show)
<div class="col-md-3" style="margin-bottom: 20px;">
<div class="card">
<img src="p3.png" alt="John" style="width:100%">
<div class="card-body">
<h5 class="title">President</h5>
<p class="card-text">Name : {{$show->name}}</p>
<p><button>VOTE</button></p>
</div>
</div>
</div>
#endforeach
i have used funtion redirect to controller
in controller i have used this function
public function Count(){
DB::table('candidate')->increment('count',1);
return view('/home');
}
still its not working
this is my Route
Route::get('/home','Homecontroller#count');

Two controllers one route laravel

I can currently show the index page along with a list of posts. The user can select a post to view post details. For this I have:
Routes:
Route::get('/', 'PostsController#showPosts');
Route::get('post/{slug}', 'PostsController#showPostDetails');
Controller:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function showPosts()
{
$posts = Post::simplePaginate(2);
return view('index', ['posts' => $posts]);
}
public function showPostDetails($slug)
{
$post = Post::findBySlug($slug);
return view('post.show',['post'=>$post]);
}
}
Model:
public static function findBySlug($slug)
{
return static::where('slug', $slug)->first();
}
index
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('img/home-bg.jpg')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Train Testing</h1>
<span class="subheading">Testing Times</span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
</div>
</div>
#stop
partials/post.blade.php
<div class="post-preview">
<a href="/post/{{ $post->slug }}">
<h2 class="post-title">
{{ $post->title }}
</h2>
<h3 class="post-subtitle">
{{ $post->excerpt }}
</h3>
</a>
<p class="post-meta">Posted by
{{ $post->author->name }}
on {{ $post->created_at->format('l d F, Y') }}</p>
</div>
<hr>
I now want to show the posts on all other pages. My other pages currently have the route Route::get('{slug}', 'PagesController#show'); And it makes sense initially to refer back to existing code so use Route::get('{slug}', 'PostsController#showPosts'); like I did on the home page to display posts there.
However I am not sure of the best way to deal with this as I believe you can not have two controllers for one route.
For other pages I currently have:
Controller:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$page = Page::findBySlug($slug);
return view('page', ['page' => $page]);
}
}
page.blade:
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('/storage/{{ $page->image }}')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="page-heading"> <h1>{!! $page->title !!}</h1>
<span class="subheading"></span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{!! $page->body !!}
</div>
</div>
</div>
#stop
Pages Controller:
use App\Page;
use App\Post;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$posts = Post::simplePaginate(2);
$page = Page::findBySlug($slug);
return view('page', ['page' => $page, 'posts' => $posts]);
}
Then in page.blade.php I can call $posts without any errors:
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
Not sure if this is the neatest way but it works.

using Pagination with orderBy Eloquent Model

I have a function witch is getting all properties joining the images tables with orderBy and need to add paginate
here is my function
public function show_all()
{
$properties = PropertyDetail::where('active', 1)->with('propImages')->paginate(15)
->orderBy('id', 'DESC')
->get();
return View::make('portal.properties.view_all', compact('properties'));
}
in my view I got
call_user_func_array() expects parameter 1 to be a valid callback,
class 'Illuminate\Support\Collection' does not have a method 'orderBy'
another thing when I removed the orderBy('id', 'DESC')->get() and I try
it works but when I try to put {{ $prop->links() }} in the view I got
Call to undefined method Illuminate\Database\Query\Builder::links()
here is how my view looks like
#foreach($properties as $prop)
<div class="property col-xs-12 col-sm-6 col-md-4">
<div class="image">
<div class="content imgContainer">
{{ HTML::link('property-details/'.$prop->id, '') }}
#foreach($prop->propImages->slice(0, 1) as $image)
{{ HTML::image('images/propertyImages/'.$image->image, $prop->title) }}
#endforeach
</div>
<div class="price"> OMR. <span class="priceNumber"> {{ $prop->price }}</span></div>
</div>
<div class="title">
<h2>{{ HTML::link('', $prop->title, array('title'=>'$prop->title')) }}</h2>
</div>
<div class="location">{{ trans('location.'.$prop->propLocation->city) }}</div>
<div class="bathrooms">
<div class="content">{{ $prop->bathroom }}</div>
</div>
<div class="bedrooms">
<div class="content">{{ $prop->bedroom }}</div>
</div><!-- /.bedrooms -->
<div class="receptionRoom">
<div class="content">{{ $prop->dining_room }}</div>
</div>
</div>
#endforeach
{{ $prop->links() }}
The correct syntax is:
$properties = PropertyDetail::where('active', 1)
->with('propImages')
->orderBy('id', 'desc')
->paginate(15);

Resources