I have 2 tables which users and groups and I also has created a pivot table for them.
What I want to display is list of the group that user have not joined yet.
my controller
public function redirectFunct()
{
$user = User::find(Auth::user()->id);
$groups=Group::all();
$exists = DB::table('group_user')
->whereuser_id(Auth::user()->id)
->count() > 0;
return view('member.dashboard',['user'=>$user,'groups'=>$groups,'exists'=>$exists]);
}
blade file
#foreach($groups as $groups)
#if($!exists)
<tbody>
<tr>
<td>{{$groups->id}}</td>
<td>{{$groups->groupName}}</td>
<td>{{$groups->groupDesc}}</td>
<td><button type="button" class="btn btn-primary .btn-{color}">Details</button></td>
<td><a href="{{url('/join',$groups->id)}}"><button type="button" class="btn btn-secondary .btn-{color}">Join</button></td>
</tr>
</tbody>
#endif
#endforeach
I do not sure how to check if the data is exist or not in the pivot table. Please help me.
you could try this in your $exist I'm not sure what ->count() > 0; is use for
$exists = DB::table('Group')
->where('userID', Auth::user()->id)
->get();
If you want to display only the group that the user doesn't exist in group you could try this
<tbody>
#foreach($groups as $group)
#if('your group parent id' != 'your user id that related to the group')
<td>{{$group->id}}</td>
<td>{{$group->groupName}}</td>
<td>{{$group->groupDesc}}</td>
<td><button type="button" class="btn btn-primary .btn-{color}">Details</button></td>
<td><a href="{{url('/join',$groups->id)}}"><button type="button" class="btn btn-secondary .btn-{color}">Join</button></td>
#endif
#endforeach
</tbody>
this is the screenshot of that page
The pivot table
Related
I want to create a page that can show data where user_id = user_id logged in
here is my controller that select all data from table, i want to filter it with corresponding logged in user id
public function staffHome()
{
$posts = Post::latest()->paginate(5);
return view('staffHome', compact('posts'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
and here is my view
#foreach ($posts as $post)
<tr>
<td class="text-center">{{ ++$i }}</td>
<td>{{ $post->title }}</td>
<td class="text-center">
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
<a class="btn btn-info btn-sm" href="{{ route('posts.show',$post->id) }}">Show</a>
<a class="btn btn-primary btn-sm" href="{{ route('posts.edit',$post->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?')">Delete</button>
</form>
</td>
</tr>
#endforeach
thanks in advance
your code should be like this
public function staffHome()
{
$posts = Post::where('user_id', Auth::id())->latest()->paginate(5);
return view('staffHome', compact('posts'))->with('i', (request()->input('page', 1) - 1) * 5);
}
You can do it like this
$posts = Post::where('user_id', Auth::id())->latest()->paginate(5);
Where Auth::id() is currently logged in user's id.
Or with relation
Auth::user()->posts()->->latest()->paginate(5);
$posts = Post::where('user_id', $user_id)->latest()->paginate(5);
Please see: https://laravel.com/docs/8.x/collections#method-where
I am new to Laravel, I know I'm just missing out on something yet I cant seem to find it.
I am sending compact('tabletinfo') from my controller index to home.blade.php under tablet folder yet my home.blade.php can't seem to find tabletinfo variable.
Here's the code for index controller.
public function index()
{
$tabletinfo = Tablets::all();
return view ('tablet.home', compact('tabletinfo'));
}
CODE FOR home.blade.php
<tbody>
#foreach ($tabletinfo as $tablet)
<tr>
<td>{{$tablet->owner_name}}</td>
<td>{{$tablet->owner_address}}</td>
<td>
<button class="btn btn-success" type="button" data-toggle="modal" data-target="#viewModal">View Details</button>
<button class="btn btn-info edit" type="button" data-toggle="modal" data-target="#editModal">Edit Details</button>
<button class="btn btn-danger" type="button">Delete</button>
</td>
</tr>
#endforeach
</tbody>
I guess my main problem is my view can't get the tabletinfo variable from my index controller.
there is a mistake. you write tablets instead of tablet
return view('tablet.home',compact('tabletinfo));
Can you please try this one ?
return view('tablet.home')->with('tabletinfo',$tabletinfo);
Or you can use this like
return view('tablet.home',compact['tabletinfo']);
I have a day column in a database and I want to display the other columns according to the day.
#foreach($specialism as $item)
#if($item->day ==="saturday")
<td>
<a class="btn btn-primary btn-lg" href="{{url('button',['id'=>$item->id])}}">
{{$item->name}}<br>
{{$item->day}}<br>
{{$item->from}}<br>
{{$item->to}}
</a>
</td>
#elseif($item->day === "sunday")
<td>
<a class="btn btn-primary btn-lg" href="{{url('button',['id'=>$item->id])}}">
{{$item->name}}<br>
{{$item->day}}<br>
{{$item->from}}<br>
{{$item->to}}
</a>
</td>
#elseif($item->day === "monday")
<td>
<a class="btn btn-primary btn-lg" href="{{url('button',['id'=>$item->id])}}">
{{$item->name}}<br>
{{$item->day}}<br>
{{$item->from}}<br>
{{$item->to}}
</a>
</td>
But it doesn't work. What's the problem??
You should sort the data before you get to the foreach loop and then just echo out the html.
If you use a date field with a carbon instances you also have a nummeric representation of the weekday so you could use that, like:
$collection->sortBy(function($a, $b){
return $a->date->day <=> $b->date->day; // date === carbon instance
});
If you only have a string representation you should get the weekday number by using either Carbon or PHP date, strtotime or DateTime and generate the number.
https://carbon.nesbot.com/docs/#api-getters
The problem is, is that when I sign a new user up (in my application a 'charity owner'), they are redirected to their dashboard.
On the dashboard, displays the all the charities that the user owns and any donations made to them in another table.
However, because the user has not added their charity to the database yet I am getting undefined variables.
#forelse($ownedCharities as $owned)
<td> {{ $owned->name }} </td>
<td> Not Available </td>
<td> <p id="desc"> {{ $owned->description }} </p> <p style="color: #000;" class="seeMore"> See more... </p> </td>
<a href="#"> <button class="btn btn-danger btnPopover"
data-toggle="popover" data-placement="top"> Delete </button> </a> </td>
<td> <button class="btn btn-warning"> Edit </button> </td>
</tr>
#empty
<p> No charities owned </p>
#endforelse
I am using Blade's #forelse
You can use php functions in blade like to see if they are or not defined or has some value
#isset($records)
// $records is defined and is not null
#endisset
#empty($records)
// $records is "empty"...
#endempty
You can use Null coalescing operator which is introduces in PHP 7
$records ?? ""
I want to know how to fetch name of checkboxes from dynamically created form. First I am displaying each records of data in a separate form then If records need to be deleted we will be able to delete. I am able to give different name as somename+id. But how do I know which name is going in the controller. As It is not clear that which name is going in the controller, I am not able to delete the record. I am doing it in laravel 5.4. Here is my code -
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td>
<span class=""><input type="checkbox" name="deletecolor[{{$color->id}}]" value="{{$color->id}}"></span>
</td>
<td>
<div style="background:{{$color->web_color}}">a</div>
</td>
<td>{{$color->color_name}}</td>
<td>
<button type="submit" class="btn btn-danger">
Delete
</button>
</td>
</form>
</tr>
#endforeach
#endif
then on form submission I want to fetch which I am doing like this -
public function destroy(Request $request)
{
//
$id = $request->input('deletecolor');
$affected = DB::update("DELETE FROM vehicle_color where id = ?", [$id]);
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
I see you have $color->id. Why not delete them based on that?
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td><span class=""><input type="checkbox" name="deletecolor[{{ $color->id }}]" value="{{$color->id}}"></span></td>
<td><div style="background:{{$color->web_color}}">a</div></td>
<td>{{$color->color_name}}</td>
<td><button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#colorDelPopup">Delete</button></td>
</form>
</tr>
#endforeach
#endif
And in your controller:
public function destroy(Request $request)
{
$ids = $request->input('deletecolor');
// Instead of raw SQL, you can use the query builder to make your life a bit easier
$affected = DB::table('vehicle_color')->whereIn('id', $ids)->delete();
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
This should do the trick.