public function onFailure(\Maatwebsite\Excel\Validators\Failure ...$failures)
{
$data = [];
foreach ($failures as $failure) {
$data[] = [
'row' => $failure->row(),
'attribute' => $failure->attribute(),
'values' => json_encode($failure->values()),
'errors' => json_encode($failure->errors()),
'module' => 'User',
];
}
dd($data);
Excel::download(new FailureExportPI($data),'failure_imports.xlsx');
}
i am trying to handle on failure items and download them in excel file but it is not working. when i die dump dd($data) i get the items but the issue is when i pass the data to download nothing happens my export is as below
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class FailureExportPI implements FromView,ShouldAutoSize
{ public function __construct($data)
{
$this->data=$data;
// dd($this->data);
}
public function view(): View
{
return view('mfiintpi.error',['data'=>$this->data]);
}
}
what am i doing wrong?
I am trying to get exercise id to complete the table, how can I get the exercise ID, like I am getting it in Auth::user()->id
<?php
namespace App\Http\Controllers;
use App\Models\MyExercises;
use Illuminate\Http\Request;
use App\Models\Exercise;
use Illuminate\Support\Facades\Auth;
class MyExerciseController extends Controller
{
public function index()
{
$myexercises = MyExercises::paginate();
return view('exercises.myexercises', compact('myexercises'));
}
public function assign(Request $request)
{
$myexercises = MyExercises::create([
'description' =>$request->description,
'done' =>$request->done,
'user_id' => Auth::user()->id,
'exercises_id' =>(xxxxxx),
'place' =>$request->place,
'duration' =>$request->duration,
]);
return redirect()->route('myexercises.index');
I have tried doing it like this in the Controller, but right now I am a bit lost on how to proceed, thank you!
public function id()
{
$client = DB::table('My_exercises')
->where('id', '=', $request->get('id'))
->first();
}
I have a store function in my controller which I use to add data to my model.
In this store function, I have a piece of code that is used to add an expense in the ʻexpends` table
public function store(Request $request) {
$model = Model::create([
'name' => $request->name
])
if ($request->add_expends == true) {
Expend::create([
'amount' => $request->amount
])
}
}
The problem is, this piece of expense add code is used on multiple controllers (more than 10) across my application.
Is there a way to make this code reusable?
This is quite annoying because if I have to make a change I would have to do it in ten controllers and that's a problem.
Example of what I want to avoid :
public function store(Request $request) {
$model = Model2::create([
'name' => $request->name
])
if ($request->add_expends == true) {
Expend::create([
'amount' => $request->amount
])
}
}
public function store(Request $request) {
$model = Model3::create([
'name' => $request->name
])
if ($request->add_expends == true) {
Expend::create([
'amount' => $request->amount
])
}
}
In your case I would use some trait.
For example create a folder app/Http/Controllers/Traits and put there your trait ExpendTrait
<?php
namespace App\Http\Controllers\Traits;
Trait ExpendTrait {
private function createExpend()
{
if (request()->add_expends == true) {
Expend::create([
'amount' => request()->amount
]);
}
}
}
Now use it in each of your controllers
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Traits\ExpendTrait;
class MyController extends Controller
{
use ExpendTrait;
public function store(Request $request) {
$model = Model::create([
'name' => $request->name
]);
$this->createExpend();
}
}
I'm new to laravel and i'm trying to add a new row in my database but it is not working.
The ultimate goal is to link it to my react native app so that every time a new user logs in, they get a unique token. For now just adding a new row with static data would be nice.
this is what I have in my routes: web.php:
Route::post('/posts/addnewdata', 'PostsController#create');
Route::get('/posts/printall', 'PostsController#index');
Route::get('/posts/{post}', 'PostsController#show');
and then in my controllers I have this:
<?php
namespace App\Http\Controllers;
use DB;
use App\Post;
class PostsController
{
public function index()
{
$allposts = Post::all();
$allposts->toJson();
return $allposts->toJson(JSON_PRETTY_PRINT);
}
public function show($slug)
{
$post = Post::where('title', $slug)->firstOrFail();
$post->toJson();
return $post->toJson(JSON_PRETTY_PRINT);
}
public function create()
{
$newData = array('id' => 6, 'title' => 'post-6', 'slug' => 'blog-6', 'body' => 'blog six in the database');
Post::create($newData);
}
}
when i type in the http://someurl/printall or the http://someurl/{some slug that is in my DB} it works, but the addnewdata gives a 404.
In my application, a user has the ability to remind another user about an event invitation. To do that, I need to pass both the IDs of the event, and of the user to be invited.
In my route file, I have:
Route::get('events/{id}/remind', [
'as' => 'remindHelper', 'uses' => 'EventsController#remindHelper']);
In my view, I have:
{!!link_to_route('remindHelper', 'Remind User', $parameters = array($eventid = $event->id, $userid = $invitee->id) )!!}
In my controller, I have:
public function remindHelper($eventid, $userid)
{
$event = Events::findOrFail($eventid);
$user = User::findOrFail($userid);
$invitees = $this->user->friendsOfMine;
$invited = $event->helpers;
$groups = $this->user->groupOwner()->get();
return view('events.invite_groups', compact('event', 'invitees', 'invited', 'groups'));
}
However, when I hit that route, I receive the following error:
Missing argument 2 for App\Http\Controllers\EventsController::remindHelper()
I'm sure I have a formatting error in my view, but I've been unable to diagnose it. Is there a more efficient way to pass multiple arguments to a controller?
When you define this route:
Route::get('events/{id}/remind', [
'as' => 'remindHelper', 'uses' => 'EventsController#remindHelper']);
You are saying that a single URI argument will be passed to the method.
Try passing the two arguments, like:
Route::get('events/{event}/remind/{user}', [
'as' => 'remindHelper', 'uses' => 'EventsController#remindHelper']);
View:
route('remindHelper',['event'=>$eventId,'user'=>$userId]);
Route :
Route::get('warden/building/{buildingId}/employee/{employeeId}',[
'uses'=>'WardenController#deleteWarden',
'as'=>'delete-warden'
]);
View :
Controller:
public function deleteWarden($buildingId,$employeeId){
$building = Building::find($buildingId);
$building->employees()->detach($employeeId);
return redirect('warden/assign/'.$buildingId)->with('message','Warden Detached successfully');
}
This is how you do it:
Click Here
Go to your controller and write code like following:
public function passData()
{
$comboCoder=['Bappy','Sanjid','Rana','Tuhin'];
$ffi=['Faisal','Sanjid','Babul','Quiyum','Tusar','Fahim'];
$classRoom=['Sanjid','Tamanna','Liza'];
return view('hyper.passData',compact('comboCoder','ffi','classRoom'));
}
/*
Again, in View part use:
(passData.blade.php)
*/
<u>Combocoder:</u>
#foreach($comboCoder as $c)
{{$c}}<br>
#endforeach
<u>FFI</u>
#foreach($ffi as $f)
{{$f}}<br>
#endforeach
<u>Class Room </u>
#foreach($classRoom as $cr)
{{$cr}}<br>
#endforeach
Route::get('/details/{id}/{id1}/{id2}', 'HomeController#SearchDetails');
//pass data like the below code
<a href="{{url("/details/{$orga_list->dcode}/{$orga_list->dname}/{$GroupHead}")}}"
target="_blank" > Details </a>
//controller write like the below code
public function SearchDetails($id, $searchtext,$grp_searchtext)
{
// get data like the below code
$data['searchtext'] = $searchtext;
$data['grp_searchtext'] = $grp_searchtext;
$data['id_is'] = $id;
}
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BookController;
Route::controller(BookController::class)->group(function () {
Route::get('author/{author_name}/book/{title}', 'show')
->name('book.show');
});
Now update the controller like:
app/Http/Controllers/BookController.php
namespace App\Http\Controllers;
use App\Models\Book;
use App\Models\Author;
use Illuminate\Http\Request;
class BookController extends Controller
{
public function show(Request $request, Author $author, Book $book)
{
return view('show',[
'book' => $book->show($request)
]);
}
}
Now update the book model:
app\Models\Book.php
namespace App\Models;
use App\Common\HasPdf;
use App\Common\HasImage;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\URL;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Book extends Model
{
use HasFactory;
protected $guarded = [];
public function author() : BelongsTo
{
return $this->belongsTo(Author::class);
}
public function url()
{
return URL::route('book.show', [
'author_name' => $this->author->author_name,
'title' => $this->title,
]);
}
}
<h3>{{ $item->title }}</h3>
Hope it can help you.