Laravel Building Referral System- Cant display Users in Blade - laravel

I Building an Affiliate System for my Users. Registration with ID works fine and save all correct in my DB, but i cant see any Referrals that use my URL ID for Registration in my Example Blade.
Any one cant find the Error?
Controller:
$referal_id = $user->id;
$referer_id = $request['referal_id'];
$referalUserData = [
'referer_id' => $referer_id,
'referal_id' => $referal_id,
];
$insert = ReferralUser::insert($referalUserData);
Blade:
<tr>
<th>#</th>
<th>User Id#</th>
<th>User Name</th>
<th>Registration Time</th>
</tr>
</thead>
<tbody>
#if(!empty($referalUser))
#foreach($referalUser as $referData)
<tr>
<th scope="row">{{$referData->id}}</th>
<td>{{$referData->referal_id}}</td>
<td>#for($i=0;$i<count($referData['names']);$i++) {{$referData['names'][$i]['name']}} #endfor
</td>
<td> {{$referData->created_at}}<br/> <span class="label label-primary">
{{Carbon\Carbon::createFromTimestamp(strtotime($referData->created_at))->diffForHumans()}} </span> </td>
</tr>
#endforeach
#else
<div class="alert alert-noreferals">
<strong></strong><span>No referrals!</span>
</div><br/><br/>
#endif
Ignore the created_at Timestamp.
Thanks

Did you convert the $request object to an array?
$request['referal_id'];
That looks suspicious. Typically you would use
$request->input('referal_id')
or via dynamic properties
$request->referal_id
To access a given value.
Are you getting an error? Anything in the log files?

Related

Laravel get one record from the database

Im trying to make a list with game developers, and when you click on one of them you can see the games they have made. I only dont know how to get further....
This is what i have now:
Index.blade.php:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Naam</th>
<th scope="col">Opgericht in:</th>
</tr>
</thead>
<tbody>
#foreach($gamedevs as $gamedev)
<tr>
<td>{{ $gamedev['id'] }} </td>
<td>{{ $gamedev['naam'] }}</td>
<td>{{ $gamedev['opgericht'] }}</td>
</tr>
#endforeach
</tbody>
I already tried some methods like these:
href="{{route('games.show', ['id'=>$gamedev->id])}}
and
href="/games/{{$gamedev['id']}}
listcontroller.php
public function index()
{
$gamedevs = Gamedevs::all();
return view("index", [ 'gamedevs' => $gamedevs]);
}
public function show(Gamedevs $gamedevs)
{
$gamedevs = Gamedevs::where($gamedevs)->first();
return view('pages.games.show')->with('Gamedevs',$gamedevs);
}
Never did this before.. so hope im on the right way :)
Laravel controller methods like show will use service container to resolve all the parameters you define there. Since you define a model, laravel will automatically find the one corresponding with explicit model binding.
So you wont have to write another query to show your Gamedev:
public function show(Gamedevs $gamedevs)
{
return view('pages.games.show')->with('Gamedevs',$gamedevs);
}
Sample route in web.php
Route::get('/games/{dev_id}','listController#show');
Sample Method in your listController.php
public function show($gamedevs)
{
#let's say your $gamedevs is your devs id so target their id's
$devs = Gamedevs::where('id',$gamedevs)->first();
return view('pages.games.show')->with([
'Gamedevs'=>$devs
]);
}
In your view
href="/games/{{$Gamedevs->id}}

Passing multiple variables in Controllers

I am trying to add an update table on a show blade for a Laravel project.
The idea being if someone is paid owed subs, they can be check off on the roll.
I can pass the roll.id into the Controller, however this is a member view so I need to grab the roll.member_id of the roll.id record and pass this into the redirect to show the updated member page
Here is the html table
<table class="table">
<thead class = 'text-primary'>
<th class="text-center">Date</th>
<th></th>
</thead>
<tbody>
#foreach ($member->outstanding as $o)
<tr>
<td class="text-center">{{$o->roll_id}}</td>
<td class="text-center"><i class="material-icons">done</i></td>
</tr>
#endforeach
</tbody>
</table>
This is the RollController#updateRoll
public function updateRoll($id){
$o = Roll::find($id);
if ($o != null)
{
$o->status = "C";
$o->save();
return redirect(action('MembersController#show'))->with ('success', 'Member Present');
}
return redirect(action('MembersController#index'));
}
So I would like to add the Member_id into the
return redirect(action('MembersController#show'))->with ('success', 'Member Present');
So I am taken back to the member show blade.
Have you tried passing the ID to the action, same as in your blade view?
For example:
return redirect(action('MembersController#show', $o->member_id))
->with ('success', 'Member Present');

