Laravel using where clause on Views - laravel

I'm newbie on Laravel.
I can send data like that:
public function index()
{
$InboxNew = Models\Inbox::where('read', false)->get();
$InboxMarkedAsRead = Models\Inbox::where('read', true)->get();
return view('dashboard.inbox', compact('InboxNew', 'InboxMarkedAsRead'));
}
I want to get data in view like that but gives some errors:
public function index()
{
$Inbox = Models\Inbox::all();
return view('dashboard.inbox', compact('Inbox'));
}
In view:
#if($Inbox->where('read', false)->get())
...
#endif

Your controller :
public function index()
{
$Inbox = Models\Inbox::all();
return view('dashboard.inbox', compact('Inbox'));
}
In blade you can achieve your data like this way :
#foreach($inbox as $query)
#if($query->read == false)
//
#endif
#endforeach

Problem with your code is that you have already called all() method on the Models\Inbox. What all() do is to simply call the get() method on model without applying any conditions.
You either need to define only a model (via query() method:
// Controller
public function index()
{
$Inbox = Models\Inbox::query();
return view('dashboard.inbox', compact('Inbox'));
}
and fetch it later with where clauses In view:
#if($Inbox->where('read', false)->get())
...
#endif
OR
You can do it in a cleaner way, which is to fetch the data in controller and use the view only to show the data
You either need to define only a model (via query() method:
// Controller
public function index()
{
$Inbox = Models\Inbox::where('read', false)->get();
return view('dashboard.inbox', compact('Inbox'));
}
and fetch it later with where clauses In view:
#if($Inbox)
...
#endif
PS: To check if a record exists, use exists() method instead of get() e.g $Inbox->exists()

Related

Laravel collection display and short issue

Hello guys!
I have a a little problem with collections. I have never worked with these. I would like to display a collection at my welcome blade but there is a problem. My collection is not in the contrroller, the collection's place is in the App/Repositories/xyz.php and in a function. How can i pass this function to the controller and after show it at the welcome blade??
App/repositories/xyz.php
public function getcars(): Collection
{
return collect([
new Car(...)
)];
controller
public function __invoke(): View
{
return view('welcome', ['cars' => collect([
new Car() ------> I would like to put datas from xyz.php repo here
new Car()
new Car()
....
And welcome.blade file where i would like to display
<div class="car-list">
<h2>{{ $title }}</h2>
#foreach($cars as $car)
<x-car :car="$car" />
#endforeach
</div>
You could do that in many ways:
Creating new instance
use App\repositories\Xyz;
public function __invoke(): View
{
$repo = new Xyz();
return view('welcome')->with('cars', $repo->getcars());
}
Pulling your class from the container
use App\repositories\Xyz;
public function __invoke(): View
{
$repo = app(Xyz::class);
return view('welcome')->with('cars', $repo->getcars());
}
Using dependency injection to resolve it from container
use App\repositories\Xyz;
protected Xyz $repo;
public function construct(Xyz $xyz): View
{
$this->repo = $xyz;
}
public function __invoke(): View
{
return view('welcome')->with('cars', $this->repo->getcars());
}
I believe from your Controller file, you can create an object property of your Repository via __construct() method like this:
protected MyRepository $myRepository;
public function __construct(MyRepository $myRepository)
{
$this->myRepository = $myRepository;
}
Then, you can call $this->myRepository's method from there, for example like getting records, etc. Then you can pass the result to your view.

accessing custom method in model

Im practicing laravel and im making a custom method for my user
In my user model i have build a function like this
public function employee(){
return $this->where('user_type','employee');
}
and then in my controller I'm accessing the function like this
public function index(){
$users = User::latest()->employee();
return UserResource::collection($users);
}
but it return an error Method Illuminate\Database\Query\Builder::employee does not exist.how to fix this?
Use local scope instand
public function scopeEmployee($query)
{
return $query->where('user_type', 'employee');
}
Your controller can be as it was !
public function index(){
$users = User::latest()->employee()->get();
return ProductsResource::collection($users);
}

One-to-many and one-to-many relationship 1 laravel

i did this(sorry my english it's bad bad....)
controller:
public function index()
{
$lessons = course::find(1)->lesson;
return view('home',compact('lessons'));
}
model lesson
public function course() {
return $this->belongsTo(Course::class);
}
model courses
public function lesson() {
return $this->hasMany(Lesson::class);
}
blade
#foreach ($lessons as $lesson )
<h4>{{$lesson->title}}</h4>
#endforeach
in browser nothing appears
why?:(
First rename your lesson method with plural lessons.
// Course model
public function lessons() // plural
{
return $this->hasMany(Lesson::class);
}
Now get the lesson's collection.
public function index()
{
$lessons = Course::find(1)->lessons;
return view('home', compact('lessons'));
}
#foreach ($lessons as $lesson )
<h4>{{$lesson->course->title}}</h4>
#endforeach
Have you tried it like this:
return view('greeting')->with('lessons', $lessons);
Now you can call '$lessons' in your view. Take a look at this link
https://laravel.com/docs/5.6/views
Can you try to use return $lessons; and show me what it says?

Qyerying a one-to-many relationship

So I have tables Uploads and Avtors (Authors). I am trying to write a query that will get me all the uploads for a given user + the author.
To achieve this I have the following function in my controller :
public function uploads()
$uploads = Auth::user()->upload()->with('author')->paginate(10);
return view('uploads')->with('uploads',$uploads);
}
This function manages to get me all the uploads for a given user, but not the author :
When I debug with dd I get this :
My relationships in the models are difined in the following ways :
User:
public function upload(){
return $this->hasMany('App\Upload');
}
Avtor:
class Avtor extends Model
{
public function uploads(){
return $this->hasMany('App\Upload');
}
}
Upload:
public function user(){
return $this->belongsTo('App\User');
}
public function author(){
return $this->belongsTo('App\Avtor');
}
Can anybody help me find out what am I doing wrong here because I can't by myself?
You can use load() method with nested relationship dot syntax:
auht()->user()->load(['uploads', 'uploads.author'])->get();
In this case, you'll not need to pass any data to a view, just do this:
#foreach (auth()->user()->uploads as $upload)
Or you can use with() method:
$uploads = Upload::with('author')->where('user_id', auth()->id())->get();

conbine response return and view

I have two return in my controller method.
How can I combine both?
public function index()
{
$data=Event::get(['title','start','color']);
$objectifs=Objectif::all();
$ob=Array('objectifs'=>$objectifs);
return view('newc')->with('objectifs', $objectifs);
return Response()->json($data);}
we can't use 'return' multiple times
public function index() {
$data = Event::pluck('title', 'start','color' );
return Response()->json($data);
}
use another controller function
public function jason() {
$ob=Array('objectifs'=>$objectifs);
return view('newc')->with(compact('ob'));
}
You should read this laravel docmentaion
https://laravel.com/docs/5.2
In place of using two return you can use multiple "with" like this
public function index()
{
$data=Event::get(['title','start','color']);
$objectifs=Objectif::all();
$ob=Array('objectifs'=>$objectifs);
return view('newc')->with('objectifs', $objectifs)->with('data',$data);

Resources