BadMethodCallException in Macroable.php line 81: Method with does not exist. Laravel ORM - laravel

I'm new to laravel, currently using ORM I am trying to retrieve data from parentCategory table by using foreign key present in category table.
following is the code in my category model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class category extends Model
{
protected $table='category';
public function parentCategory(){
return $this->belongsTo('App\parentCategory','mCategoryId');
}
}
following is my parentCategory model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class parentCategory extends Model
{
protected $table='maincategory';
public function categories(){
return $this->hasMany('App\category');
}
}
following is the code of my categoryController by which i am using to retrieve data:
class categoryController extends Controller
{
function index(){
$category=category::all()->with('parentCategory');
$parentCategory=parentCategory::all();
return view('admin.category',['categories'=>$category,'parentCategories'=>$parentCategory]);
}
function add(Request $request){
$category= new category();
$category->categoryName=$request["name"];
$category->mCategoryId=$request["parentCategory"];
$category->save();
return redirect()->route('category');
}
}
the error further states:
at Collection->__call('with', array('parentCategory')) in categoryController.php line 17

In your index() function, you can try
$category = category::with('parentCategory')->get();
This should solve the problem. Follow the Eager Loading documentation.

Related

count(): Parameter must be an array or an object that implements Countable on Laravel relationship

I have forums table, topics table and posts table. the relationship between the posts and topics to forums in one to many. I have implemented the relationship but when I try to access the topics count by doing {{count($forum->topics)}} I get this error: count(): Parameter must be an array or an object that implements Countable However, when I try accessing the posts count in the same way:{{count($forum->posts)}}, it works correctly. Please help
Forum model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Forum extends Model
{
use HasFactory;
protected $guarded = [];
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function topics()
{
return $this->hasMany('App\Models\Topic');
}
public function posts()
{
return $this->hasMany('App\Models\Post');
}
}
Topic model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Topic extends Model
{
use HasFactory;
public function forum()
{
return $this->belongsTo('App\Models\Forum');
}
}
Post Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
public function forum()
{
return $this->belongsTo('App\Models\Forum');
}
}

Cannot access model using controller in Laravel

I create new controller and models using php artisan command. And I tried to access model using controller but I get Class 'Thankful' not found error.
Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Thankful;
class Thanks extends Controller
{
public function hello(){
$my_data = new Thankful;
$data = $my_data->my_model();
return view("thanks",compact('data'));
}
}
Model Code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Thankful extends Model
{
public function my_model()
{
$data = "Thank You";
return $data;
}
}
The namespace of your Thankful model is incorrect:
<?php
namespace App\Http\Controllers;
use App\Thankful;
use Illuminate\Http\Request;
class Thanks extends Controller
{
public function hello ()
{
//
}
}

How to view data form two table join data in Laravel?

I have to table
This is classroom table
And this is joinclass table
Now i want to view classroom data which class_code match form joinclass table.
Here is my classroom model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class classroom extends Model
{
public function user()
{
return $this->belongsToMany('App\User');
}
}
And this is my join class model
<?php
namespace App\Eloquent;
use Illuminate\Database\Eloquent\Model;
class joinclass extends Model
{
public $timestamps= true;
protected $table='joinclass';
protected $guarded=['id'];
public function user()
{
return $this->belongsToMany('App\User');
}
}
So now what should to write in my controller?
Use following simple join query first, and then integrate it with your tables
$arr_tags = DB::table('tags')
->join('users','users.id','=','tags.user_id')
->select('tags.id','tags.status','tags.tag_name','users.first_name','users.last_name','tags.created_at','tags.updated_at')
->get();

Multilevel Relationship in Laravel 5.1

I have three tables and i want to make multiple relationship between then
orders,order_details,book
I want to make a common relation between then to take values in a single array .I am using following Code
Order.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
public $timestamps = false;
protected $table='orders';
public function orderDetails(){
return $this->hasMany('App\OrderDetail','order_id');
}
}
OrderDetail.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderDetail extends Model
{
//
public $timestamps = false;
protected $table='order_details';
public function orders(){
return $this->belongsToMany('App\Order');
}
public function book(){
return $this->hasMany('App\Book');
}
}
and Book.php
public function orderDetail(){
return $this->hasManyThrough('App\OrderDetail','App\Book','id','item');
}
where item is foreign key and primary key in book table
in OrderController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Order,App\OrderDetail,App\Book,App\User;
use Auth;
use App\Http\Requests;
use Response;
use App\Http\Controllers\Controller;
class OrderController extends Controller
{
public function userOrders(){OrderDetail::find($orderId)->where('order_id',$orderId)->where('item','book.id')->get(); }

Call to a member function save() on a non-object

I am building a small laravel app beginning level. I got error in code in I couldn't solve out.
Here is the code
post model
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
public function user()
{
return $this->belongsTo('app\user');
}
}
user model
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
$this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
Thank you in advance for any try from you to solve this query.
Make sure inside your user model you're returning the relationship.
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('app\post');
}
}
Your posted code doesn't have a return inside the posts function.
i think this should fix
add your table field to protected $fillable = [] array
user
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
protected $fillable = ['body'];
public function user()
{
return $this->belongsTo('app\user');
}
}
post
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
protected $fillable = ['username', 'field2', 'blabla'];
public function posts()
{
return $this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post(['body' => $request->body]);
$request->user->posts()->save($post);
return redirect()->route('dashboard');
}
}

Resources