I'm getting an error when rendering a model Blogs
My errors:
Call to undefined method App\Models\Blog::getContent()
Can you suggest what is the cause of the error.
Model code given below
namespace App\Models;
use App\Models\Categories;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
public $table = "blogs";
protected $fillable = [
'id',
'category_id',
'title_ru',
'title_ua',
'slug',
'img',
'img_min',
'url',
'duration',
'annotation_ru',
'annotation_ua',
'description_ru',
'description_ua',
'meta_ru',
'meta_ua',
'keywords_ru',
'keywords_ua',
'views',
'type_record',
'schedule',
'published',
'created_at',
'updated_at'
];
public function category()
{
return $this->belongsTo(Categories::class);
}
public function scopePublished($query)
{
return $query->where('published', true);
}
}
This code helps to call the model Blog.
Related
I have a project about quizzes and their questions,
I don't know why but when i'm trying to get the quiz's questions i get
this error
This are my files and scripts:
Route web.php file:
Route::get('/admin/quizzes/{id}/questions', 'AdminQuizzesController#questions')->name('admin.quizzes.questions')
Model Quiz.php file:
class Quiz extends Model
{
protected $dates = [ 'start_at', 'end_at', 'created_at', 'updated_at', 'deleted_at'];
protected $fillable = [
'title', 'category_id', 'level_id', 'is_active', 'start_at', 'end_at'
];
public function questions(){
return $this->belongsToMany('App\Question');
}
}
Model Question.php file:
class Question extends Model
{
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
protected $fillable = [
'title', 'correct_answer', 'category_id', 'level_id'
];
public function quiz(){
return $this->belongsTo('App\Quiz');
}
}
Controller AdminQuizzesController.php file:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Quiz;
class AdminQuizzesController extends Controller
{
public function questions($id)
{
$quiz = Quiz::findOrFail($id);
$questions = $quiz->questions;
return view('admin.quizzes.questions.index', compact('quiz', 'questions'));
}
}
the answer of #MazinoSUkah solved my problem.
just did 'composer dump-autoload' in my cmd
AFAIK composer dump-autoload regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project or changed/renamed a class file. Hope it helps.
Model ListDeals
class ListsDeals extends Model
{
protected $table = "deals";
protected $fillable = ['title', 'slug', 'description', 'price', 'has_discount', 'price_to_discount', 'price_reduced', 'status', 'approved', 'suspended', 'start_date', 'end_date'];
public function lists()
{
return $this->belongsToMany('App\Models\Lists', 'list_has_deals' , 'deal_id', 'list_id')->withPivot('list_id');
}
public function user(){
return $this->belongsTo('App\Models\User');
}
Model Lists
class Lists extends Model
{
protected $table = "lists";
protected $fillable = ['title', 'slug', 'short_description', 'description', 'website', 'email', 'phone', 'lat_map', 'lng_map', 'address_reference', 'video', 'renewal_date', 'status', 'approved', 'suspended'];
public function categories()
{
return $this->belongsToMany('App\Models\ListsCategories', 'list_has_categories' , 'list_id', 'category_id');
}
public function deals()
{
return $this->belongsToMany('App\Models\ListsDeals', 'list_has_deals' , 'list_id', 'deal_id');
}
public function user(){
return $this->belongsTo('App\Models\User');
}
Pivot Table list_has_deals
id
list_id
deal_id
Controller HomePageController
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use App\Models\User;
use App\Models\Lists;
use App\Models\ListsCategories;
use App\Models\ListsDeals;
use DB;
class HomePageController extends Controller
{
public function homepage(){
$matchThese = [ 'suspended' => 0, 'status' => 1, 'approved' => 1 ];
$deals = ListsDeals::where( $matchThese )->limit( 3 )->offset( 0 )->orderBy( 'start_date' )->get();
return view( "homepage" )
->with( "deals", $deals );
}
}
I want get in the view the same categories of the list for the deal in the HomePageController, but i dont kwno thw way, i try with withPivot('list_id') but i dont get the id of the list, thank for the help.
Check out the section Retrieving Intermediate Table Columns
https://laravel.com/docs/5.4/eloquent-relationships#many-to-many
I am using Laravel 5.3 and this model:
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use SoftDeletes;
protected $table = 'categories';
protected $fillable = [
'name',
'slug',
'description',
'thumbnail',
'parent',
'created_by'
];
protected $hidden = [
'created_by'
];
protected $dates = ['deleted_at'];
public static function getSubcategories($category)
{
return Category::whereParent(Category::whereSlug($category)->first()->id)->get();
}
}
It works perfectly on my localhost server, but when I upload it on my production server, it outputs following error:
Trying to get property of non-object (on line ....)
It is on this line:
return Category::whereParent(Category::whereSlug($category)->first()->id)->get();
(Lines are hidden, because this model has much more functions and would be too long for this post)
Full trace:
its because the Category::whereSlug($category)->first() is returning null and that you are trying to get id of that null. so its as the error states that you are trying to get a property of non object.
I see that you are trying to get self reference category. you could so it this way as a relationships.
//children
public function categories()
{
return $this->hasMany(self::class, 'parent');
}
//parent
public function parent()
{
return $this->belongsTo(self::class, 'parent');
}
if you want to select recursively you could add this too.
public function parentRecursive()
{
return $this->parent()->with('parentRecursive');
}
public function categoriesRecursive()
{
return $this->categories()->with('categoriesRecursive');
}
like in the headline is written, I'm getting a :
No query results for model [App\Models\Thread\Comment].
Error message. I'm getting this error after I'm trying to delete a Thread. The funny thing is that this Error Message says "No results for the Comment Model. But I wan't to delete a thread, not a comment. It doesn't matter if the Thread have some comments or not, I'm getting this error message every time I try it. I can't really say why, cause I haven't changed the Comment Model. Can someone have a look over it?
My Comment model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public $table = 'comments';
public $fillable = [
'comment',
'thread_id',
'user_id',
];
public function commentuser()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
}
Thread Model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model
{
public $table = 'thread';
public $fillable = [
'thread',
'content',
'user_id',
'themen_id',
];
public function userthread()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
public function threadthema()
{
return $this->belongsTo('App\Thread\Thema', 'thema_id', 'id');
}
}
The delete route:
Route::delete('/show/{id}', ['as' => 'destroycomment', 'uses' => 'Test\\TestController#destroycomment']);
The blade where the threads + the comments are:
http://pastebin.com/0NUPx18C
distroy method in controller:
public function destroy($id)
{
$thread = Thread::query()->findOrFail($id);
$thread->delete();
return redirect(action('Test\\TestController#startpage', [Auth::user()->id]));
}
i'm developing an API but when I create the relations (Many to Many) and want to show in the index function I'm getting an error QueryException in Connection.php line 669 The error says:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CTL_Tags.id' in 'on clause' (SQL: select `CTL_Tags`.*, `CTL_Resource_has_Tags`.`idResource` as `pivot_idResource`, `CTL_Resource_has_Tags`.`idTag` as `pivot_idTag` from `CTL_Tags` inner join `CTL_Resource_has_Tags` on `CTL_Tags`.`id` = `CTL_Resource_has_Tags`.`idTag` where `CTL_Resource_has_Tags`.`idResource` is null)
I believe my error is in my model because it's looking for id in CTL_Tags table when my id name in that table is idTag.
This is my CTL_Resource model
<?php
namespace Knotion;
use Illuminate\Database\Eloquent\Model;
class CTL_Resource extends Model {
public $timestamps = false;
protected $table = "CTL_Resource";
protected $hidden = [
'coachVisibility', 'thumbnail', 'tags', 'relatedTo',
'studentVisibility', 'isHTML','studentIndex', 'coachIndex',
'isURL', 'source', 'path', 'status', 'updateTime', 'isfolder',
'parentResource', 'idModifierUser'
];
protected $fillable = ['idResourceType','productionKey', 'tags', 'idCreatorUser', 'idModifierUser', 'idCreationCountry', 'title', 'description', 'URL', 'fileName', 'extension', 'quicktag', 'minimumAge', 'maximumAge', 'productionKey'];
public function creatorUser() {
return $this->belongsTo('Knotion\OPR_User', 'idCreatorUser');
}
public function creationCountry() {
return $this->belongsTo('Knotion\CTL_Country', 'idCreationCountry');
}
public function resourceType() {
return $this->belongsTo('Knotion\CTL_ResourceType', 'idResourceType');
}
public function quickTags() {
return $this->belongsToMany('Knotion\CTL_QuickTag', 'CTL_Resource_has_QuickTags', 'idResource', 'idQuickTag');
}
public function tags() {
return $this->belongsToMany('Knotion\CTL_Tag','CTL_Resource_has_Tags', 'idResource', 'idTag');
}
public function relatedTo() {
return $this->belongsToMany('Knotion\CTL_RelatedTo', 'CTL_Resource_has_RelatedTo', 'idResource', 'idRelatedTo');
}
}
and I just will show you the code of one of the relations
<?php
namespace Knotion;
use Illuminate\Database\Eloquent\Model;
class CTL_QuickTag extends Model {
protected $table = "CTL_QuickTags";
protected $fillable = ['name'];
protected $hidden = ['status', 'createTime', 'updateTime'];
public function resources() {
return $this->belongsToMany('Knotion\CTL_Resource', 'CTL_Resource_has_QuickTags', 'idResource', 'idQuickTag');
}
}
and this is my Controller
<?php
namespace Knotion\Http\Controllers;
use Illuminate\Http\Request;
use Knotion\Http\Requests;
use Knotion\Http\Requests\ResourcesRequest;
use Knotion\CTL_Resource;
use Knotion\CTL_Tag;
use Knotion\CTL_QuickTag;
use Knotion\CTL_RelatedTo;
use Knotion\CTL_ResourceType;
class ResourcesController extends Controller {
public function index(Request $request) {
$resources = CTL_Resource::paginate(10);
$resources->each(function($resources) {
$resources->tags;
$resources->quickTags;
$resources->relatedTo;
});
return response()->json(
$resources
);
I'll be so grateful who anyone can help me. Thank you so much.
Try to define
$primaryKey
in your model , with the correct column name
https://laravel.com/docs/5.2/eloquent#defining-models