Laravel retrieve records from database using mysql view instead of the table itself - laravel

I know this is a simple question and I tried hard to search a solution for this. Maybe someone knows or experienced this already that might help me.
I am only a beginner in using Laravel please bear with me.
So I have this AgencyModel, the name of my table in the database is agency.
And I have a MySQL view which is named view_agency, this contains the inner join of the agency table and other tables that has connection with it.
Model (This is the working code)
protected $table = 'agency';
The records displays when I use the agency as the table name
But MySQL view table view_agency does not display/retrive the records from the database.
Model (This code is not working)
protected $table = 'view_agency';
I have a suspicion that the laravel eloquent might have to do with this, because $table is only being accepted and not mysql view.
use Illuminate\Database\Eloquent\Model;
Thanks...

In SQL, a view is a virtual table based on the result-set of an SQL statement.
And you can get datas from view just like table's datas:
select * from view_agency
So you can use Laravel query builder or Eloquent builder to query records like table,
Laravel will convert the query builder or Eloquent to sql, so it will works:
DB::table('view_agency')->get();
I used do this before, I created a views/ directory inside the models/, and create the all views' files inside.
I think you can create that view model like this:
namespace App\Models\Views;
use Illuminate\Database\Eloquent\Model;
class ViewAgency extends Model {
}
However, I am following the laravel's way. I use the lowercase underscore plural form. So when I use ViewAgency, Laravel will find the table or view's name which is view_agencies:
ViewAgency::first();
It works fine.
And If you want to change the name, and I think there is no error with protected $table = 'view_agency;', You can try this way:
class ViewAgency extends Model {
public function __construct()
{
$this->setTable('view_agency');
}
}
This will work, too

Related

Laravel: Saving a relationship when instanciating an Eloquent model rises this SQL error: "Integrity constraint violation"

Summary
Context and Needs
Minimal, Testable and Executable sources (with instructions for testing)
Actual Results and Expected Results
What I've tried
The Question
Context and Needs
The relationship between both Eloquent models GalleryImage and GalleryGroup is: GalleryImage * <-> 1 GalleryGroup. I want to save an instance of GalleryGroup, then of GalleryImage.
Minimal, Testable and Executable sources
Instructions to test
I wanted to show you how to test my code in the case you really want to do it ;-) . However, I think you don't actually need to test. Indeed, the code is very simple. By reading it, if you know more than me Laravel, maybe you will find the problem and be able to bring me some help. I let you reading the following contents but I think you'll agree with me.
Create the tables for GalleryGroup and GalleryImage (out of topic). The fields to create and the name of the tables are contained in the following sources.
Copy/Paste the Eloquent models and the script that instanciates them and tries to save them in DB.
Creates the routes of your choice to run the script and then, run the script (ie.: access the Web page or use a REST client)
The Eloquent models
-- GalleryGroup.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class GalleryGroup extends Model
{
use HasFactory;
protected $primaryKey = 'group_id';
private $name;
public function images() {
return $this->hasMany(GalleryImage::class);
}
}
-- GalleryImage.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class GalleryImage extends Model
{
use HasFactory;
protected $primaryKey = 'image_id';
public function group() {
return $this->hasOne(GalleryGroup::class, 'group_id', 'image_id');
}
}
Instanciations and concretization of the relationship
The Eloquent model GalleryGroup is instanciated and saved in db; then, the Eloquent model GalleryImage is instanciated and should be saved in db:
$img_group = new GalleryGroup();
$img_group->name = 'foobar';
$img_group->save();
$image = new GalleryImage();
var_dump($img_group->group_id); // It exists and it's not empty
$image->group()->save($img_group);
$image->save();
Actual Results and Expected Results
The last line is never executed because this error is raised at the line $image->group()->save($img_group);:
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'group_id' cannot be null (SQL: update gallery_groups set group_id = ?, gallery_groups.updated_at = 2021-01-09 10:16:44 where group_id = 24) in file /var/www/html/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
I don't understand why it tries to update the group entry, and I don't understand why group_id is NULL or empty, because $img_group actually has a non-empty group_id (cf.: the line var_dump($img_group->group_id);).
The actual results are: 1) the model GalleryGroup is correctly instanciated and correctly saved in db and 2) the model GalleryImage is correctly instanciated and not saved in db because of the above SQL error is raised.
The expected results are: 1) the model GalleryGroup is correctly instanciated and correctly saved in db and 2) the model GalleryImage is correctly instanciated and saved in db.
What I've tried
I've tried to var_dump several times several variables but did not found any relevant information to help debugging this issue.
I've read and re-read the docs https://laravel.com/docs/8.x/eloquent-relationships#the-save-method and https://laravel.com/docs/8.x/eloquent#primary-keys but did not found any relevant information to help debugging this issue.
The Question
Why is this error raised and how to fix it?
One of these relationships needs to be a belongsTo as one of these tables has the foreign key on it that relates to the other table. I would assume a GalleryImage belongs to a GalleryGroup:
GalleryGroup
images
hasMany GalleryImage
GalleryImage
gallery
belongsTo GalleryGroup
Once those are setup correctly you should be able to do this to save the relationship:
$img_group->images()->save($image);

Laravel - Model get data from another Model

