When calling a Controller function, in a Laravel 5.6 blade/view, I get the following error message.
Method Illuminate\Database\Query\Builder::show does not exist
I can't find where the error or the source of this error is. I read dozens of posts here with the same error, but none of them were related to code/context/type of models/controllers(...) that I have.
Blade view where the error message is shown.
Blade
#foreach (\App\Portfolio::show() as $port)
<option value="{{$port->id_portfolio}}">{{$port->name}}</option>
#endforeach
Controller
class PortfolioController extends Controller
{
public function show()
{
$portfolio = \App\Portfolio::where([
['flg_active', '=', true],
['id_user', '=', Auth::user()->id]
])->get();
return $portfolio;
}
}
Model
class Portfolio extends Model
{
public function transaction()
{
return $this->hasMany('App\Transaction', 'id_portfolio', 'id_portfolio');
}
public function user()
{
return $this->belongsTo('App\User', 'id_user', 'id');
}
}
Test with all() instead of show(), and it works.
In Blade you call the model.
And the method you need is in the controller PortfolioController
PortfolioController
public static function show()
Blade
#foreach (\App\PortfolioController ::show() as $port)
<option value="{{$port->id_portfolio}}">{{$port->name}}</option>
#endforeach
Related
I actually managed to get data from HasManyThrough relations, but when I want to reach the relational data, it throws the error:
Property [publishings] does not exist on this collection instance.
(View: C:\Xampp\htdocs\wave\resources\views\pages\category.blade.php)
As can be seen in the picture down below, I received "publishings" data.
Where I got the error from is this Blade file:
category.blade.php
#foreach ($data->publishings as $item)
<div></div>
#endforeach
Since I have the data, the codes down below can be unnecessary for the solution, but they are there, just in case.
CategoryController.php
public function index($category)
{
$data = Category::where('slug', $category)
->with(['publishings' => function ($query) {
$query->where('slug', '!=', 'placeholder')->latest()->paginate(10);
}])->get();
return view('pages.category')->with('data', $data);
}
Category.php
public function assets()
{
return $this->hasMany('App\Models\Asset');
}
public function publishings()
{
return $this->hasManyThrough('App\Models\Publishing', 'App\Models\Asset');
}
Asset.php
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function publishings()
{
return $this->hasMany('App\Models\Publishing');
}
Publishing.php
public function asset()
{
return $this->belongsTo('App\Models\Asset');
}
Thanks for your help.
You are passing an Eloquent Collection to the view. If you want to query only the first result, you can use the first() method of the Query Builder instead of get():
public function index($category)
{
$data = Category::where('slug', $category)
->with(['publishings' => function ($query) {
$query->where('slug', '!=', 'placeholder')->latest()->paginate(10);
}])->first();
return view('pages.category')->with('data', $data);
}
If anyone has the same problem and comes across this message, the solution, in my case was actually quite easy. The 3rd line in the picture says:
0 => App\Models\Category
And I was trying to reach the relational data as if the $data is an object. That's why
$data->publishings
thrown the error. Solution is
$data[0]->publishings
Or return only that data from the controller:
return view('pages.category')->with('data', $data[0]);
I got stuck with one to one relation
Facade\Ignition\Exceptions\ViewException
Class 'App\App\User' not found (View: /var/www/html/project1/resources/views/pengajuan/datauser/index.blade.php)
http://app.web/pengajuan/datauser
my User Model
public function dataUser()
{
return $this->hasOne(App\DataUser::class);
}
my DataUser Model
public function User()
{
return $this->belongsTo(App\User::class);
}
my controller
public function index()
{
$datauser = DataUser::all();
return view('pengajuan.datauser.index', compact('datauser'));
}
and my index view
#foreach ($datauser as $d)
<td>{{$d->masa_penilaian}}</td>
<td>{{$d->user->nama}}</td>
<td>{{$d->nip}}</td>
<td>{{$d->nuptk}}</td>
#endforeach
Can Help Me?
Thanks
Try DataUser::class instead of App\DataUser::class and User::class instead of App\User::class?
How do I make One to Many Accessor ?
User Model :
public function histories()
{
return $this->hasMany('App\History');
}
History Model :
public function user()
{
return $this->belongsTo('App\User');
}
If I do like below, it will works.
User Model :
public function getHappyHistoryAttribute()
{
return $this->products()->happyHistory()->first()->text;
}
History Model :
public function scopeHappyHistory(Builder $builder)
{
return $builder->where(function (Builder $query) {
return $query->where('status', 'happy');
});
}
Controller :
public function show($id)
{
return view("my_view", [
'user' => User::find($id)
]);
}
Blade view :
{{ dd($user->happy_history) }}
It works !
But, How should I do if I want this :
{{ dd($user->histories->happy) }}
I try like
History Model :
public function scopeHappyHistory(Builder $builder)
{
return $builder->where(function (Builder $query) {
return $query->where('status', 'happy');
});
}
public function getHappyAttribute()
{
return $this->happyHistory->first()->text;
}
It doesn't works !
If I code like
{{ dd($user->histories->happy) }}
It will return
Property [happy] does not exist on this collection instance.
If I code like
{{ dd($user->histories()->happy) }}
It will return
Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$happy
Correct me If I wrong
It will not work like that because the relationship is oneToMany.
Try following in your User model
/**
* #return mixed
*/
public function happyHistory()
{
//Happy history is your scope form History model
//Following will not get you first model
return $this->histories()->happyHistory()->first();
}
That will give you first model and then you can do following from User model
$user->happyHistory->text
Not tested but this should work.
Also you dont need to pass the query builder in scope. You can do following
public function scopeStatus($query, $status = "happy")
{
return $query->where('status', $status);
}
Now you can use it for all status type.
History::status()->all(); //Will return all Happy history
History::status("sad")->all(); //Will return all Sad history
Hope this helps.
I have a blog and want to include the Users Name when shown to the public.
When creating the blog I make sure to include the user_id in the blogs table
In my Blog model I have the following:
public function users()
{
return $this->belongsTo(User::class);
}
In my Users model I have:
public function blogs()
{
return $this->hasMany(Blog::class);
}
In my Blog Controller I have:
public function index(User $user)
{
$users = User::get();
$blogs= DB::table('blogs')->where('user_id', '=', $users->id)->orderBy('id', 'DESC')->paginate(6);
return view('blogs.index',compact('blogs'));
}
Then in my view:
#foreach($blogs as $blog)
<h1>{{$blog->title}}</h1>
Source:{{$blog->users->first_name}} // This does not work
Source:{{$blog->first_name}} // This does not work either
#endforeach
I thought I could do something like this to show the names:
{{ $blogs->users->first_name }} {{ $blogs->users->last_name }}
But this isn't working either...
Try this:
#foreach($blogs as $blog)
<h1>{{$blog->title}}</h1>
{{$blog->user->first_name}}
#endforeach
And on your Blog Model
public function user()
{
return $this->belongsTo(User::class);
}
In your Blog controller the variable $blog needs to be $blogs. You also have extra characters (right parenthesis) in your Blade. It should be:
#foreach($blogs as $blog)
Source: {{ $blog->user->first_name }}
...
#endforeach
Blog Model
This function replaces the old "users" function, as only one user is returned (belongsTo is a singular relationship).
class Blog extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
User Model
public function blogs()
{
return $this->hasMany('App\Blog');
}
Controller Function
And, as such, you can cut down your controller code, including removing the redundant elements.
public function index(User $user)
{
$blogs = Blog::where('user_id', '=', $user->id)->orderBy('created_at','desc')->paginate(6);
return view('blogs.index', compact('blogs'));
}
The way you did is called Query Builder
$blogs= DB::table('blogs')->where('user_id', '=', $users->id)->orderBy('id', 'DESC')->paginate(6);
Query Builder does not support lazy loading, cause lazy loading is only supported for the Eloquent method
$blog->users->first_name
For Eloquent way you can try this instead:
$blogs = Blog::where('user_id', $user->id)->get()
foreach($blogs as $blog){
dd($blog->user); // you will get the user detail here
}
For lazy loading have a performance issue when come to load heavy data so to prevent lazy loading can use this
$blogs = Blog::with('user')->where('user_id', $user->id)->get()
For more information can look at Eloquent Relationship Documentation
For query builder, the only way to link your user is use join, which will be something like this
$blogs = DB::table('blogs')
->join('users', 'users.id', '=', 'blogs.user_id')
->get();
foreach($blogs as $blog){
dd($blog->first_name) // user first name
}
For more information can look at Query Builder Join
BlogController.php
public function index(){
$blogs = Blog::with('user')->get();
return view('blogs.index')->with('blogs',$blogs);
}
Blog.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function blogs()
{
return $this->hasMany('App\Blog');
}
I'm looking for some help. I've searched on other topics, and saw what is the problem approximatively, but didn't succeed to fix it on my code.
Now the question is: I have NotFoundHttpException when i try to submit an update on my code.
Here is the Controller and my function update
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\T_collaborateurs_table;
class testing extends Controller
{
public function index()
{
$user = T_collaborateurs_table::all();
return view ("read", compact("user"));
}
public function create()
{
return view("create");
}
public function store(Request $Request)
{
T_collaborateurs_table::create(Request::all());
return redirect("index");
}
public function show($id)
{
$user=T_collaborateurs_table::find($id);
return view("show", compact("user"));
}
public function edit($id)
{
$user=T_collaborateurs_table::find($id);
return view("update", compact("user"));
}
public function update(Request $Request, $id)
{
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
}
Now the routes
Route::get("create", "testing#create");
Route::post("store", "testing#store");
Route::get("index", "testing#index");
Route::get("show/{id}", "testing#show");
Route::get("edit/{id}", "testing#edit");
Route::patch("update/{id}", "testing#update");
And now the view update.blade.php
<body>
{{Form::model($user, ['method'=>'patch', 'action'=>['testing#update',$user->id]])}}
{{Form::label('Id_TCa', 'ID')}}
{{Form::text('Id_TCa')}}
{{Form::label('Collaborateur_TCa', 'collab')}}
{{Form::text('Collaborateur_TCa')}}
{{Form::label('Responsable_TCa', 'resp')}}
{{Form::text('Responsable_TCa')}}
{{Form::submit("update")}}
{{Form::close()}}
</body>
Here the route:list
I'm sorry if my words are not very understable...
Thank you all for your time.
{{Form::model($user, ['method'=>'PATCH', 'action'=> ['testing#update',$user->id]])}}
Or try to use 'route' instead of 'action',to use 'route' you just need a little edit in your update route.
Route::patch("update/{id}", array('as' => 'task-update', 'uses'=>'testing#update'));
in your view:
{{Form::model($user, ['method'=>'PATCH', 'route'=>['task-update',$user->id]])}}
And please follow the convention of class naming. Your class name should be 'TestingController' or 'Testing'.
You could try method spoofing by adding
{{ method_field('PATCH') }}
in your form and change the form method to POST
{{ Form::model($user, ['method'=>'POST', 'action'=>['testing#update', $user->id]]) }}
add the id as an hidden field
{{ Form::hidden('id', $user->id) }}
access the id in the controller as
public function update(Request $Request)
{
$id = Input::get('id');
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
also need to modify your route accordingly
Route::patch("update", "testing#update");
Try using on function update:
return redirect()->route('index');