In Student model, i have made attribute like this
protected $appends = ['present'];
public function getPresentAttribute(){
return $this->getTotalDays()-$this->getAbsentDays();
}
My Database looke like this
| id | student_name | created_at |
*----*----------------------*-------------------*--
| 1 | Adam | 2020/11/10 |
| 2 | Annie | 2020/11/10 |
|3 | Paul | 2020/11/10 |
if i do like this in controller then
$students=Student::get();
foreach($students as $stu){
echo "<pre>"; print_4($stu->student_name,$stu->present); die;
}
My result will be like this
Adam -> 32
Annie ->34
Paul->33
But i need to convert like this
['Students', 'Present Day',],
['Adam' , 32],
['Annie' , 34],
['Paul' , 33],
So in controller, I tried like this
public function attendancePerformance(){
$students=Student::get();
$data=[];
$student_data=[];
$arrayHeader = ["Students","Present Day"];
array_push($data,$arrayHeader);
foreach ($students as $stu) {
array_push($student_data, $stu->student_name,$stu->present);
}
array_push($data,$student_data)
}
But it not working.How should i do it ?
Related
How do I store data in users.sport_id:
User class:
public function sport() {
return $this->belongsToMany('App\Sport');
}
Where sports table has:
+----+------------+
| id | sport |
+----+------------+
| 1 | baseball |
+----+------------+
| 2 | basketball |
+----+------------+
Do I add to user.sport_id = 1,2 or save it as array [1,2] and use $casts = ['sport_id'=>'array'];
messages Table
+----+----------+
| id | text |
+----+----------+
| 1 | message1 |
| 2 | message2 |
+----+----------+
tags table
+----+-----------+
| id | name |
+----+-----------+
| 1 | valentine |
| 2 | funny |
| 3 | santa |
+----+-----------+
message_tag
+----+------------+--------+
| id | message_id | tag_id |
+----+------------+--------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
+----+------------+--------+
I have written this query so far to get all messages but i cant return the
message where tag_id = 1;
Message::with([
'tags'=>function($q){
$q->select("name","tag_id");
}
])->select("id", "text")->paginate(10);
i have used where clause inside function($q){$q->where('tag_id',1)}. but it didn't worked.
You have to include use Illuminate\Support\Facades\DB; at the top of class
$tagId = 1;
$return = DB::table('message_tag')
->join('messages', 'messages.id', '=', 'message_tag.message_id')
->join('tags', 'tags.id', '=', 'message_tag.tag_id')
->where("message_tag.tag_id", "=", $tagId)
->select('messages.text', 'tags.text')
->get();
Try using whereHas(), assuming you set a many-to-many relationship properly:
$tag = 'Madhab452';
$messages = Message::whereHas('tags', function ($query) use ($tag) {
$query->where('name', '=', $tag)
->select('name', 'tag_id');
})->get();
Book
----------------------------
| id | name | published |
----------------------------
| 1 | book1 | 1 |
| 2 | book2 | 1 |
| 3 | book3 | 0 |
Chapter
----------------------------
| id | book_id | name | published |
----------------------------
| 1 | 1 | chapter1 | 1 |
| 2 | 1 | chapter2 | 0 |
| 2 | 2 | chapter1 | 0 |
| 3 | 3 | chapter1 | 1 |
class Book{
public function getChapter()
{
return $this->hasMany(Chapter::className(), ['kook_id' => 'id']);
} }
class Chapter{
public function getBook()
{
return $this->hasOne(Book::className(), ['id' => 'book_id']);
} }
How can i get published books with published pages using ActiveRecord (i want get book1 with chapter1 and book2 without any chapters)?
smth like Book::find($id)->where(['published' => 1])-> {{{left join with where}}} ->all())
ADDED
And then i wand to use it
echo $book->name;
foreach($book->getChapter() as chapter){
echo chapter->name;
}
Change your Book class relation as
class Book
{
public function getChapter()
{
return $this->hasMany(Chapter::className(), ['book_id' => 'id'])->where(['published' => 1]);
}
}
To get Related Records use with()
Book::find()->with(['chapter'])->where(['id' => $id ,'published' => 1])->all()
To Use it:
//Book Name
echo $book->name;
//For Chapters Name
if($book->chapter){
foreach($book->chapter as $chapter){
echo $chapter->name;
}
}
as would do this in laravel ?
I need to pass this condition laravel
$query = mysql_query("SELECT * FROM citas WHERE
especialidad='".$especialidad."'AND doctor='".$doctor."'AND
fecha ='".$fecha."' AND hora = '".$hora."'" , $con);
if ($existe = mysql_fetch_object($query))
{
}else{}
this is my table in the database:
fecha | hora | especialidad | doctor |
12-07-2015 | 01:17:00 | General | Carlos |
12-07-2015 | 01:35:00 | General | Carlos |
This would be what shipping.
fecha | hora | especialidad | doctor |
12-07-2015 | 01:22:00 | General | Carlos | <--- It is saved by the time it is different
12-07-2015 | 01:35:00 | General | uriel | <--- It is saved by the dcotor it is different
12-07-2015 | 01:35:00 | General | carlos | <--- It is no, because
it already exists
Try this:
$query = DB::table('citas')
->where('especialidad',$especialidad)
->where('doctor',$doctor)
->where('fecha',$fecha)
->where('hora',$hora)
->get();
if($query != null){
// your code will be here
}else{
// you code will be here
}
Or you can try this by Eloquent Model. I assume you have a model named Cita
Add use App\Cita at top of your code after namespace in your controller
$query = Cita::where('especialidad',$especialidad)
->where('doctor',$doctor)
->where('fecha',$fecha)
->where('hora',$hora)
->get();
if($query != null){
// your code will be here
}else{
// you code will be here
}
I have two simple tables, cats and breeds:
mysql> select * from cats;
+----+--------+---------------+----------+---------------------+---------------------+
| id | name | date_of_birth | breed_id | created_at | updated_at |
+----+--------+---------------+----------+---------------------+---------------------+
| 1 | Rita | 2008-07-06 | 1 | 2015-05-09 20:40:49 | 2015-05-09 20:50:20 |
| 2 | Muni | 1992-05-15 | 3 | 2015-05-09 20:50:54 | 2015-05-09 20:50:54 |
| 3 | Hector | 2005-01-23 | 4 | 2015-05-09 21:08:23 | 2015-05-09 21:08:23 |
+----+--------+---------------+----------+---------------------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from breeds;
+----+------------+
| id | name |
+----+------------+
| 1 | Domestic |
| 2 | Persian |
| 3 | Siamese |
| 4 | Abyssinian |
+----+------------+
4 rows in set (0.00 sec)
All I want to do is get a list of the cats that are of a certain breed. In the cats table, breed_id is a FK that points to the PK of the breeds table. The example code in my book which is supposed to return a view with all cats of a specific breed returns no results:
The url I am trying is /cats/breeds/Domestic
The view that is generated uses this logic in the routes.php file:
Route::get('cats/breeds/{name}', function($name)
{
$cats = Furbook\Cat::with('breeds')
->whereName($name)
->get();
dd($cats);
});
When I dd($cats) I get zero results even though I have a cat with a domestic breed_id of 1. What have I done wrong here?
EDIT:
I can get it to work with the following hacky code, which first gets the id from the breeds table by querying against the name field, then queries the cats table with that id:
$breed = Furbook\Breed::whereName($name)->get();
$id = $breed[0]['attributes']['id'];
$cats = Furbook\Cat::whereId($id)->get();
How do I make this into one Eloquent query? I see no examples for this kind of query on the Laravel site.
For more information this is how the models look:
class Breed extends Model {
public $timestamps = false;
public function cats()
{
return $this->hasMany('Furbook\Breed');
}
}
class Cat extends Model {
protected $fillable = [
'name',
'date_of_birth',
'breed_id',
];
public function breed()
{
return $this->belongsTo('Furbook\Breed');
}
}
You're asking the system to give you all cats with a name of Domestic - not all cats of the breed name Domestic.
Assuming your model relationships are in order, you could do e.g.
$cats = Furbook\Breed::whereName($name)->first()->cats;
Also, my Burmese cat just hit the monitor; I think she's upset that she's not in the breeds list.