I'm quite new to laravel and I'm trying to understand the Eloquent Relations.
I've already read some answers and the documentation but I haven't found a simple case similar to mine.
I have two model with one-to-many relation.
Document Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Document extends Model
{
public function dossiers()
{
return $this->belongsTo('App\Dossier');
}
protected $table = 'documents';
protected $primaryKey = 'id_document';
}
Dossier Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Dossier extends Model
{
public function documents()
{
return $this->hasMany('App\Document');
}
protected $table = 'dossiers';
protected $primaryKey = 'id_dossier';
}
So there is an attribute "protocol" inside both table. Inside Dossier is an unique attribute, inside Document can be repeated because more documents may be part of same dossier with that protocol.
Let's suppose I have to create a Document Model. But I need an attribute "color" that already exists in a Dossier.
so:
I have a form for the document. That can set all attribute except
color (so it can create the protocol shared with Dossier)
I submit the form then check if exists a Dossier with that protocol
(that is not the primary_key)
If i find a Dossier with that protocol, I took the Color of that dossier and
I insert it in the Document Model.
I'd like to know how I should implement the second step.
At the moment I've wrote in the DocumentController:
$document = Dossier::where('protocol', '=', $request->protocol)->first();
and then
$document -> color;
But I fell that's not the way.
Thanks for any advice
I think you want to insert color in document from dossier table if protocol match. If i understand you correctly then you can write it like this.
$dossier = Dossier::where('protocol', '=', $request->protocol)->first();
$color = ($dossier) ? $dossier->color: 'defaultColor';
Now in your create document
$document -> create([
'color' => $color,
... other form data here
]);
Note: it is not good practice to use primary key field as id_document or id_dossier. Better you change these to id, otherwise your current relationship will not work. Default, laravel relationship assume id as primary key. If you want to use different primary key name then you need to pass that key name in relationship as a second parameter.
$this->hasMany('App\Comment', 'foreign_key', 'local_key');
https://laravel.com/docs/5.6/eloquent-relationships#one-to-many
There are manyway to write the relation code, one is as rkj said anad u did as well.
The other one which i personally prefer is
$this->hasManny(Comment::class,'foreign_key', 'local_key'); //This is only if you are not following laravel standard table syntex.
But if your local key is id and foreign key on another table is comment_id. Then you dont need to add foreign key and primary key. You can simply do like this
$this->hasManny(Comment::class);
For making query, You can simply do this
$dossier = Dossier::where('protocol', $request->protocol)->first(); //This will give object
You do not need = , if its other then that u need to add that. This way its looks much cleaner right?
Then to display you can simply do this $dossier->color

get only one row from first table in table left join in laravel

I have two table
1. Bog_post
2. blog_image
Now I want only one image from blog_image table.
I also save post id in blog_image table.
how can i do this query in laravel ?
Create a model as "BlogImage" and edit it.
class BlogImage extends Model
{
protected $table = 'blog_image';
}
Then create a query with this model.
$blog_image = BlogImage::where('post_id',$post_id)->pluck('image')->first();
You can get just "image" column data in this way, as you want.
Also you can set releationship with this two tables. Here is documentation

Laravel Datatables display data from related tables

I'm using Datatables for displaying data. It works good, but when I want to display data from related tables (models), they do not appear.
Model Keyword.php
public function website() {
return $this->belongsToMany('App\Website');
}
Model Website.php
public function keywords() {
return $this->hasMany('App\Keyword');
}
'websites' database table: id, siteName, siteUrl
'keywords' database table: id, website_id, kwName
Url that should display data: projects/'.$website->id.'/edit (example: projects/2/edit - (edit is a blade))
So, at this Url I want to show a datatable that display all kwName for certain website_id(in this example /2/).
Please, help me what should I use and how to do this.
Your Eloquent relationships are the wrong way around.
keywords has a reference to website via website_id, this is a belongsTo relationship.
website is referenced by many keywords, so this is a hasMany relationship.
Laravel 5.4 Eloquent Relationships

Laravel assumes the database table is the plural form of the model name

By default, Laravel is assuming that the database table is the plural form of the model name. But what if my table name is "news" and i still want to use this feature? should i change it to "newses" or should i use "new" for the model name?
You may specify a custom table by defining a table property on your model as below
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'my_flights';
}
Ref:https://laravel.com/docs/5.1/eloquent
If you have a model ending with the letter 's', it will keep the table name the same. In your case, your model will name your table news by default.
If you want to use another name though, you can use:
protected $table = 'tablename';
inside of your model.
EDIT: I tested this in my application. I made a model named News. Then I made a new instance of News and retrieved the table name:
$news = new News();
dd($news->getTable());
It returns: news
Inside your eloquent model you have to define table name. For example if my model is named user and table in database is named user_of_application then i do it this way
class user extends Model
{
protected $table = 'user_of_application';
}
Laravel uses a "standard set" rule that defines:
A Model is a single instance of a record
A Collection is one or more records
Therefore it assumes that a table is a collection of records.
The nomenclature has a problem when it clashes with various features / gotchas of human languages. For example, what if I have a Model called Sheep? That does mean my Database Table should be called "Sheeps"?
It's up to the developer to avoid "Gollum/Smeagol" syntax. Indeed, you wouldn't want a table called "Newses" as much I'd like to end up with a table called "Sheeps".
Ultimately, I construct Migrations with:
sudo php artisan make:migration create_sheep_table --create=sheep
As for Models, you'll notice in the documentation that they have a different table name for "Flights" called "my_flights"
https://laravel.com/docs/master/eloquent#defining-models
Again, it's up to the developer / DB manager to make decisions on naming conventions that make sense in an application context.

Resources