Spring Boot And Thymeleaf remain on the same page on post request is made

I've created an web app that searches for online car ads and populates the page with a few tables after a post request.It looks like this:
After Search:
Every table element has a save button which should save those values to the database but my problem is that the page gets refreshed after submitting a post request. How can I do this without refreshing the page?
Table:
<form th:action="#{/saveAd}" method="POST">
<table class="table table-sm table-hover">
<thead class="thead-dark">
<tr>
<th>Imagine</th>
<th>Titlu Anunt</th>
<!-- <th style="width: 16.66%">Link</th> -->
<th>Pret</th>
<th>Oras</th>
</tr>
</thead>
<tbody id="myTable">
<th:block th:each="element : ${lista}">
<trth:onclick="'javascript:rowClicked(\'' + ${element.url} + '\');'">
<td><img th:src="#{${element.img}}" class="size" /></td>
<td th:text="${element.title}"></td>
<td th:text="${element.pret}"></td>
<td th:text="${element.oras}"></td>
<td><input class="btn btn-danger" type="submit"value="Save"></td>
</tr>
</th:block>
</tbody>
Controller:
#RequestMapping(path = "/saveAd", method = RequestMethod.POST)
public String saveAd(#ModelAttribute("adValue") AdValue adValue) {
System.out.println(adValue.getImg());
System.out.println(adValue.getLink());
System.out.println(adValue.getOras());
System.out.println(adValue.getPret());
System.out.println(adValue.getTitle());
return "home";
}
And also, how could I bind the list to a model object after pressing the save button?
I guess you may like this project. It is similar with what you need :
https://github.com/adinafometescu/tutorials/tree/master/spring-elasticsearch
You can use .bootstrapTable(). It a very method to update dynamically your table.
<table id="car-table" class="table table-striped">
<thead>
<tr>
<th data-field="id">Id</th>
<th data-field="title">Title</th>
<th data-field="price">Price</th>
<th data-field="city">City</th>
<th data-width="10" data-formatter="saveButtonFormatter"/>
</tr>
</thead>
</table>
The beauty is that it will map the data-field to a java object.
js stuff:
function saveButtonFormatter(value, row, index){
var adId = row.id;
return "<button class=\"btn btn-danger \" data-title=\"Save\"
data-toggle=\"modal\" onclick=\"saveAd(\'"+adId+"\')\"
</button>"
}
The function saveAd(adId) will call the rest endpoint and will update the bootstrap table on success.
I see that in your thymeleaf you don't have inputs. I suggest to not post your entire object if you don't need user input, just the id.
// anotate the controller class with #RestController
#Autowired
AdService adService;
#PostMapping(path = "/ad/{id}")
public ResponseEntity<?> saveAd(#PathVariable("id") String id) {
adService.saveAd(id);
return new ResponseEntity<>(HttpStatus.ACCEPTED);
}
P.S : don't write code in Romanian :D

Use image path stored in database to display the image in Laravel 5.0

