I'm working on laravel's project flyer and I'm continually getting this error from artisan tinker.
$flyer->photos()->create(['photo' => 'foo.jpg']);
BadMethodCallException with message 'Call to undefined method Illuminate
\Database\Query\Builder::photos()'
Here is my Flyer.php file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flyer extends Model
{
public function photos()
{
return $this->hasMany('App\Photo');
}
}
and here is my Photo.php file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
protected $table = 'flyer_photos';
protected $fillable = ['photo'];
public function flyer()
{
return $this->belongsTo('App\Flyer');
}
}
looks like the method photos() doesn't get recognised or something
From the error description, $flyer is not a Flyer object, it's an Illuminate \Database\Query\Builder object. That's your error. When creating $flyer, be sure to use a get() or first() after building your query.
Related
I just started with laravel media library. While uploading images to the database I am getting an error. I tried searching in documentation But I am not finding an answer for this.
the error which I am getting
Argument 1 passed to Spatie\MediaLibrary\MediaCollections\FileAdder::processMediaItem() must be an
instance of Spatie\MediaLibrary\HasMedia, instance of App\Service given, called in
C:\xampp\htdocs\Matheen\furniture_backend\vendor\spatie\
laravel-medialibrary\src\MediaCollections\FileAdder.php on line 372
controller
public function store(Request $request)
{
$service = Service::create([
'service_name' => $request->service_name
]);
$file = $request->file('image');
$service->addMedia($file)->toMediaCollection('services');
return redirect('services')->with('success','Service Added Successfully');
}
Model
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\HasMedia;
class Service extends Model
{
use InteractsWithMedia;
protected $fillable = ['service_name'];
public function registerMediaCollections(): void
{
$this->addMediaCollection('services');
}
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumbnail')
->width(1000)
->height(250);
}
}
in model class statement
you have: "class Service extends Model"
you need: "class Service extends Model implements HasMedia"
...
...
use Spatie\MediaLibrary\HasMedia;
class Service extends Model implements HasMedia
{
// code
}
..
i did this
composer require spatie/laravel-medialibrary:10.0.7
and problem solved
laravel 9
medialibrary 10.0.7
PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Tpost;
class PostController extends Controller
{
public function createUserPost(Request $request){
$this->validate($request,[
'post'=>'required'
]);
$tpost =new Tpost();
$tpost->body =$request['post'];
$message = "something wrong";
if($request->tuser()->tposts()->save($tpost)) //problem is there
{
$message = "post Successfully Submited";
}
return redirect()->route('dashboard')->with('message',$message);
}
}
Tpost.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tpost extends Model
{
//
public function tuser(){
return $this->belongsTo('App\Tuser');
}
}
Tuser.php
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class Tuser extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function tposts(){
return $this->hasMany('App\Tpost');
}
Got error: BadMethodCallException in Macroable.php line 81: Method
tuser does not exist.
//If I use
$tpost ->save();
//simply inserted into database //I thought I failed to make relation
between Tuser and Tpost models.
In PostController.php, If I do dd($tpost->tuser()); then it returned an associative array as shown, but If do dd($request->tuser()); then it says Method tuser does not exist.
please Help....
You have a typo in your method call precisely where you write "problem is there". It should read:
if($request->tuser()->tposts()->save($tpost))
Edit--
Judging from the error message, it's pretty clear that Laravel thinks you're asking for the tuser() method on the $request instance, which obviously doesn't exist.
Can you use:
Auth::user()->tposts()->save($tpost)
If this doesn't work, I'd suggest making sure that the config/auth.php file reflects your particular usage case, specifically the User Providers section.
I am new to Laravel, I am trying things around while going through a tutorial. This is where I am facing an unexpected behaviour.
I have a model tweet and a controller named tweetsController; when I call tweet::find() or any similar method I found this:
FatalErrorException in tweetsController.php line 13:
Class 'App\Http\Controllers\tweet' not found
I have also tried App\tweet::find().
Everything seems fine through tinker.
Please explain.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class tweetsController extends Controller
{
public function show(){
$data = tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
public function delete($id){
return "here we dele the tweet ".$id;
}
public function add(){
return "i add your tweet to database then show you all the tweets";
}
}
tweet.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tweet extends Model
{
protected $fillable = array(
'tweetHead',
'tweetBody'
);
}
?>
A few options the maybe generating this error:
The model/controller namespace is incorrect;
The name of the file and the class name for the model needs to be "Tweet" with the first letter in uppercase;
If you set the right namespace on the model "Tweet.php" and import that on your "TweetController.php"
I hope that helps :)
UPDATE:
In the TweetController.php add this
use App\Tweet;
Before the class declaration, like this
use App\Tweet;
class tweetsController extends Controller
{
And remember to change the controller name in the class declaration like this
class TweetsController extends Controller
{
And the controller filename will become "TweetsController.php"
The Model also has to be named "Tweet" and not "tweet" in the class declaration and the filename
class tweet extends Model
will become
class Tweet extends Model
and the file will be named "Tweet.php"
and everytime you need to call the model you will do this
public function show(){
$data = App\Tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
Add use Add\User below namespace in your controller
I'm new to Laravel, and using Laravel 5 i'm having trouble returning an array from my database.
I have several "acts", and each act has many "banners". Whenever I try to get output from my array of banners ( $act->banners->count() ), i find it throws an error because it is null.
Here is the code:
routes.php:
Route::model('banners', 'Banner');
Route::model('acts', 'Act');
// Controller routes
Route::resource('acts', 'sf_ActController');
Route::resource('acts.banners', 'sf_BannerController');
Route::bind('banners', function($value, $route) {
return App\Banner::whereact_id($value)->first();
});
Route::bind('acts', function($value, $route) {
return App\Act::whereact_id($value)->first();
});
Act.php (model)
namespace App;
use Illuminate\Database\Eloquent\Model;
class Act extends Model
{
protected $table = 'sf_act';
protected $primaryKey = 'act_id';
public function act() {
return $this->hasMany('Banner');
}
}
Banner.php (model)
namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model
{
protected $table = 'sf_banner';
protected $primaryKey = 'banner_id';
public function banner() {
return $this->belongsTo('Act' , 'act_id' , 'act_id');
}
}
sf_ActController.php (controller)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Act;
use App\Banner;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use Redirect;
class sf_ActController extends Controller
{
public function show(Act $act)
{
//pass object to correct view
return view('pages.acts.show' , compact('act'))->with('banner', Banner::find($act));
}
acts/show.blade.php (view)
<!-- /resources/views/acts/show.blade.php -->
#extends('app')
#section('content')
<h2>{{ $act->act_title }}</h2>
{{ $act->banners->count() }}
at this point I get the following error:
FatalErrorException in a03036ad81fb4e6d90e9fe5e3da62c65 line 7:
Call to a member function count() on null
Why am I not fetching my banner data!? (The title variable in the h2 tags outputs fine, so the db and everything up until that point is working.)Thanks.
You need to specify the complete "route" to the model in the relationships including the namespace:
public function act() {
return $this->hasMany('App\Banner');
}
And the same on belongsTo:
public function banner() {
return $this->belongsTo('App\Act' , 'act_id' , 'act_id');
}
Could be a good idea to include the name of the foreign kay in the hasMany method.
public function act() {
return $this->hasMany('App\Banner', 'act_id');
}
Also possibly you don't need to include the third parameter in the belongs to.
Hope it helps. Also can share with you a link to learn about Laravel step-by-step: Learn Laravel 5.0 => 5.1
You currently have your hasMany relationship setup like this:
public function act() {
return $this->hasMany('Banner');
}
However in your view your calling this relationship:
$act->banners->count()
Shouldn't it be:
$act->act()->count();
I get the following error while trying to retrieve a array list which we use to load into the view to create a select list.
Error:
Call to undefined method Illuminate\Database\Query\Builder::albums
In our controller we are using following:
$albums = \Auth::user()->albums->lists('name', 'id');
And in model Albums.php we are using:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Albums
*
*/
class Albums extends Model
{
protected $table = 'albums';
}
In in our main file:
public function albums()
{
return $this->hasMany('App\Models\Albums', 'name', 'id');
}
The problem is \Auth::user() object does not have albums function
The albums() function must be created in User model class
After searching around, i have solved it by adding the following to the User() class.
public function albums()
{
return $this->belongsTo('App\Media');
}