This question already has answers here:
Base table or view not found error
(2 answers)
Closed 3 years ago.
Before copying this project everything was ok but after copying this project to another laptop I am not able to migrate the database error
Illuminate\Database\QueryException : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'discussionforum.channels' doesn't exist (SQL: select * from `channels`)
at /opt/lampp/htdocs/discussionforum/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
661| // If an exception occurs when attempting to run a query, we'll format the error
662| // message to include the bindings with SQL, which will make this exception a
663| // lot more helpful to the developer instead of just the database's errors.
664| catch (Exception $e) {
> 665| throw new QueryException(
666| $query, $this->prepareBindings($bindings), $e
667| );
668| }
669|
Exception trace:
1 Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))
[internal]:0
2 PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'discussionforum.channels' doesn't exist")
/opt/lampp/htdocs/discussionforum/vendor/laravel/framework/src/Illuminate/Database/Connection.php:327
Please use the argument -v to see more details.
You should check AppServiceProvider or other providers which boot before migrate, if you use some table or model in providers u not able to use migration when that table doesn't exist.
If you look better there is error in
1 Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))
[internal]:0
Check providers which using models before any table exists.
If u insist to use model in provider, you should check is it exists before of usage, like this:
Schema::hasTable('mytable'); // off course schema must be imported.
If u post content of you're providers, it will help.
If you don't need to preserve the current data
you could try resetting your data using
php artisan migrate:refresh --seed
and/or delete and recreate your
"database.(sql extension)" file
Related
Hi I am new to creating factories on Laravel and I am setting up a blog website. I had this going just fine and now it isn't working anymore.
When I use "tinker" to add fake data using (App/Models/Post::factory(30)->create();), below error keep occurs. Could anyone please educate me why this error occurs?
App/Models/Post::factory(30)->create();
PHP Warning: Use of undefined constant App - assumed 'App' (this will throw an Error in a future version of PHP) in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: Use of undefined constant Models - assumed 'Models' (this will throw an Error in a future version of PHP) in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: A non-numeric value encountered in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: A non-numeric value encountered in F:\coding\laravel\test-laraveleval()'d code on line 1
PHP Warning: Division by zero in F:\coding\laravel\test-laraveleval()'d code on line 1
[!] Aliasing 'Post' to 'App\Models\Post' for this Tinker session.
Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'sint' for key 'categories_name_unique' (SQL: insert into categories (name, slug, updated_at, created_at) values (sint, ut-quisquam-et-et-tenetur-molestias-in-dolor-voluptatum, 2022-03-16 07:11:05, 2022-03-16 07:11:05))
'
You have to define factory rules such that you have a constraint in your database that checks for category name uniqueness.
You should read how to write a custom factory in Laravel 8:Examples how to generate factories.
Getting this error while calling the stored procedure in laravel controller
$sql = DB::select(DB::raw("CALL whole_x_count('".$node."',#count_750 ,#count_1500 ,#count_3000 ,
#count_t_750 ,#count_t_1500 ,#count_t_3000 ,
#count_p_750 ,#count_p_1500 ,#count_p_3000,
#count_c_1 ,#count_c_2 ,#count_c_3,
#count_c_4 ,#count_c_5 ,#count_c_6)" ));
this is how am calling the procedure
getting this error
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'temp_left_1' already exists (SQL: CALL whole_x_count('jk001',#count_750 ,#count_1500 ,#count_3000 , #count_t_750 ,#count_t_1500 ,#count_t_3000 , #count_p_750 ,#count_p_1500 ,#count_p_3000, #count_c_1 ,#count_c_2 ,#count_c_3, #count_c_4 ,#count_c_5 ,#count_c_6))
This is not a problem with Laravel, but with its procedures that are trying to create a table that already exists. As I see it appears to be a temporary table. An alternative (without change any code in the existing procedures) is to execute a command to delete this temporary table before running this SQL command.
DB::raw("DROP TABLE IF EXISTS temp_left_1");
php artisan migrate:fresh
simply run this
I'm tryin to import and excel file to my SQL Server Database but when I try to create the form to upload the file I get this error:
Illuminate\Database\QueryException
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error converting data type nvarchar to bigint. (SQL: select top 1 * from [schedules] where [schedules].[id] = import)
Screenshoot of error
Even when I have the function in the controller it throws this error. Seems that is trying to get 1 record from database but cannot pass the nvarchar to match the ID column.
I have checked the documentation and nothing. Made it a few times, different methods with same error.
Seems that I figured it out. It was problem with the routing calling de show function
I had the same issue and trying to find the solution came upon this question. Thankfully I read the comments as Fernando's solution was also mine.
This was caused by a misordered Route in web.php
Route::Get('label/{label}' ...
...
Route::Get('label/create' ...
I swapped the order and all fixed
Route::Get('label/create' ...
...
Route::Get('label/{label}' ...
Now I have a problem when use dblink postgres in Laravel query
My query in postgres like that
$query = "SELECT * FROM dblink('host=localhost user=postgres password=123#123a dbname=shbbank',
'SELECT contract_ref_no,currency_iso,report_date FROM bigq.credit_data')
AS source(contract_ref_no text,currency_iso text,report_date date)"
And then I do that query in Laravel
DB::select($query);
I got error:
Undefined function: 7 ERROR: function dblink(unknown, unknown) does
not exist. You might need to add explicit type casts.
Please help me to resolve
It seems that extension dblink has not been created.
Try running script create extension dblink via migration or directly on your database. Once you have created the extension, you can use dblink from the right schema.
I'm trying Laravel transaction for the first time... I do most of my queries with Eloquent and since there no transaction there I have to do a mixture of Eloquent and query builder.
Here is my code:
DB::beginTransaction();
try{
Setting::truncate();
Setting::insert($data);
DB::commit();
jok('all ok');
}
catch (\Exception $e)
{
DB::rollback();
jerror('some error accorded! ');
}
So I've tied to add some invalid data to Settings and i got the some error accorded error as expected but the query before INSERT Setting::truncate(); was executed anyway and I ended up with a empty table.
So either I'm doing something wrong or transaction doesn't work on truncate.
TRUNCATE TABLE in a database transaction will cause a implicit COMMIT action. DELETE does not have that same behavior, so you can safely run
DELETE FROM tblname
This will clear your table and still keep the desired rollback feature of a transaction.
See the MySQL documentation on TRUNCATE. The bullet points explain this functionality http://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
A better solution to do delete is to use Model method:
Setting::query()->delete();