I was creating one model file and trying to fetch the table data using this model.
Every time it's showing the error below:
"SQLSTATE[42S02]: Base table or view not found: 1146 Table
'laravel_student.students' doesn't exist (SQL: select * from
students)"
The controller file looks like to the following:
namespace project1\Http\Controllers;
use Illuminate\Http\Request;
use project1\Student;
use project1\Http\Requests;
class StudentController extends Controller
{
public function index()
{
$students = Student::all();
return view('student_form',compact('students'));
}
}
Open your Student Model
and add $table
class Student extends Model {
protected $table = 'student';
}
Follow Eloquent Defining Models for more details
Related
I am trying to make Multi-Level Category using laravel but I am facing this error: How to fix this error?
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view
not found: 1146 Table 'storykadb.category_navmenus' doesn't exist
(SQL: select * from category_navmenus where p_id = 0)
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class category_navmenu extends Model
{
public function childs(){
return $this->hasMany('App\category_navmenu','p_id');
}
}
Route
Route::get('test',function(){
return App\category_navmenu::with('childs')->where('p_id',0)->get();
});
Laravel Eloquent is taking the wrong table name, either you can change your Model name and table name according to Laravel's naming convention or add in the $table property in your model, like this :
namespace App;
use Illuminate\Database\Eloquent\Model;
class category_navmenu extends Model
{
public $table = "category_navmenu";
public function childs(){
return $this->hasMany('App\category_navmenu','p_id');
}
}
Because Laravel will find your table by changing your classname to lowercase underscore and plural. Just like:
CategoryNavmenu will become category_navemenus.
You have no specifies the table name of the protected $table. So laravel will find the table by default rule.
You can put this line in your model:
protected $table = 'category_navemenu';
so laravel can find your table.
But the important thing is plz change your class name to Upper Camel Case。This is a normal grammatical norms.
Just Change your model file name to CategoryNavmenu.php and classname to CategoryNavmenu.
I have a pivot model for a Many-to-Many relationship called UserWebpage.
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserWebpage extends Pivot
{
public function collections(){
return $this->belongsToMany('App\Collection', 'collection_user_webpage');
}
}
I also have a model called Collection
namespace App;
use Illuminate\Database\Eloquent\Model;
class Collection extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function userWebpages(){
return $this->belongsToMany('App\UserWebpage', 'collection_user_webpage');
}
}
I'm trying to establish a Many-to-Many relationship between UserWebpage and Collection. I know something like this should be possible based on this SO question that I found:
Laravel: How to use multiple pivot table relationships
My issue is this:
When I try to insert a relationship between a UserWebpage instance and a Collection instance, I get the following error.
Code to insert the relationship:
App\UserWebpage::where('user_id', $user->id)->take(2)->get()->each(function($user_webpage) use ($user){
$collection = factory(App\Collection::class)->create(['user_id' => $user_webpage->user_id]);
$collection->userWebpages()->attach($user_webpage->id);
});
The error I get:
Illuminate\Database\QueryException : SQLSTATE[42S22]: Column not
found: 1054 Unknown column '' in 'field list' (SQL: insert into
collection_user_webpage (``, collection_id) values (1, 1))
Is it possible to create this type of a relationship? Or am I missing something?
The default value for $foreignPivotKey doesn't work in Pivot, you have to explicitly specify it:
public function collections(){
return $this->belongsToMany('App\Collection', 'collection_user_webpage', 'user_webpage_id');
}
I'm trying to access a custom pivot class directly through Artisan tinker like this: App\Registration::all(). But it seems that classes that extend pivot are not directly accessible?
The Errormessage: Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'teknologiskolen.registration' doesn't exist (SQL: select * from registration where registration.id = 1 limit 1)'
Specify the table in the pivot model:
class Registration extends Pivot {
protected $table = 'registrations';
}
I have created both employes and employes_detail tabel with the data
i have created model for both of the table that is given below:
emloye model:
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use App\Http\Model\EmployeDetail;
class Employe extends Model
{
public function employes_detail()
{
return $this->hasOne(EmployeDetail::class);
}
}
and eployedetail model:
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
class EmployeDetail extends Model
{
public function employe()
{
public function employe()
{
return $this->belongsTo(Employe::class);
}
}
}
and in controller i used like :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Model\Employe;
use App\Http\Model\EmployeDetail;
class EmployeController extends Controller
{
public function index(Request $request)
{
$Employe=Employe::all();
$convert=$Employe->toArray();
echo "<pre>";print_r($convert);exit;
//return view('employe.employe');
}
}
it showing only employe table data how can i show the data for the
employes_detail as well as .still i am not able to understand it on
laravel documentation can anyone please help me related this.
how can i get the all data from employes and employes_details table for all the records
but when i used this code in controller:
public function index(Request $request)
{
$Employe=Employe::where('id',1)->first();
//$convert=$Employe->toArray();
echo "<pre>";print_r($Employe->employes_detail);exit;
//return view('employe.employe');
}
its shows me the employe_detail table data
but i want both of the table data in a same array and i dont want to use where condition here.
the function employes_detail and employe in your models only declares the relationships between the models but if you want to load the relationship, you can try this :
Employe::with('employes_detail')->get();
or
$employees = Employe::all(); $employees->load('employes_detail');
Then you can access for each employees the relation attribute like that :
foreach($employees as $employe) {
$employe->employes_detail->id;
}
Hopes it helps you.
i'm trying to create a system of register and authentication using three tables. Users, Account and Profile.
But when attempting to use a registration form displays the following message below:Tables
Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from users where email = thiagothgb#gmail.com limit 1)
I do a relationship in the models Users like that.
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use App\Model\Account;
use App\Model\Profile;
class Users extends Model
{
protected $table = 'users';
protected $connection = 'mysql';
public function account(){
return $this->hasOne('App\Model\Account');
}
public function profile(){
return $this->hasOne('App\Model\Profile');
}
}
What I can try to for to solve this problem and extend Models when i go create a object extended.