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.
Related
have been trying to get the output of these code from my vew but its giving me issues, Please i woild really be bappy to get help.
In my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
$data = App\allLeave::find(1)->full_name;
return view('leave/allLeave',["data"=>$data]);
}
}
in my employee model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class Personnel
* #package App
*/
class Employee extends Audit
{
public function leave()
{
return $this->belongsTo('App\allLeave');
}
}
In allLeaveModel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class allLeave extends Model
{
public function empolyee()
{
return $this->hasMany('App\Employee');
}
}
in the blade
{{$data->employee->full_name}}
You already assign full name to data in controller. You only need {{ $data }} in blade
If i have to do the same i will do it simply like
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;
class LeavesController extends Controller
{
public function getAllLeave()
{
/* No need to use App\allLeave because you already have that used in
top of the project */
$data=allLeave::findorFail(1);
return view('leave.allLeave')->with('data', $data);
}
}
in front end just use
{{$data->first_name}} //same column as in database table
Note: Make sure are using laravel eloquent model relationships
In your controller should be like this:
$data = App\allLeave::find(1)->empolyee();
And your blade:
{{$data->full_name}}
Here is the Model which simply inserts data into the table:
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserHandle extends Model {
public $timestamps=false;
}
Here is the Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\UserHandle;
class UserHandleCntrl extends Controller {
public function index(){
$data = new UserHandle;
$data->name='Laravel';
$data->email='dbtcbd#gmail.com';
$data->username='bdlaravel';
$data->password='laravel12345';
$data->save();
echo 'Data Inserted';
}
}
Here is the route :
Route::get('/user', 'UserHandleCntrl#index');
But when I am trying to insert data into the database I am getting error that model class not found. Why I am getting this and what is the solution. Please help. Bellow is the error screenshot.
Keep the a of App capital while you are including that model.
use App\UserHandle;
including model use App instead of app in your controller file.
use App\UserHanle;
I wanna try to check the role of the auth user.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\User;
use App\Newstb as NewstbEloquent;
use Redirect;
use Spatie\Permission\Traits\HasRoles;
class NewsController extends Controller
{
use HasRoles;
protected $guard_name = 'web';
public function createNews(Request $request){
$id=Auth::user()->id;
$user = User::find($id);
if($user->hasRoles('TA')){
return "You are TA";
}
else{
return "you are not TA";
}
}
}
anything else i didnt set?
when i triggered this function, it will show "Method Illuminate\Database\Query\Builder::hasRoles does not exist."
Have anyone know how to solve it? or any method to debug it?
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.
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