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');
}
}
Related
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');
}
}
Hello I want to use my own repository class in my Laravel 5.8 project
I created my file Repository in the App File and in this file I added A class called ConversationRepository
This is my class:
<?php
namespace App\Repository;
class ConversationRepository{
private $user;
public function __construct(User $user){
$this->user=$user;
}
public function getConversation(int $userId){
return $this->user->newQuery()
->select('name','id')
->where('id','!=',$userId)
->get();
}
}
And then when I use it on my controller :
<?php
namespace App\Http\Controllers;
use App\User;
use Auth;
use Illuminate\Http\Request;
use App\Repository\ConversationRepository;
class ConversationsController extends Controller
{
private $r;
private $auth;
public function __construct(ConversationRepository $conversationRepository,AuthManager $auth){
$this->r = $conversationRepository;
$this->auth = $auth;
}
public function index(){
return view('conversation.index',[
'users'=>$this->r->getConversation($this->auth->user()->id)
]);
}
public function show(User $user){
return view('conversation.show',['users'=>$this->r->getConversation(
$this->auth->user()->id),
'user'=>$user
]);
}
public function store(User $user){
}
}
I get the error
Class App\Repository\User does not exist
Apparently, you forgot to add use App\User; in the class ConversationsController file.
I am trying to out all users on my admin dashboard by parsing the result through to my route
Here is my controller;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$user = User::all()->orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}
I get the "Method Illuminate\Database\Eloquent\Collection::orderBy does not exist." error
You have applied wrong query:
You don't need to add all() as you are using paginate
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('notAdmin');
}
public function index(){
$users = User::orderBy('id', 'desc')->paginate(100);
return view('admin-dashboard')->with('users', $users);
}
}
Folder Structure:
app
|-Admin.php
|-Admin
|
|-Product.php
Admin.php
--------------------------------------------------------
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
public function erp()
{
return $this->belongsToMany(Admin\Product::class);
}
}
-----------------------------------------------------------
Product.php
-----------------------------------------------------------
namespace App\Admin;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $primaryKey = 'productcode';
public $incrementing = false;
public function updatedBy()
{
return $this->belongsTo(Admin::class);
}
}
-----------------------------------------------------------
But got an error Class 'Admin::class' not found
any solution??
Use correct namespaces:
Admin model:
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Admin\Product;
class Admin extends Authenticatable
{
public function erp()
{
return $this->belongsToMany(Product::class);
}
}
Product model:
namespace App\Admin;
use Illuminate\Database\Eloquent\Model;
use App\Admin;
class Product extends Model
{
protected $primaryKey = 'productcode';
public $incrementing = false;
public function updatedBy()
{
return $this->belongsTo(Admin::class);
}
}
Or add namespace of a model as string, for example App\Admin
Please see your name space
Product.php
-----------------------------------------------------------
namespace App\Admin;
So when you call Admin::class it mean App\Admin\Admin::class
So please change your relation like this.
class Product extends Model
{
protected $primaryKey = 'productcode';
public $incrementing = false;
public function updatedBy()
{
return $this->belongsTo('App\Admin');
}
}
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(); }