In my ImageController, I've moved an image to a directory and saved the image path in a table in database as,
public function store(Request $request)
{
$new_country = new SelectCountry();
$message = [
'required' => "This field can not be empty",
];
$this->validate($request, [
'country_name' => 'required',
'alternate_title' => 'required',
'country_flag' => 'required',
], $message);
$new_country->country_name = $request->country_name;
$new_country->alternate_title = $request->alternate_title;
if($request->hasfile('country_flag'))
{
$country_flag_image = $request->file('country_flag');
$country_flag_name = $country_flag_image->getClientOriginalName();
$extention = $country_flag_image->getClientOriginalExtension();
$dirpath = public_path('assets/images/country/');
$country_flag_image->move($dirpath,$country_flag_name);
$new_country->country_flag_path = $dirpath.$country_flag_name;
}
$new_country->save();
return redirect()->route('admin.country');
}
Now I want to use the path and display the image along other informations. I've used the following code to return view,
public function select_country()
{
$countries = SelectCountry::all();
return view('auth.select_country')->with('countries',$countries);
}
And in the view, I've used following code to display datas stored in the database,
<table class="table-hover table-responsive container-fluid col-xs-12 col-sm-12">
<thead>
<tr>
<th>Country Name</th>
<th>Alternate Title</th>
<th>Country Flag</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($countries as $country)
<td>{{ $country->country_name }}</td>
<td>{{ $country->alternate_title }}</td>
<td><img src="{{ $country->country_flag_path }}"></td>
<td>
<a type="button" href="" class="btn btn-warning btn-xs">Update</a>
<a type="button" href="" class="btn btn-danger btn-xs">Delete</a>
</td>
#endforeach
</tbody>
The problem is the image is not displayed while other informations are displayed. The src attribute in img tag is returning path but not directory.
If there is any solution, please tell me. Thanks.
I think your issue lies with where you are setting the storage folder. Laravel with automatically reference the public folder for storage of images etc so there's no need for the public_path function.
Try setting your $dirpath to just $dirpath = "/asset/images/country" as Laravel looks inside public by default. You can then reference it by doing <img src="{{ $country->country_flag_path }}">
Then upload a new image and see what the database has the path set to, hopefully it should be a bit more normal :)
Please note that my original answer of:
Try setting your $dirpath to just $dirpath = "images/country" as Laravel looks inside public by default. You can then reference it by doing <img src="{{ asset($country->country_flag_path) }}">
Should still work as it's how I've always done it :)

Invalid argument supplied for foreach() in Laravel

I have double foreach here. Here is my view
<table border="1">
<tr>
<th>ID</th>
<th>ISBN</th>
<th>Klasifikasi</th>
<th>Lokasi</th>
<th>Cp_Or</th>
<th>Tahun</th>
<th>ID_Master_Buku</th
<th>Jenis</th>
<th>Status</th>
<th>Tgl_Masuk</th>
<th>can_issue</th>
<th>ID</th>
<th>Edisi</th>
<th>Pengarang</th>
<th>Deskripsi</th>
<th>Penerbit</th>
<th>Judul</th>
<th>Jumlah_Buku</th>
<th>Bahasa</th>
<th>Gambar</th>
<th>Subjek</th>
<th>Topik</th>
</tr>
#foreach($bukus as $buku)
#foreach($buku->tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>
<td>{{$buku->Lokasi}}</td>
<td>{{$buku->Cp_Or}}</td>
<td>{{$buku->Tahun}}</td>
<td>{{$buku->ID_Master_Buku}}</td>
<td>{{$buku->Jenis}}</td>
<td>{{$buku->Status}}</td>
<td>{{$buku->Tgl_Masuk}}</td>
<td>{{$buku->can_issue}}</td>
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>
<td>{{$item->Penerbit}}</td>
<td>{{$item->Judul}}</td>
<td>{{$item->Jumlah_Buku}}</td>
<td>{{$item->Bahasa}}</td>
<td>{{$item->Gambar}}</td>
<td>{{$item->Subjek}}</td>
<td>{{$item->Topik}}</td>
#endforeach
</tr>
#endforeach </table>
It works in my other views. But here, I got an error
Invalid argument supplied for foreach()
Could you please help me? Thanks in advance
Sounds a lot like at least one $buku->tmbuku is null...
Try wrapping it in an #if:
#foreach($bukus as $buku)
#if($tmbuku = $buku->tmbuku
#foreach($tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>
<td>{{$buku->Lokasi}}</td>
<td>{{$buku->Cp_Or}}</td>
<td>{{$buku->Tahun}}</td>
<td>{{$buku->ID_Master_Buku}}</td>
<td>{{$buku->Jenis}}</td>
<td>{{$buku->Status}}</td>
<td>{{$buku->Tgl_Masuk}}</td>
<td>{{$buku->can_issue}}</td>
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>
<td>{{$item->Penerbit}}</td>
<td>{{$item->Judul}}</td>
<td>{{$item->Jumlah_Buku}}</td>
<td>{{$item->Bahasa}}</td>
<td>{{$item->Gambar}}</td>
<td>{{$item->Subjek}}</td>
<td>{{$item->Topik}}</td>
</tr>
#endforeach
#endif
#endforeach
Try checking if its null and items count before executing the second loop e.g.:
#foreach($bukus as $buku)
#if(!is_null( $buku->tmbuku ) && count( $buku->tmbuku ) )
#foreach($buku->tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>....
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>...
</tr>
#endforeach
#endif
#endforeach

Resources