Data can not be appears, database relation one to one Laravel - laravel

ErrorException
Trying to get property 'category_name' of non-object (View: D:\xampp\htdocs\e-catalog\resources\views\backend\pages\gmproducts.blade.php)
This is my blade code below.
<tbody>
#foreach($gmproducts as $no => $gm)
<tr>
<td>{{$no+1}}</td>
<td>{{$gm->type}}</td>
<td>{{$gm->gmcategories->category_name}}</td>
<td>{{$gm->gram}}</td>
<td>{{$gm->carat}}</td>
<td>#currency($gm->price)</td>
<td><a class="test-popup-link" href="{{url('p_goldmart/'.$gm->file)}}">Preview</a></td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash-o"></i>
</td>
</tr>
#endforeach
</tbody>
This is my model Gmcategories & Gmproducts.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Gmcategories extends Model
{
protected $table = "gmcategories";
protected $fillable = ['category_name'];
public function gmproducts()
{
return $this->hasOne('App\Gmproducts');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Gmproducts extends Model
{
use SoftDeletes;
protected $table = "gmproducts";
protected $fillable = ['type', 'category_id', 'gram', 'carat', 'price', 'file'];
protected $dates = ['deleted_at'];
public function gmcategories()
{
return $this->belongsTo('App\Gmcategories');
}
}
This is my controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Gmproducts;
use App\Gmcategories;
use File;
class GmproductsController extends Controller
{
public function index()
{
$gmproducts = Gmproducts::orderBy('id', 'desc')->get();
$trash = Gmproducts::orderBy('deleted_at', 'desc')->onlyTrashed()->count();
return view('backend.pages.gmproducts', compact('gmproducts', 'trash'));
}
public function create()
{
$gmcategories = Gmcategories::all();
return view('backend.pages.create', compact('gmcategories'));
}
Just info, I just rename my model and controller. Does it have any effect? Thanks.

// Gmcategories model
public function gmproducts()
{
return $this->hasOne('App\Gmproducts', 'category_id');
}
// Gmproducts model
public function gmcategories()
{
return $this->belongsTo('App\Gmcategories', 'category_id');
}

Your BuildImage model should be
Get the source type of the Building Image.
public function type() {
return $this->hasOne('App\BuildingType',"id","source");
}
And BuildingType Model should be
Get the Building Image that owns the building type.
public function buildingImage()
{
return $this->belongsTo('App\BuildingImage',"source","id");
}

Related

How to display multiple or single images for a product page in Laravel?

[enter image description here][1]
I'm trying to store multiple images in the database. I have products table and images tables. I have faced the issue for image not showing the product list page. I am trying to more tutorials codes unfortunately my issues not solve. Please check the below cods and help me the problem solve.
Products Model
class Products extends Model
{
use HasFactory;
protected $table = 'cw_products';
protected $fillable =
['product_id','product_title','product_details','product_cid'];
public function images()
{
return $this->hasMany(ProductsImage::class, 'images_pdct_id', 'images_id');
}
}
ProductsImage Model
class ProductsImage extends Model
{
use HasFactory;
protected $table = 'cw_products_images';
protected $fillable =
['images_id','images_pdct_id','images_image','images_thumbnail'];
public function products()
{
return $this->belongsTo(Products::class, 'product_id');
}
}
ProductsController
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\Products;
use App\Models\ProductsImage;
use Hash;
use Session;
use File;
use Image;
class ProductsController extends Controller
{
public function index(Request $request)
{
$data = array();
//$products = Products::where('product_status', '>=', 0)->orderBy('product_id', 'desc')->paginate(30); // Replace 100
$products = Products::with('images')->where('product_status', '>=', 0)->orderBy('product_id', 'desc')->paginate(30);
//DD($products);
return view('webadmin.pages.products',compact('data','products'));
}
}
Products List View Page
#if($products->count())
#foreach($products as $product)
<tr style="color:rgb(26, 25, 25);">
<td>
#foreach($product->images as $pimages)
<!--$pdctImage = $product->images[0]->images_thumbnail;
dd($pdctImage); -->
<img src="../../assets/img/products/thumbnail/{{ $pimages->images_thumbnail }}" alt="ProjectImage" class="img-thumbnail" style="width: 50%; height: 50px;"/>
#endforeach
</td>
<td>{{ $product->product_title }}</td>
</tr>
#endforeach
#endif
The relation ships are defined using foreign key and local key like this - return $this->hasMany(Comment::class, 'foreign_key', 'local_key'); -https://laravel.com/docs/9.x/eloquent-relationships . So you would need to change relationship in both models.
class Products extends Model
{
use HasFactory;
protected $table = 'cw_products';
protected $fillable =
['product_id','product_title','product_details','product_cid'];
public function images()
{
return $this->hasMany(ProductsImage::class, 'images_pdct_id', 'product_id');
}
}
class ProductsImage extends Model
{
use HasFactory;
protected $table = 'cw_products_images';
protected $fillable =
['images_id','images_pdct_id','images_image','images_thumbnail'];
public function products()
{
return $this->belongsTo(Products::class, 'product_id','images_pdct_id');
}
}

Class 'App/Board' not found . Why happen this error

I using relation of model on laravel.
when I access to viewpage but show error [Class 'App/Board' not found].
I have Board.php in App folder absolutely.
I have no idea what's wrong.
I spent 1hours .
please give me advice.
controller
namespace App\Http\Controllers;
use App\Board;
use App\Person;
use App\Http\Requests\PostRequest;
use Illuminate\Http\Request;
class ReviewController extends Controller
{
.
.
.
public function show()
{
$items_p = Person::all();
$data = [
'items_p' => $items_p
];
return view('Review.show', $data);
}
}
Board.php(In App folder)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Board extends Model
{
protected $fillable = [
'person_id',
'message'
];
public function getData()
{
return $this->message;
}
}
Person.php(In App folder)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $fillable = [
'name'
];
public function board()
{
return $this->hasOne('App/Board');
}
}
View(Review.show)
.
.
.
<ul>
<li>
#foreach ($items_p as $item)
{{$item->name}}
{{$item->board->getData()}}
#endforeach
</li>
</ul>
・
・
・
You have an error in slash. you have passed /. it should be \.
class Person extends Model
{
protected $fillable = [
'name'
];
public function board()
{
return $this->hasOne('App\Board');
}
}

How to display a column of different tables using the pivot table?

I am beginner to Laravel and now I am doing this project but I am stuck in this point for a couple days. Here is what I wish do, I would like to display the day and timeslot labels selected by the tutor!
here is my tables and their relationships
https://imgur.com/WMMwhHK
Day Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Day extends Model
{
protected $guarded = ['id', '_token'];
}
Timeslot Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Timeslot extends Model
{
protected $guarded = ['id', '_token'];
public function Tutor()
{
return $this->hasMany(Tutor::class);
}
}
Tutor_availability Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tutor_availability extends Model
{
protected $fillable = ['tutor_id', 'timeslot_id', 'day_id'];
protected $guarded = ['id', '_token'];
public function tutor()
{
return $this->belongsTo('App\Tutor');
}
public function day()
{
return $this->belongsTo('App\Day');
}
public function timeslot()
{
return $this->belongsTo('App\Timeslot');
}
}
tutor_availabilities Migration
public function up()
{
Schema::create('tutor_availabilities', function (Blueprint $table) {
// $table->bigIncrements('id');
$table->integer('day_id')->unsigned();
$table->foreign('day_id')->references('id')->on('days')->onDelete('cascade');
$table->integer('timeslot_id')->unsigned();
$table->foreign('timeslot_id')->references('id')->on('timeslots')->onDelete('cascade');
$table->integer('tutor_id')->unsigned();
$table->foreign('tutor_id')->references('id')->on('tutors')->onDelete('cascade');
$table->timestamps();
$table->primary(['day_id', 'timeslot_id', 'tutor_id']);
});
}
TutorController
public function index()
{
$tutors = Tutor::latest()->paginate(5);
$tutor_availabilities = Tutor_availability::all();
return view('tutors.index', [
'tutors' => $tutors,
'tutor_availabilities' => $tutor_availabilities
]);
}
View
<tr>
<th>Days</th> <th> Timings</th>
</tr>
<tr>
<td>
{{$tutor_availabilities->day->label}}
</td>
<td>
{{$tutor_availabilities->timeslot->label}}
</td>
</tr>
Put this in your Tutor_availability model
public function day()
{
return $this->belongsTo(Day::class);
}
public function timeslot()
{
return $this->belongsTo(Timeslot:class);
}
Then in controller
public function index()
{
$tutor_availabilities = Tutor_availability::all();
return view('tutors.index',
compact('tutor_availabilities')) ;
}
Then in view
<table>
#foreach ($tutor_availabilities as $tutor_available)
<tr><td>
{{$tutor_available->day->label}}
</td>
<td>
{{$tutor_available->timeslot->label}}
</td></tr>
#endforeach
</table>

How do I see published posts count with Eloquent using a ServiceProvider

I have a Blog Categories in the sidebar.blade.php
#foreach ($categories as $category)
<li>
<i class="fa fa-angle- right"></i> {{$category->title}}
<span class="badge pull-right">{{$category->posts()->count()}}</span>
</li>
#endforeach
But this count give me all the posts in my database, even the ones that are scheduled to post at a later date.
PostsController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Category;
class PostsController extends Controller
{
protected $limit = 3;
public function index()
{
$posts = Post::with('author')
->latestFirst()
->published()
->paginate($this->limit);
return view('posts.index', compact('posts'));
}
public function category(Category $category)
{
$categoryName = $category->title;
$posts = $category->posts()
->with('author')
->latestFirst()
->published()
->paginate($this->limit);
return view('posts.index', compact('posts', 'categoryName'));
}
Here's the Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;
class Post extends Model
{
//Table Name
protected $table = 'posts';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
/*protected $fillable = [
'title',
'excerpt',
'body',
'categery_id',
'image',
];*/
protected $dates = ['published_at'];
public function author()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function getImageUrlAttribute($value)
{
$imageUrl = "";
if( ! is_null($this->image))
{
$imagePath = public_path() . "/img/" . $this->image;
if(file_exists($imagePath)) $imageUrl = asset("img/" . $this->image);
}
return $imageUrl;
}
public function getDateAttribute()
{
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}
public function getExcerptHtmlAttribute(){
return $this->excerpt ? Markdown::convertToHtml(e($this->excerpt)) : NULL;
}
public function getBodyHtmlAttribute()
{
return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL;
}
public function scopeLatestFirst($query)
{
return $query->orderBy('published_at', 'desc');
}
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
}
Here's my Category.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
/*protected $fillable = [
'title',
'excerpt',
'body',
'categery_id',
'image',
'slug'
];*/
public function posts()
{
return $this->hasMany(Post::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
Web.php
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/category/{category}', [
'uses' => 'PostsController#category',
'as' => 'category'
]);
Route::resource('books', 'BooksController');
Route::resource('posts', 'PostsController');
Route::resource('categories', 'CategoriesController', ['except'=> ['create']]);
ComposerServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Category;
use App\Post;
class ComposerServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('layouts.sidebar', function($view){
$categories = Category::with(['posts' => function($query){
$query->published();
}])->orderBy('title', 'asc')->get();
return $view->with('categories', $categories);
});
}
}
To recap on what I need done...
I want to just have my published post to be accounted for in the counter to show, not all the of the posts in my database..
(<span class="badge pull-right">{{$category->posts->count()}}</span>)
Post.php
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
But this is not working right for me, does anyone know why?
Try this one:
<span class="badge pull-right">{{$category->posts()->published()->count()}}</span>
Didn't try but this should do the trick.

LARAVEL 5.6 - error Trying to get property 'nsc' of non-object

I want to echo value of NSC from table Group Class but i get this error Trying to get property 'nsc' of non-object
Table Item Name
Table Group Class
Model Item Name
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ItemName extends Model
{
protected $table = 'tbl_item_name';
protected $fillable = [
'inc',
'item_name',
'short_name',
'definition_eng',
'definition_ind'
];
public function GroupClass()
{
return $this->belongsTo('App\GroupClass', 'nsc', 'inc');
}
}
Model Group Class
namespace App;
use Illuminate\Database\Eloquent\Model;
class GroupClass extends Model
{
protected $table = 'tbl_group_class';
protected $fillable = [
'inc',
'nsc',
'description',
'main_group'
];
public function ItemName()
{
return $this->belongsTo('App\ItemName', 'inc', 'nsc');
}
}
Blade
<td>{{ $ItemName->GroupClass->nsc }}</td>
Please help to solve this problem, thank you so much
You should try this::
Blade
<td> {{ isset($ItemName->GroupClass) ? $ItemName->GroupClass->nsc : '' }} </td>
Model Item Name
public function GroupClass()
{
return $this->belongsTo('App\GroupClass', 'inc', 'nsc');
}
Model Group Class
public function ItemName()
{
return $this->belongsTo('App\ItemName', 'nsc', 'inc');
}

Resources